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}.

解题思路:求区间[A,B]与N互质的数的个数,我们可以从其对立面来考虑:分别求区间[1,A-1]、区间[1,B]中与N不互质的数的个数为num1、num2,那么区间[A,B]与N互质的数的个数就有(B-num2)-(A-1-num1)。怎么求出区间[1,X]与N不互质的数的个数呢?先分解出N的所有素因子,然后用这些素因子来筛选计算出区间[1,X]中与N不互质的数的个数即X/p_i(p_i为素因子,X为区间右端点),因为任何一个不小于2的数都能表示成若干个素数的乘积,这样就得到区间[1,X]中是素因子的倍数的个数,但为了不重复和不遗漏计数,应采用容斥定理,公式:,其中选择某几个素因子可以看成是二进制对应bit上的1,如果当前所选个数为奇数,符号为正,否则为负。注意:容斥计数x/p_i中p_i是几个素数的最小公倍数,由于素数之间是互质的,所以可以直接相乘起来作为其最小公倍数。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int t,cnt,prime[];LL a,b,n;
LL solve(LL x){//求与n不互质的总个数
int num;LL ans=,tp;
for(int i=;i<(<<cnt);++i){//用二进制来表示每个质因子是否被使用,即有2^cnt-1种可能,此时cnt较小,题目中1e9最多也就8个素因子,二进制优化
tp=,num=;
for(int j=;j<cnt;++j)
if(i&(<<j))num++,tp*=prime[j];//表示选择哪几个素因子
if(num&)ans+=x/tp;//奇加
else ans-=x/tp;//偶减
}
return x-ans;
}
int main(){
while(~scanf("%d",&t)){
for(int i=;i<=t;++i){
scanf("%lld%lld%lld",&a,&b,&n);cnt=;
for(LL j=;j*j<=n;++j){//求出n内的所有质因子
if(n%j==){
prime[cnt++]=j;
while(n%j==)n/=j;
}
}
if(n>)prime[cnt++]=n;
printf("Case #%d: %lld\n",i,solve(b)-solve(a-));//区间差
}
}
return ;
}

题解报告:hdu 4135 Co-prime(容斥定理入门)的更多相关文章

  1. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

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

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

  3. HDU 4135 Co-prime(容斥+数论)

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. BZOJ2839 : 集合计数 (广义容斥定理)

    题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...

  5. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  7. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. hdu 6053 trick gcd 容斥

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

  9. HDU 2841 Visible Trees(容斥定理)

    Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. eclipse无法启动问题记录

    几天没开eclipse,居然报错“can not unload……”,搜索答案发现没有准确的,遵从了一个多人顶赞的办法重下eclipse,把配置文件拷贝一份,结果悲剧了,虽然能够打开了,但我之前配置的 ...

  2. Array types are now written with the brackets around the element type问题的解决方法

    在xcode6.1中来编写swift空数组时.出现的的这个问题,依照官方 Swift 教程<The Swift Programming Language>来写 let emptyArray ...

  3. hadoop的一般端口使用

  4. hadoop-client

    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common --> <dependency> ...

  5. CSS3 (一)

    属性选择器 1. E[attr^="value"]:指定了属性名,并且有属性值,属性值是以value开头的: .wrap a[href^="http://"]{ ...

  6. js中字符串函数indexOf与search的区别

    IndexOf()方法是用来判断一个字符串是否存在于一个更长的字符串中.从长字符串左端到右端来搜索,如果存在该子字符串就返回它所处的位置(即索引).如果在被搜索的字符串没有找到要查找的字符串返回-1. ...

  7. 【bzoj3282】Tree

    LCT模板题: 话说xor和的意思是所有数xor一下: #include<iostream> #include<cstdio> #include<cstring> ...

  8. docker pure-ftp 搭建ftp服务器

    参考:https://hub.docker.com/r/stilliard/pure-ftpd/ docker-compose.yml: ftp: image: stilliard/pure-ftpd ...

  9. YTU 2392: 求各位数字之和

    2392: 求各位数字之和 时间限制: 1 Sec  内存限制: 128 MB 提交: 1253  解决: 292 题目描述 编写一个程序,计算任意输入的正整数的各位数字之和.(输入的位数不要超过10 ...

  10. MYSQL初级学习笔记九:MySQL索引的使用!(视频序号:初级_51)

    知识点十一:索引的使用(51) 什么是索引: 索引的定义: 在关系型数据库中,索引是一种与表有关的数据库结构,它可以使对应于表的SQL语句执行的更快.索引的作用相当于图书的目录,可以 根据目录中的页码 ...