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. eclipse左侧的导航栏不见了怎么调

    点击eclipse上方导航栏的window 然后再点击Reset Perspective 这样左侧的导航栏就出来了

  2. 去掉CI框架默认url中的index.php

    1:.htaccess //放置在根目录下,和入口文件index.php的同级目录<IfModule mod_rewrite.c>RewriteEngine onRewriteCond % ...

  3. [leetcode-582-Kill Process]

    Given n processes, each process has a unique PID (process id) and its PPID (parent process id). Each ...

  4. tab切换实现方式2

    tab切换实现方式2: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  5. web拖动条显示

    <!DOCTYPE html"> <html> <head> <title>滚动条拖动评分的JS效果</title> <st ...

  6. web前段学习2017.6.15

    CSS---表现层,修饰和表现html文档,为了解决结构层和表现层分离的问题. 通过CSS极大的提高了工作效率,方便工作人员维护和管理CSS:层叠样式表,目前用的最广泛的css版本为css2,最新版本 ...

  7. vijos1090题解

    题目: 有n个正整数排成一行.你的目的是要从中取出一个或连续的若干个数,使它们的和能够被k整除. 例如,有6个正整数,它们依次为1.2.6.3.7.4.若k=3,则你可以取出1.2.6,或者2.6.3 ...

  8. spa单页面应用(angular)

    本篇文章是对单页面的一个简单的基本逻辑操作,这个方法可以搭建基本的单页面的逻辑结构. 简单理解:单页面应用,锚点值切换,一个路由对应一个页面. 路由:此时会创建一个信息保存路由的信息,之后对页面a标签 ...

  9. Java自学手记——注解

    注意区分注释和注解,注释是给人看的,注解是给程序看的. 注解的作用是代替配置文件,在servlet3.0中,就可以不再使用web.xml文件,而是所有配置都是用注解!比如注解类 @WebServlet ...

  10. BootLoader--改进(基于2440)

    BootLoader--改进 之前编写的Bootloader启动内核时间使用差不多7秒钟的时间,大多都是用在CPU将内核从Nandflash读取到SDRam中,故首先想到的方法是改变CPU时钟频率. ...