题目求[A,B]区间内与N互质数的个数。

  • 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]。
  • 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1,X]区间内与N不互质数的个数。
  • 于是就可以先处理出N的所有质因数{p0,p1,p2,...,pn}。
  • 而[1,X]能被pi整除的数有$\lfloor \frac X{p_i} \rfloor$个,再利用容斥原理除掉质因数公倍数重复计数的部分就能求出不互质个数。
  • 最后X减去不互质个数就是互质个数了。
 #include<cstdio>
#include<cstring>
using namespace std;
int prime[],pn;
long long calc(long long n){
long long res=;
for(int i=; i<(<<pn); ++i){
int tmp=,cnt=;
for(int j=; j<pn; ++j){
if(((i>>j)&)==) continue;
++cnt;
tmp*=prime[j];
}
if(cnt&) res+=n/tmp;
else res-=n/tmp;
}
return n-res;
}
int main(){
long long a,b;
int t,n;
scanf("%d",&t);
for(int cse=; cse<=t; ++cse){
scanf("%lld%lld%d",&a,&b,&n);
pn=;
for(int i=; i*i<=n; ++i){
if(n%i) continue;
while(n%i==) n/=i;
prime[pn++]=i;
}
if(n!=) prime[pn++]=n;
printf("Case #%d: %lld\n",cse,calc(b)-calc(a-));
}
return ;
}

HDU4135 Co-prime(容斥原理)的更多相关文章

  1. hdu4135 Co-prime【容斥原理】

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

  2. [HDU4135]CO Prime(容斥)

    也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...

  3. hdu4135 Co-prime 容斥原理

    Given a number N, you are asked to count the number of integers between A and B inclusive which are ...

  4. hdu4135容斥原理 组合遍历

    容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...

  5. HDU4135(容斥原理)

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

  6. 容斥原理的(二进制思想和质因子分解+模板)hdu4135+ecf81.D

    题:http://acm.hdu.edu.cn/showproblem.php?pid=4135 题意:求[A,B]与N互质的数的个数 #include<iostream> #includ ...

  7. Codeforces1036F Relatively Prime Powers 【容斥原理】

    题目分析: 这种题目标题写莫比乌斯反演会不会显得太恐怖了,那就容斥算了. gcd不为1的肯定可以开根.所以把根式结果算出来就行了. 辣鸡题目卡我精度. 代码: #include<bits/std ...

  8. HDU4135容斥原理

    #include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...

  9. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

随机推荐

  1. Intent的Flag

    小段代码: Intent it=new Intent(); it.setClass(Setting.this,Main.class);//从Setting跳转到Main it.addFlags(Int ...

  2. 5款Linux最佳照片管理软件

    在谈到 Linux 中的应用程序时,对于不同的用户.不同的使用场景以及不同的使用习惯,在同一类应用当中,总会有多种软件可供选择和备选.就 Linux 中的照片管理软件来说,相信此前的 Google P ...

  3. IOS 页面之间的跳转

    1.UINavigationController popToViewController 对应popViewControllerAnimated: 也可以使用: [self.navigationCon ...

  4. Apache Common DbUtils

    前段时间使用了Apache Common DbUtils这个工具,在此留个印,以备不时查看.大家都知道现在市面上的数据库访问层的框架很多,当然很多都是包含了OR-Mapping工作步骤的 例如大家常用 ...

  5. 基于DCMTK的DICOM相关程序编写攻略

    2008年09月10日 星期三 15:35 基于DCMTK的DICOM相关程序编写攻略 前言: 由于现在的医学影像设备的图像存储和传输正在逐渐向DICOM标准靠拢,在我们进行医学图像处理的过程中,经常 ...

  6. poj1094

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29539   Accepted: 10 ...

  7. Linux awk命令详解??????????(研究)

    http://blog.chinaunix.net/uid-25120309-id-3801250.html 一.  AWK 说明  awk是一种编程语言,用于在linux/unix下对文本和数据进行 ...

  8. 关于ubuntu配置静态IP 无法正常上网的解决方案

    在ubuntu中配置静态IP后无法正常上网. 解决: 1.在终端执行 vim /etc/network/interfaces 在文件中加入如下内容,网关要写上,我开始一直无法上网就是因为没有配置网关 ...

  9. 【SpringMVC】SpringMVC系列11之Restful的CRUD

      11.Restful的CRUD 11.1.需求 11.2.POST转化为PUT.DELETE的fileter 11.3.查询所有 11.4.添加 11.5.删除     优雅的 REST 风格的资 ...

  10. Eclipse 一直提示 loading descriptor for 的解决方法(转)

    启动eclipse之后,进行相关操作时,弹出界面,提示:loading descriptor for xxx 解决方法: 在Eclipse左侧的Project Explorer 最右上角有一个小钮,鼠 ...