hdu 4135 A-B中,与N不互质的数
2017-08-07 20:42
169 查看
传送门
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5403 Accepted Submission(s): 2151
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
HintIn the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
Source
The Third Lebanese Collegiate Programming Contest
Recommend
lcy | We have carefully selected several similar problems for you: 1796 1434 3460 1502 4136
求A-B中,与N不互质的数。也就是说求1-B中与N互质的数。再求1-(A-1)中与N互质的数,用B-(A-1)-互质的数=不互质的数。但有一个容斥在里面,因为N可以分解,
p1^a1*p2^a2*p3^a3````。奇加偶减。
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5403 Accepted Submission(s): 2151
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
HintIn the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
Source
The Third Lebanese Collegiate Programming Contest
Recommend
lcy | We have carefully selected several similar problems for you: 1796 1434 3460 1502 4136
求A-B中,与N不互质的数。也就是说求1-B中与N互质的数。再求1-(A-1)中与N互质的数,用B-(A-1)-互质的数=不互质的数。但有一个容斥在里面,因为N可以分解,
p1^a1*p2^a2*p3^a3````。奇加偶减。
//china no.1 #pragma comment(linker, "/STACK:1024000000,1024000000") #include <vector> #include <iostream> #include <string> #include <map> #include <stack> #include <cstring> #include <queue> #include <list> #include <stdio.h> #include <set> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <cctype> #include <sstream> #include <functional> #include <stdlib.h> #include <time.h> #include <bitset> using namespace std; #define pi acos(-1) #define endl '\n' #define srand() srand(time(0)); #define me(x,y) memset(x,y,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); cin.tie(0); #define FOR(x,n,i) for(int i=x;i<=n;i++) #define FOr(x,n,i) for(int i=x;i<n;i++) #define W while #define sgn(x) ((x) < 0 ? -1 : (x) > 0) #define bug printf("***********\n"); typedef long long LL; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; const int dx[]={-1,0,1,0,1,-1,-1,1}; const int dy[]={0,1,0,-1,-1,1,-1,1}; const int maxn=1e4+10; const int maxx=1e6+100; const double EPS=1e-7; const int MOD=10000007; #define mod(x) ((x)%MOD); template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} inline LL Scan() { LL Res=0,ch,Flag=0; if((ch=getchar())=='-')Flag=1; else if(ch>='0' && ch<='9')Res=ch-'0'; while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0'; return Flag ? -Res : Res; } //freopen( "in.txt" , "r" , stdin ); //freopen( "data.out" , "w" , stdout ); //cerr << "run time is " << clock() << endl; int tot=0,cnt=0,vis[maxx]; LL pr[maxx],a[maxx/100]; LL A,B; int N; void init() { tot=0; int len = sqrt(maxx) + 1; for (int i = 2; i <= len; ++i)if (!vis[i]) { pr[tot++]=i; for (int j = i*i; j <= maxx; j += i)vis[j] = 1; } } void pri(LL x) { cnt=0; for(int i=0;i<tot&&pr[i]*pr[i]<=x;i++) { if(x%pr[i]==0) { a[cnt++]=pr[i]; while(x%pr[i]==0) x/=pr[i]; } } if(x>1) a[cnt++]=x; } LL solve(LL x) { LL st=(1<<cnt); LL ret=0; for(LL i=1;i<st;i++) { LL t=i,k=0,tmp=1; for(LL j=0;j<cnt;j++) { //cout<<t<<endl; if(t&1) { 4000 k++; tmp*=a[j]; } t>>=1; } if(k&1) ret+=x/tmp; else ret-=x/tmp; } return ret; } int main() { init(); int t; t=Scan(); for(int cas=1;cas<=t;cas++) { A=Scan();B=Scan();N=Scan(); pri(N); LL ans=B-solve(B)-A+1+solve(A-1); printf("Case #%d: %lld\n",cas,ans); } }
相关文章推荐
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 A-B中,与N不互质的数
- HDU 4135(Co-prime-[1,m]内与n互质的数的个数)
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 容斥原理 求(1,m)区间与n互质的数的个数. 4000
- HDU 4135-Co-prime(容斥求区间内与N互质的个数(队列||位运算))
- HDU 4135 Co-prime 区间内与n互质的个数 容斥(入门
- HDU 4135 统计[a,b]这个区间中和n互质的数的个数 (容斥+数组队列) 好题
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 A-B中,与N不互质的数
- hdu 1695 hdu 4135 容斥原理求1-n 区间内与r互质的个数
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 A-B中,与N不互质的数
- HDU 4135:Co-prime 容斥原理求(1,m)中与n互质的数的个数
- hdu 4135 A-B中,与N不互质的数
- hdu 4135 容斥原理