Co-prime

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2786    Accepted Submission(s): 1072

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

 

题解:模版题,刚开始开成int了;

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
typedef long long LL;
vector<int>p;
void getp(LL x){
p.clear();
for(int i=2;i*i<=x;i++){
if(x%i==0){
p.push_back(i);
while(x%i==0)x/=i;
}
}
if(x>1)p.push_back(x);
}
LL tc(LL x){
LL sum=0;
for(int i=1;i<(1<<p.size());i++){
LL num=0,cur=1;
for(int j=0;j<p.size();j++){
if(i&(1<<j)){
num++;
cur*=p[j];
}
}
if(num&1)sum+=x/cur;
else sum-=x/cur;
}
return x-sum;
}
int main(){
LL T,A,B,K,flot=0;
scanf("%lld",&T);
while(T--){
scanf("%lld%lld%lld",&A,&B,&K);
getp(K);
printf("Case #%lld: %lld\n",++flot,tc(B)-tc(A-1));
}
return 0;
}

  

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

  1. [HDU4135]CO Prime(容斥)

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

  2. POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)

      题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...

  3. HDU 4059 容斥初步练习

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  4. XTU 1242 Yada Number 容斥

    Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime ...

  5. hdu1695:数论+容斥

    题目大意: 求x属于[1,b]和 y属于[1,d]的 gcd(x,y)=k 的方案数 题解: 观察发现 gcd()=k 不好处理,想到将x=x/k,y=y/k 后 gcd(x,y)=1.. 即问题转化 ...

  6. hdu5072(鞍山regional problem C):容斥,同色三角形模型

    现场过的第四多的题..当时没什么想法,回来学了下容斥,又听学长讲了一讲,终于把它过了 题目大意:给定n个数,求全部互质或者全部不互质的三元组的个数 先说一下同色三角形模型 n个点 每两个点连一条边(可 ...

  7. HUST 1569(Burnside定理+容斥+数位dp+矩阵快速幂)

    传送门:Gift 题意:由n(n<=1e9)个珍珠构成的项链,珍珠包含幸运数字(有且仅由4或7组成),取区间[L,R]内的数字,相邻的数字不能相同,且旋转得到的相同的数列为一种,为最终能构成多少 ...

  8. Codeforces 803F Coprime Subsequences (容斥)

    Link:http://codeforces.com/contest/803/problem/F 题意:给n个数字,求有多少个GCD为1的子序列. 题解:容斥!比赛时能写出来真是炒鸡开森啊! num[ ...

  9. YYHS-分数(二分+容斥)

    题目描述 KJDH是个十分善于探索的孩子,有一天他把分子分母小于等于n的最简分数列在了纸上,他想找到这些分数里第k小的数,这对于KJDH来说当然是非常轻易,但是KJDH最近多了很多妹子,他还要去找妹子 ...

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

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

随机推荐

  1. C++ signal的使用

    1.头文件 #include  <signal.h> 2.功能 设置某一信号的对应动作 3.函数原型 typdef  void  (*sighandler_t )(int); sighan ...

  2. linux 虚拟文件系统----------Virtual File System VFSkky

    在了解虚拟文件系统之前,必须先知道什么是 Kernal  Space 与 User  Space. Kernal Space 与User Space 的差别,在于内存使用上安全机制的差异. kerna ...

  3. js中this指向问题

    1.在全局范围内使用this的时候  它指向的是全局对象 this Window {top: Window, window: Window, location: Location, external: ...

  4. SQL(Oracle)日常使用与不常使用函数的汇总

    --日常使用的sql语句和oracle语句,有些相对使用的频率比较高,收藏起来还是比较值得的 -- 绝对值 SQL:) value Oracle:) value from dual -- 2.取整(大 ...

  5. Robolectric 探索之路

    layout: post title: Roboletric探索之路,从抗拒到依赖 description: Roboletric Android Unit Testing category: blo ...

  6. Html中截切文章内容,造成标签不全的问题

    把标签全部进行替换 ) { string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+ ...

  7. 技术是容易学会的(Copy)

    1)自信第一.自信这东西很是奇怪,人有可能一开始会莫明其妙的自信,但一切都从这开始的.你觉得自己行你就一定会变得行,反之,如果觉得自已不行,那一定会不行.信心是我们转动飞轮的第一步,有了这一步我们的能 ...

  8. php实现简单的上一页下一页

    思路整理: 现在好多人用id的增1和减1实现上一篇和下一篇但是难道文章ID不会断了吗所以你要知道上个ID和个ID是多少就OK了那怎么解决这个问题呢,很简单例子:假如这篇文章的ID200 <a h ...

  9. Oracle Primavera P6 R84单机版安装教程

    本教程用于指导Oracle Primavera P6 R84版本的单机版安装过程.P6 R84版本于2014年9月30日发布.其功能较之前版本有较大提升.单机版的安装方式也发生了很大的变化.P6 R8 ...

  10. 用javascirpt画个太极

    偶然看到用代码画太极,感觉很有趣,用JS写了一个 过程很简单,画了张图,应该一看就懂了 代码也很简单,如下,注释很多 function TaiJi(r,canvas){ this.r = r; thi ...