Co-primetime limit:1000msmemory limit:32768kbthis problem'll be judged onHDU. Original id:4135
64-bit integer IO format: %i64d Java class name: Main Given A number N, you is asked to count the number of integers between a and B inclusive which is relatively 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 in input contains T (0 < T <=) 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).OutputFor each test case, print the number of integers between A and B inclusive which is relatively prime to N. follow the Out Put format below.Sample Input
21 10 23) 15 5
Sample Output
Case #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}.SourceThe third Lebanese collegiate programming Contest problem Solving: tolerant + number theory
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4Vector<ll>G;5 ll solve (ll X,ll y) {6 g.clear ();7 for(LL i =2; I*i <= y; ++i) {8 if(y%i = =0) {9 while(y%i = =0) Y/=i;Ten for(intj = g.size ()-1; J >=0; --j) One if(ABS (I*g[j] <= x)) G.push_back (-i*g[j]); A G.push_back (i); - } - } the if(Y >1) { - for(inti = g.size ()-1; I >=0; --i) - if(ABS (Y*g[i]) <= x) G.push_back (-y*g[i]); - g.push_back (y); + } -LL ret =0; + for(inti = g.size ()-1; I >=0; I.) RET + = x/G[i]; A returnX-ret; at } - intMain () { - intT,cs =1; - LL a,b,n; -scanf"%d",&t); - while(t--){ inscanf"%i64d%i64d%i64d",&a,&b,&n); -printf"Case #%d:%i64d\n", Cs++,solve (B,n)-Solve (A-1, N)); to } + return 0; -}
View Code
HDU 4135 co-prime