Co-prime

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

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
2
1 10 2
3 15 5
 
Sample Output
Case #1: 5
Case #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}.

 
Source
/*
* @Author: lyuc
* @Date: 2017-08-16 16:52:21
* @Last Modified by: lyuc
* @Last Modified time: 2017-08-16 21:44:34
*/ /*
题意:给你a,b,n让你求在区间[a,b]内有多少数与n互质 思路:求出n的所有质因子,然后[1,a]区间内的与n互质的数的数量就是,a减去不互质的数的数量,同理
[1,b]的也可以这么求,容斥求出这部分的结果
*/
#include <bits/stdc++.h> #define LL long long
#define MAXN 1005
#define MAXM 10005 using namespace std; int t;
LL a,b,n;
LL factor[MAXN];
LL tol;
LL que[MAXM]; void div(LL n){//筛出来n的因子
tol=;
for(LL i=;i*i<=n;i++){
if(n%i==){
factor[tol++]=i;
while(n%i==){
n/=i;
}
}
}
if(n!=) factor[tol++]=n;//如果是素数的话加上本身
} LL cal(LL x){//容斥计算出[1,x]内与n不互质的元素的个数
LL res=;
LL t=;
que[t++]=-;
for(int i=;i<tol;i++){
int k=t;
for(int j=;j<k;j++){
que[t++]=que[j]*factor[i]*-;
}
}
for(int i=;i<t;i++){
res+=x/que[i];
}
return res;
} int main(){
// freopen("in.txt","r",stdin);
scanf("%d",&t);
for(int ca=;ca<=t;ca++){
printf("Case #%d: ",ca);
scanf("%lld%lld%lld",&a,&b,&n);
div(n);
printf("%lld\n",b-cal(b)-(a--cal(a-)) );
}
return ;
}

HDU 4135 Co-prime(容斥+数论)的更多相关文章

  1. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  2. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

  3. C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥

    C - Visible Trees HDU - 2841 思路 :被挡住的那些点(x , y)肯定是 x 与 y不互质.能够由其他坐标的倍数表示,所以就转化成了求那些点 x,y互质 也就是在 1 - ...

  4. HDU 5297 Y sequence 容斥 迭代

    Y sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Description Yellowstar likes integer ...

  5. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  6. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  7. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

  8. HDU 4609 3-idiots FFT+容斥

    一点吐槽:我看网上很多分析,都是在分析这个题的时候,讲了半天的FFT,其实我感觉更多的把FFT当工具用就好了 分析:这个题如果数据小,统计两个相加为 x 的个数这一步骤(这个步骤其实就是求卷积啊),完 ...

  9. HDU 4336 Card Collector(容斥)

    题意:要收集n种卡片,每种卡片能收集到的概率位pi,求收集完这n种卡片的期望.其中sigma{pi} <=1; 思路:容斥原理.就是一加一减,那么如何算期望呢.如果用二进制表示,0表示未收集到, ...

随机推荐

  1. 关于XCode9的一些问题

    最近几天一直在做一项工作,为新项目在做搭建框架的前期准备,然后恰逢更新iOS11和XCode9,笔者心急的尝了个先,发现了一些问题,记录一下,如果有相同问题的程序猿,也可以参考一下. 目前问题遇到两点 ...

  2. Java的垃圾回收

    Java的垃圾回收 System.gc()和Runtime.gc()用来请求JVM启动垃圾回收 try与return的问题 任何调用try 或者catch中的return语句之前,都会先执行final ...

  3. Spring框架(二)

    Spring反射机制: 1, 通过spring来获取一个对象的实例 <bean id="user" class="com.model.User"> ...

  4. JAVA 并发(待补全!)

    从性能上看 如果没有任务会阻塞 那么在单处理器的机器人使用并发就没有任何意义 (需要上下文切换 时间反而长) 进程是运行在他自己地址空间的自包容的程序 协作多线程与抢占式多线程 想要定义任务需要实现R ...

  5. RMQ-ST算法的理解与实现(C++)

    RMQ-ST的含义 RMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返 ...

  6. http://codeforces.com/contest/834

    A. The Useless Toy time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. BZOJ-1192-[HNOI2006]鬼谷子的钱袋

    Description 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一场拍卖会,其中有一 ...

  8. 深度学习框架caffe在macOS Heigh Sierra上安装过程实录

    第一步.安装依赖库 brew install -vd snappy leveldb gflags glog szip lmdb brew tap homebrew/science brew insta ...

  9. VBA 中窗体模式切换,一次设计2种表现

    Sub ModelChange() Then DoCmd.RunCommand acCmdSubformFormView ''''就这句 Me.Form.AllowEdits = True ' Mod ...

  10. RobotFramework自动化测试框架-移动手机自动化测试Click A Point关键字的使用

    Click A Point关键字用来模拟点击APP界面上的一个点,该关键字接收两个三个参数[ x=0 | y=0 | duration=100 ],x和y代表的是点的坐标位置,duration代表的是 ...