Co-PrimeTime
limit:MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64u SubmitStatusAppoint Description:System Crawler (2015-01-07)
Description
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.
Input
The 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 <=) and (1 <=n <= 10 9).
Output
For 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
Sample Output
Hint
In the first test case, the five integers in range [1,10] which is relatively prime to 2 is {1,3,5,7,9}.
Calculate the number of coprime in a to B, and N
Statistics 1 to A-1, and 1 to B and N coprime number, in subtraction, 1 to M and N coprime number, first 1 to M and n not coprime number.
To find the number of mass after n decomposition, with a binary number to denote the selection and non-selection of the first prime, to get the number contained in M, the election of odd number of prime numbers is, the results of the statistical summation, for even number, minus.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define LL __int64int PRIM[1000000], vis[1000000], cnt, void sieve () {memset (vis,0,sizeof (VIS)); CNT = 0; LL I, J; for (i = 2; I <= 1000000; i++) {if (!vis[i]) {prim[cnt++] = i; for (j = i*i; J < 1000000; J + = i) vis[j] = 1; }}}ll p[1000], p_num; ll F (ll N,ll m) {ll k = n, temp, ans = 0; int I, j, num; for (i = 0, p_num = 0; i < cnt; i++) {if (k% prim[i] = = 0) {p[p_num++] = Prim[i]; } while (k% prim[i] = = 0) {k/= prim[i]; } if (k = = 1) break; } if (K > 1) p[p_num++] = k; for (i = 1; i < (1<<p_num); i++) {for (j = 0, num = 0, temp = 1; j < P_num; J + +) { if ((1<<j) & i) {temp *= p[j]; num++; }} if (num% 2) ans + = m/temp; else ans-= m/temp; } return ans; int main () {LL T, TT, A, b, N; Sieve (); scanf ("%i64d", &t); for (TT = 1; TT <= t; tt++) {scanf ("%i64d%i64d%i64d", &a, &b, &n); printf ("Case #%i64d:%i64d\n", TT, (B-f (N,b))-(A-1-f (N,a-1))); } return 0;}
Hdu4135--co-prime (Euler function + repulsion principle)