HDU _4135 Co_prime
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.
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).
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.
Input
2
1 10 2
3 15 5
Output
Case #1: 5
Case #2: 10
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include <bits/stdc++.h>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#define maxn 1001000
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MOD 1000000007
#define ll long long
using namespace std;
ll Q[maxn],fac[maxn];
ll num;
void Divide(ll n)
{
num=0;
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
while(n%i==0)
n/=i;
fac[num++]=i;
}
}
if(n!=1) fac[num++]=n;
}
ll solve(ll n)
{
ll k,t,ans;
t=ans=0;
Q[t++]=-1;
for(ll i=0;i<num;i++)
{
// cout<<"num "<<num<<endl;
k=t;
for(ll j=0;j<k;j++)
{
Q[t++]=-1*Q[j]*fac[i];
// printf("fac[%lld]->%lld -- Q[%lld] -> %lld\n",i,fac[i],t-1,Q[t-1]);
}
}
for(ll i=1;i<t;i++)
{
ans+=n/Q[i];
}
return ans;
}
int main()
{
int T,kase=1;
scanf("%d",&T);
while(T--)
{
ll a,b,n;
scanf("%lld%lld%lld",&a,&b,&n);
Divide(n);
ll ans=b-solve(b)-(a-1-solve(a-1));
printf("Case #%d: %lld\n",kase++,ans);
}
return 0;
}