Co-prime HDU
来源:互联网 发布:李银桥和权延赤 知乎 编辑:程序博客网 时间:2024/06/05 11:05
HDU _4135 Co_prime
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.
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 <= 10 9).
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.
Input
2
1 10 2
3 15 5
Output
Case #1: 5
Case #2: 10
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
#pragma comment(linker, "/STACK:1024000000,1024000000")//#include <bits/stdc++.h>#include<string>#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<queue>#include<stack>#include<vector>#include<algorithm>#define maxn 1001000#define INF 0x3f3f3f3f#define eps 1e-8#define MOD 1000000007#define ll long longusing namespace std;ll Q[maxn],fac[maxn];ll num;void Divide(ll n){ num=0; for(ll i=2;i*i<=n;i++) { if(n%i==0) { while(n%i==0) n/=i; fac[num++]=i; } } if(n!=1) fac[num++]=n;}ll solve(ll n){ ll k,t,ans; t=ans=0; Q[t++]=-1; for(ll i=0;i<num;i++) { // cout<<"num "<<num<<endl; k=t; for(ll j=0;j<k;j++) { Q[t++]=-1*Q[j]*fac[i]; // printf("fac[%lld]->%lld -- Q[%lld] -> %lld\n",i,fac[i],t-1,Q[t-1]); } } for(ll i=1;i<t;i++) { ans+=n/Q[i]; } return ans;}int main(){ int T,kase=1; scanf("%d",&T); while(T--) { ll a,b,n; scanf("%lld%lld%lld",&a,&b,&n); Divide(n); ll ans=b-solve(b)-(a-1-solve(a-1)); printf("Case #%d: %lld\n",kase++,ans); } return 0;}
- hdu 4135 co-prime
- HDU 4135 Co-prime
- HDU - 4135 Co-prime
- HDU 4135 Co-prime
- hdu 4135 Co-prime
- HDU 4135 Co-prime
- 【HDU】 4135 Co-prime
- HDU - 4135 Co-prime
- HDU 4135 Co-prime
- HDU-4135-Co-prime
- HDU 4135 Co-prime
- 【hdu 4135】Co-prime
- HDU 4135 Co-prime
- 【HDU-4135】Co-prime
- Co-prime HDU
- HDU 4135——Co-prime
- [容斥原理] hdu 4135 Co-prime
- 【HDU】4135 Co-prime 容斥原理
- SDWebImage支持webp
- Shell教程
- vue使用 better-scroll的参数和方法
- Centos下 Nginx安装与配置
- UltraEdit的使用
- Co-prime HDU
- Python selenium —— selenium与自动化测试成神之路
- JS中的函数、BOM和DOM操作
- Java求101--200之间有多少个素数(质数)
- ios UIView...可变的圆角和边框,某个角,多个角,设置圆角和边框
- Linux管理输入输出 管道 tee
- POJ
- JQueryUI
- Kotlin基础(一)——基本类型