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.

InputThe 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 <= 10 15) and (1 <=N <= 10 9).OutputFor 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}.
题目大意:给你3个数A,B,C,让你求出从A到B的所有数字中与C互质的个数,
题解:直接求互质不好求,我们就求与C不互质的个数,然后最后在减去就可以了
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1E5+;
ll arr[N];
vector<ll >ve;
int main(){
int t,k=;
scanf("%d",&t);
while(t--){
k++;
ll a,b,c,pos=;
scanf("%lld%lld%lld",&a,&b,&c);
for(int i=;i*i<=c;i++){
if(c%i==){
pos++;
ve.push_back(i);
while(c%i==) c/=i;
}
}
if(c>)
{
pos++;
ve.push_back(c);
}
ll sa=,sb=;
for(ll i=;i<(<<pos);i++){
ll sum=,cnt=;
for(ll j=;j<pos;j++){
if(&(i>>j)){
sum*=ve[j];
cnt++;
}
}
if(cnt&){//容斥里的奇减偶加
sa+=(a-)/sum;//a-1前有多少个数字是sum的倍数,
sb+=(b)/sum;
}
else {
sa-=(a-)/sum;
sb-=(b)/sum;
}
}
sb=sb-sa;//应题目要求 从A到B
printf("Case #%d: %lld\n",k,b-a+-sb);
ve.clear();
}
return ;
}

最新文章

  1. ES6 语法笔记
  2. Mallet 使用说明
  3. Android应用反破解的思路
  4. hashtable用法
  5. navigationController Pop回指定页面
  6. Django内置template标签
  7. DFS 分布式文件系统 选型笔记
  8. 定时任务框架APScheduler学习详解
  9. FusionCharts重写单系列图
  10. TCP 详解
  11. Vim/Vi常用操作(第二版)
  12. RHEL6非交互式工具sshpass和expect安装
  13. 配置Tomcat时遇到的问题
  14. ES6 使用数据类型Set求交集、并集、差集
  15. [转帖]浏览器的F5和Ctrl+F5
  16. Gym 102028C - Supreme Command - [思维题][2018-2019 ACM-ICPC Asia Jiaozuo Regional Contest Problem C]
  17. 关于git中自己的分支和主分支有冲突的解决方案(git和乌龟git)
  18. 【Orleans开胃菜系列1】不要被表象迷惑
  19. UVa 1374 快速幂计算(dfs+IDA*)
  20. 13 款最棒的 jQuery 图像 360&#176; 旋转插件

热门文章

  1. 【数据结构和算法】001 单链表 LinkedList
  2. 单元测试实践思考(junit5+jmockit+testcontainer)
  3. layer弹层插件
  4. 使用TensorFlow v2库实现线性回归
  5. java新时间类
  6. ArcGIS Server的安装
  7. MySQL的MVCC机制
  8. Django ORM查询结果是model对象
  9. opnet
  10. 20175314 实验五 Java网络编程