Co-Prime
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2371 Accepted Submission (s): 887
Problem Descriptiongiven A number N, you is asked to count the number of integers between a and B inclusive which is rel Atively Prime to N.
Integers is said to be co-prime or relatively prime if they has no common positive divisors other than 1 or, equival Ently, 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 <=) the number of test cases, each of the next T lines contains t Hree integers A, B, N where (1 <= A <= B <= 1015) and (1 <=n <= 109).
Outputfor each test case, print the number of integers between A and B inclusive which is relatively prime to N. Follow T He output format below.
Sample Input21 10 23 15 5
Sample outputcase #1:5Case #2:10
HintIn the first test case, the five integers in range [1,10] which is relatively prime to 2 is {1,3,5,7,9}.
The principle of tolerance and repulsion ...
/************************************************author:P k28created time:2015/8/18 9:29:57File NAME:4.CP p*************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<iomanip>#include<list>#include<deque>#include<stack>#defineull unsigned long Long#definell Long Long#defineMoD 90001#defineINF 0x3f3f3f3f#defineMAXN 10000+10#defineCLE (a) memset (A,0,sizeof (a))Constull inf = 1LL << A;Const Doubleeps=1e-5;using namespacestd;BOOLcmpintAintb) { returnA>b;} ll A,b,n;ll solve () {ll sum=0; Vector<ll>v; for(intI=2; i*i<=n;i++) if(n%i==0) {v.push_back (i); while(n%i==0) n/=i; } if(n>1) V.push_back (n); for(LL st=1;st< (1<< (V.size ())); ++st) {//0 1ll bits=0, mult=1; for(intI=0;i< (int) v.size (); + +i) { if(st& (1<<i)) { ++bits; Mult*=V[i]; }} ll cur=b/mult-a/mult; if(a%mult==0) cur++; if(bits&1) sum+=cur; Elsesum-=cur; } returnb-a-sum+1;}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif //freopen ("OUT.txt", "w", stdout); intT; CIN>>T; for(intI=1; i<=t;i++) {scanf ("%i64d%i64d%i64d",&a,&b,&N); printf ("Case #%d:%i64d\n", I,solve ()); } return 0;}
HDU 4135 co-prime