题目大意:求在[A,B]区间中,与n互质的的数的个数。( 1≤A≤B≤1015 1≤n≤108 )
简要题解:
设
f(x)
表示
1
~
AC code:
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cctype>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int, int> pii;
inline LL read()
{
LL x = 0; int v = 1, c = getchar();
for(; c < '0' || c > '9'; c = getchar()) if(c == '-') v = -1;
for(;c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
return x * v;
}
template<typename T>
inline void write(T x, int ch = 10)
{
int s[20], t = 0;
if(x < 0) { x = -x; putchar('-'); }
do s[t++] = x % 10; while(x /= 10);
while(t--) putchar('0' + s[t]);
putchar(ch);
}
const int delta[] = {-1, 1};
int n;
LL A, B;
vector<int> p;
void decompose(int x)
{
p.clear();
int m = sqrt(x + 0.5);
for(int i = 2; i <= m; ++i)
{
if(x % i == 0)
{
p.PB(i);
while(x % i == 0)
{
x /= i;
}
}
}
if(x > 1) p.PB(x);
}
LL solve(LL x)
{
LL ans = 0;
for(int i = 1; i < (1 << p.size()); ++i)
{
int res = 1, cnt = 0;
for(int j = 0; j < p.size(); ++j)
{
if(i & (1 << j))
{
res *= p[j];
cnt++;
}
}
ans += delta[cnt&1] * x / res;
}
return x - ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("hdu4135.in", "r", stdin);
freopen("hdu4135.out", "w", stdout);
#endif
int T = read(), kase = 0;
while(T--)
{
A = read(); B = read(); n = read(); decompose(n);
printf("Case #%d: ", ++kase); write(solve(B) - solve(A-1));
}
#ifndef ONLINE_JUDGE
fprintf(stderr, "Run Time: %dms\n", clock());
fclose(stdin), fclose(stdout);
#endif
return 0;
}