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. js验证是否是数字,支持正负数小数

    js验证是否是数字,支持正负数小数. function isShuzi(str){ //var regExp =/[0-9]$/;//不支持小数 var regExp =/^\-?[0-9]+(.[0 ...

  2. python3中socket套接字的编码问题解决

    一.TCP 1.tcp服务器创建 #创建服务器 from socket import * from time import ctime #导入ctime HOST = '' #任意主机 PORT = ...

  3. Jquery-鼠标事件

    鼠标事件是在用户移动鼠标光标或者使用任意鼠标键点击时触发的.(1):click事件:click事件于用户在元素敲击鼠标左键,并在相同元素上松开左键时触发.        $('p').click(fu ...

  4. nuget挂了吗?

    [nuget.org] Unable to load the service index for source https://api.nuget.org/v3/index.json. 发送请求时出错 ...

  5. kbengine服务端引擎技术概览

    http://www.kbengine.org/assets/other/KBEngine_overview.zip

  6. VB6之写注册表

    难免会遇到写注册表的情况,写了个实用点的RegWrite函数.为了减少代码量,用WScript.Shell取代了API来实现. 使用方式就在注释中了,就不再过多解释了.PS:注释比实现代码要丰富多了, ...

  7. [NOIP2013/Codevs3287]货车运输-最小[大]生成树-树上倍增

    Problem 树上倍增 题目大意 给出一个图,给出若干个点对u,v,求u,v的一条路径,该路径上最小的边权值最大. Solution 看到这个题第一反应是图论.. 然而,任意路径最小的边权值最大,如 ...

  8. Java 9 揭秘(11. Java Shell)

    Tips 做一个终身学习的人. 在本章节中,主要介绍以下内容: 什么是Java shell JShell工具和JShell API是什么 如何配置JShell工具 如何使用JShell工具对Java代 ...

  9. php 批量进行复制文件

    php 批量进行复制文件 1.前言 目标:php批量进行复制指定的目录文件夹下所有文件复制到另一个文件夹下 所使用的的php函数:is_dir,opendir,readdir,scandir,mkdi ...

  10. 以Random Forests和AdaBoost为例介绍下bagging和boosting方法

    我们学过决策树.朴素贝叶斯.SVM.K近邻等分类器算法,他们各有优缺点:自然的,我们可以将这些分类器组合起来成为一个性能更好的分类器,这种组合结果被称为 集成方法 (ensemble method)或 ...