求 [A,B] 内与 n 互质的数的个数。
n≤109 ,所以对 n 的质因子枚举子集,容斥原理计算。
因为
所以
n
的不同质因子最多
时间复杂度:
O(n−−√+2p)
(
p
为
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>
template<class Num>void read(Num &x)
{
char c; int flag = 1;
while((c = getchar()) < '0' || c > '9')
if(c == '-') flag *= -1;
x = c - '0';
while((c = getchar()) >= '0' && c <= '9')
x = (x<<3) + (x<<1) + (c-'0');
x *= flag;
return;
}
template<class Num>void write(Num x)
{
if(x < 0) putchar('-'), x = -x;
static char s[20];int sl = 0;
while(x) s[sl++] = x%10 + '0',x /= 10;
if(!sl) {putchar('0');return;}
while(sl) putchar(s[--sl]);
}
#define REP(__i,__start,__end) for(int __i = (__start); __i <= (__end); __i++)
const int size = 31624, add[2] = {-1, 1};
const double eps = 1e-4;
int n;
long long A, B, ans;
int prime[size], tot;
void prework()
{
int sz = sqrt(n) + eps, tp = n;
tot = 0;
REP(i, 2, sz)
{
if(!(tp % i))
{
while(!(tp % i)) tp /= i;
prime[tot++] = i;
sz = std::min(sz, tp);
}
}
if(tp != 1) prime[tot ++] = tp;
}
long long solve(long long S)
{
long long ret = S;
REP(i, 0, (1<<tot) - 1)
{
int poi = 1, cnt = 0;
REP(j, 0, tot - 1)
if((i >> j)&1)
{
poi *= prime[j];
cnt++;
}
ret += (S/poi) * add[cnt&1];
}
return S - ret;
}
int main()
{
int T;
#ifndef ONLINE_JUDGE
freopen("4135.in","r",stdin);
freopen("4135.out","w",stdout);
#endif
read(T);
REP(CaseNum, 1, T)
{
printf("Case #%d: ", CaseNum);
read(A), read(B), read(n);
prework();
ans = solve(B) - solve(A - 1);
write(ans), puts("");
}
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}