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
代码:
 /*
[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的更多相关文章

  1. HDU 4135 容斥

    问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. ...

  2. [容斥原理] hdu 4135 Co-prime

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others) ...

  3. hdu 5901 count prime & code vs 3223 素数密度

    hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...

  4. HDU 4135

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这 ...

  5. hdu 4135 a到b的范围中多少数与n互质(容斥)

    Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains ...

  6. hdu (kruska and prime) 继续畅通工程

    题目http://acm.hdu.edu.cn/showproblem.php?pid=1879 复习一下最小生成树的两个基本算法. 由于存在道路是否已修建的问题,如果已修建,那么该条道路的成本即为0 ...

  7. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  8. 容斥 - HDU 4135 Co-prime

    Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一 ...

  9. 【容斥】HDU 4135 Co-prime

    acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b) ...

  10. 【hdu 4135】Co-prime

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 让你求出[a..b]这个区间内和N互质的数的个数; [题解] 利用前缀和,求出[1 ...

随机推荐

  1. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  2. 暂停和播放CSS3动画的两种实现方法

    1,直接修改animationPlayState <!DOCTYPE html> <html> <head lang="en"> <met ...

  3. centos 7 安装MySQL 5.6

    本文根据mysql的官方文档操作:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 由于Centos7 默认数据库是mariabd(网上 ...

  4. JanaScript预解析

    JS预解析是什么?      在当前的作用域下,js运行之前.会有带有 var 和 function关键字的代码事先声明,      并在内存中安排好,然后从上到下的执行js代码. JS预解析 js逐 ...

  5. JavaScript中的坑--全局变量惹得祸

    js中变量的作用域及闭包的概念   概述 身为一名程序员,因为bug周末加班是必不可少的事情,当解决bug的时候,总有些bug是因为规范导致的,但是这些bug往往不好找,也就是"前人挖坑,后 ...

  6. Java编程代码性能优化总结

    如果有足够的时间开发.维护代码,这时候就必须考虑每个可以优化的细节了,一个一个细小的优化点累积起来,对于代码的运行效率绝对是有提升的. 代码优化的目标是: 1.减小代码的体积 2.提高代码运行的效率 ...

  7. Java条形码/二维码生成和解析

    注意-本类依赖jar包文件:core.jar和zxing-javase.jar 下载jar文件,到本博客文件栏目下载. import com.google.zxing.BarcodeFormat; i ...

  8. VMware安装Redhat6.5

    VMware安装Redhat6.5 VMware安装Redhat6.5安装之前需要一些准备工作,首先,电脑上需要安装VMware(这个是废话),其次就是需要Redhat6.5的镜像文件(现在貌似都出R ...

  9. 业余草推荐18个Java开源免费的CMS系统

    1.InfoGlue infoglue是一个高级的.可扩展的.健壮的内容管理系统,完全用Java开发.重要的功能包括完全支持多语言,站点之间良好的重用,以及广泛的集成能力. 该项目主页:http:// ...

  10. Jersey +jetty 实现微服务(一)

    微服务:    传输协议基本都是http,数据序列化,协议层目前protocol buffer,Json, xml,Java序列化,最多使用Json.实现以上二点并不难,spring 就可以,但spr ...