【HDU-4135】Co-prime

来源:互联网 发布:8090端口是干嘛的 编辑:程序博客网 时间:2024/06/05 09:18


点击打开链接



Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5468    Accepted Submission(s): 2181


Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
 

Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).
 

Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
 

Sample Input
21 10 23 15 5
 

Sample Output
Case #1: 5Case #2: 10
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
 

//      互质问题

//代码如下:


/*[a,b] 与n互质的数的个数1~b : a11~a-1 : a2 ans = a1 - a2----> [1,k] 与 n 互质的数的个数----> num - 与n不互质的数的个数*/#include<cstdio>#include<cmath>int p[100000];int ant;void ResolvePrime(int n)//分解质因数 {int endd = sqrt(n);for (int i = 2 ; i <= endd ; i++){if (n % i == 0){p[ant++] = i;while (n % i == 0)n /= i;}}if (n > 1)p[ant++] = n;}__int64 solve(__int64 a,int n)//求 1~ a 不与n互质的数的个数 {__int64 ans = 0;for (int i = 1 ; i < ((__int64)1 << ant) ; i++){__int64 mul = 1;int cnt = 0;for (int j = 0 ; j < ant ; j++){if (i & ((__int64)1 << j))//选中 {cnt++;mul *= p[j];}}if (cnt & 1)ans += a / mul;elseans -= a / mul;}return ans;}int main(){int T;int n;__int64 a,b;int Case = 1;scanf ("%d",&T);while (T--){ant = 0;scanf ("%I64d %I64d %d",&a,&b,&n);ResolvePrime(n);printf ("Case #%d: %I64d\n",Case++,b-(a-1)-(solve(b,n)-solve(a-1,n)));}return 0;}




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 贷款回执单丢了怎么办 医院挂号过号了怎么办 脑子里总是胡思乱想睡不着怎么办 被妹夫给睡了怎么办 被妹夫抱了下怎么办 最里面牙齿烂了怎么办 老公有外遇不承认怎么办 对老公疑心太重怎么办 当你发现老公出轨怎么办 发现自己老婆出轨了怎么办 笔袋密码忘记了怎么办 海关锁密码忘了怎么办 800度近视献了血怎么办 当老公有了外遇妻子该怎么办 妻子有外遇我该怎么办 喜欢拔粗的头发怎么办 小腿长水泡很痒怎么办 脚丫有水泡又痒怎么办 脚上起水泡怎么办很疼 脚上反复长水泡怎么办 高考没考上二本怎么办 车子被扣了60分怎么办 科目三挂了三次怎么办 装修到一半跑了怎么办 13岁自慰被发现怎么办 应用锁密码忘了怎么办 3d缺少dll文件怎么办 真三国无双6闪退怎么办 孙策死了大乔怎么办 30周羊水偏多怎么办 怀孕34周羊水多怎么办 孕34周羊水偏多怎么办 怀孕37周羊水少怎么办 电脑无法识别u盘怎么办 后厨地沟堵了怎么办 浅口鞋子不跟脚怎么办 机械键盘串键了怎么办 银龙突然不吃食怎么办 狗被蜈蚣咬了怎么办 婴儿被孕妇抱了怎么办 手机充电口坏了怎么办