经典容斥入门题?
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL solve(LL num, LL n)
{
vector<LL> p;
LL temp = n;
for(LL i = 2; i <= temp/i; ++i)
{
if(temp%i == 0)
{
p.push_back(i);
while(temp%i == 0) temp /= i;
}
}
if(temp > 1) p.push_back(temp);
LL res = 0;
for(int i = 1; i < (1<<p.size()); ++i)
{
LL mult = 1;
int cnt = 0;
for(int j = 0; j < p.size(); ++j)
{
if(i&(1<<j))
{
mult *= p[j];
++cnt;
}
}
if(cnt&1) res += num/mult;
else res -= num/mult;
}
return num-res;
}
int main()
{
ios::sync_with_stdio(false);
int T;
LL A,B,N;
int time = 0;
cin >> T;
while(T--)
{
cin >> A >> B >> N;
cout << "Case #" << ++time << ": ";
cout << solve(B,N)-solve(A-1,N) << endl;
}
return 0;
}