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.
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 <= 10
15
) and (1 <=N <= 10
9
).
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
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
Idea: The number of numbers that are relatively prime to n in the interval [a,b] can be transformed into finding the number of numbers that are relatively prime to n in [1,b] and the number that is relatively prime to n in [1,a] The number of numbers can be subtracted at the end.
The number of numbers that are coprime to n in [1,b] can be transformed into the number of numbers that are not coprime to n in [1,b], the total number minus it is the number of coprime .
To ask for the number of numbers in [1,b] that are not coprime to n, we must first know what kind of number is not coprime to n: it must be a multiple of a prime factor of n.
So first decompose n into prime factors to get several prime factors of n {fac[1], fac[2],..}, and then find out how many multiples of fac[1] in [1,b], how many The multiples of fac[2], ..., and then according to the principle of inclusion and exclusion, we can get how many numbers in [1,b] are multiples of a number in {fac[1],fac[2],..}.
The principle of inclusion and exclusion: Assuming that n has three prime factors {fac[1], fac[2], fac[3]}, then [1,b] is {fac[1], fac[2], fac[3] The number of multiples of a number in } is f(b)=n/fac[1]+n/fac[2]+n/fac[3]-n/(fac[1]*fac[2])-n /(fac[1]*fac[3])-n/(fac[2]*fac[3])+n/(fac[1]*fac[2]*fac[3]) (odd and even reduce)
AC code:
#include <iostream> #include<cstdio> #include<cstring> #define ll long long using namespace std; ll factor[1000010]; ll que[1000010]; ll cnt; void get_factor(ll n){ // Decompose prime factors (simulate short division) memset(factor, 0 , sizeof (factor)); cnt=0; for(ll i=2;i*i<=n;i++){ if(n%i==0){ factor[++cnt]=i; while(n%i==0) n/=i; } } if(n>1) factor[++cnt]=n; } ll fun(ll n){ // Use an array to calculate the formula memset(que, 0 , sizeof (que)); ll k=0; for(ll i=1;i<=cnt;i++){ that[ ++k]= factor[i]; ll tmp=k; for(int j=1;j<=tmp-1;j++) que[++k]=que[tmp]*que[j]*(-1); } ll ret=0; for(ll i=1;i<=k;i++) ret+=n/que[i]; return ret; } intmain () { ll t; scanf("%lld",&t); ll a,b,n; for(ll i=1;i<=t;i++){ scanf("%lld%lld%lld",&a,&b,&n); get_factor(n); printf("Case #%lld: %lld\n",i,b-fun(b)-(a-1-fun(a-1))); } return 0; }