【转】Hdu--4135 Co-prime
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.
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).
between A and B inclusive which are relatively prime to N. Follow the output
format below.
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 : a1
1~a-1 : a2 ans = a1 - a2 ----> [1,k] 与 n 互质的数的个数 ----> num - 与n不互质的数的个数 */
#include<cstdio>
#include<cmath>
int p[];
int ant;
void ResolvePrime(int n) //分解质因数
{
int endd = sqrt(n);
for (int i = ; i <= endd ; i++)
{
if (n % i == )
{
p[ant++] = i;
while (n % i == )
n /= i;
}
}
if (n > )
p[ant++] = n;
} __int64 solve(__int64 a,int n) //求 1~ a 不与n互质的数的个数
{
__int64 ans = ;
for (int i = ; i < ((__int64) << ant) ; i++)
{
__int64 mul = ;
int cnt = ;
for (int j = ; j < ant ; j++)
{
if (i & ((__int64) << j)) //选中
{
cnt++;
mul *= p[j];
}
}
if (cnt & )
ans += a / mul;
else
ans -= a / mul;
}
return ans;
} int main()
{
int T;
int n;
__int64 a,b;
int Case = ;
scanf ("%d",&T);
while (T--)
{
ant = ;
scanf ("%I64d %I64d %d",&a,&b,&n);
ResolvePrime(n);
printf ("Case #%d: %I64d\n",Case++,b-(a-)-(solve(b,n)-solve(a-,n)));
}
return ;
}
【转】Hdu--4135 Co-prime的更多相关文章
- HDU 4135 容斥
问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...
- [容斥原理] hdu 4135 Co-prime
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 5901 count prime & code vs 3223 素数密度
hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...
- HDU 4135
http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...
- hdu 4135 a到b的范围中多少数与n互质(容斥)
Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...
- hdu (kruska and prime) 继续畅通工程
题目http://acm.hdu.edu.cn/showproblem.php?pid=1879 复习一下最小生成树的两个基本算法. 由于存在道路是否已修建的问题,如果已修建,那么该条道路的成本即为0 ...
- hdu 4135 [a,b]中n互质数个数+容斥
http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...
- 容斥 - HDU 4135 Co-prime
Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...
- 【容斥】HDU 4135 Co-prime
acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...
- 【hdu 4135】Co-prime
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 让你求出[a..b]这个区间内和N互质的数的个数; [题解] 利用前缀和,求出[1 ...
随机推荐
- windows下pycharm远程调试pyspark
参考http://www.mamicode.com/info-detail-1523356.html1.远端执行:vi /etc/profile添加一行:PYTHONPATH=$SPARK_HOME/ ...
- 给linux虚拟机添加Samba用户
Window系统连上我们的开发机Linux,自然需要在Samba里添加一个新用户. linux-06bq:/usr/local/services/samba/bin # ./smbpasswd -a ...
- (转载)在spring的bean中注入内部类
原文链接:http://outofmemory.cn/java/spring/spring-DI-inner-class 在spring中注入内部类,有可能会遇到如下异常信息: 2014-5-14 2 ...
- VB6之HTTP服务器的实现
之前用VBS写过一个,效率和支持比较low,这次闲着没事用VB重写了一次. 当前的实现版本仅支持静态文件的访问(*.html之类),支持访问方式为GET,HTTP状态支持200和404. 两个文件,一 ...
- User implements HttpSessionBindingListener
public class User implements HttpSessionBindingListener { private int id; private String userName; p ...
- mac版破解office
下载地址:http://ereach-public.oss-cn-shanghai.aliyuncs.com/office%202016%20for%20mac.dmg 解压密码:www.ifunma ...
- [BZOJ2783/JLOI2012]树 树上倍增
Problem 树 题目大意 给出一棵树,求这个树上的路径的数量,要求路径上的点权和等于s且路径的上每个点深度不同. Solution 这个题目可以用不少方法做. 首先,路径上每个节点的深度不同决定了 ...
- POJ 2152 fire / SCU 2977 fire(树型动态规划)
POJ 2152 fire / SCU 2977 fire(树型动态规划) Description Country Z has N cities, which are numbered from 1 ...
- Spark的误解-不仅spark是内存计算,hadoop也是内存计算
市面上有一些初学者的误解,他们拿spark和hadoop比较时就会说,Spark是内存计算,内存计算是spark的特性.请问在计算机领域,mysql,redis,ssh框架等等他们不是内存计算吗?依据 ...
- SSH连不上虚拟机的问题解决
这几天工作任务不重,今早上班打算在liunx下运行下python脚本.打开VM,SSH突然连不上虚拟机了.网上试了很多方法都不行,最后花费2个小时解决了这一问题. 结合我的实际情况,问题解决如下: 1 ...