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