题意
求a~b之间与n互素的个数
思路
求出 1~b 之间与n互素的个数减去 1~a-1之间与n互素的的个数即为所求。
#include<cstdio>
#include<queue>
#include<set>
#include<cstdlib>
#include<string.h>
#include<string>
#include<iostream>
#include<cmath>
#include<unordered_map>
#include<map>
#include<algorithm>
#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ll long long int
#define ull unsigned long long int
#define mt(a,b) memset(a, b, sizeof a)
//#define int long long
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
using namespace std;
const int N = 1e5 + 7, M = 1e6 + 10;
ll fac[N], m;
//对n分解质因数,质因数个数为m
void div(ll x)
{
m = 0;
int q = sqrt(x);
for (int i = 2; i <= q; i++)
if (x % i == 0)
{
fac[++m] = i;
while (x % i == 0) x /= i;
}
if (x > 1) fac[++m] = x;
}
//应用容斥原理求出1~b之间含有n的质因子的数的个数,也即与n不互素的数的个数
ll co_prime(ll n)
{
ll ans = 0;
for (ll i = 0; i < 1ll << m; i++)//i == 0的时候,t = 1, ans = n,也即是b可以含有任何质因子
{
int k = 0;
ll t = 1;
for (int j = 0; j < m; j++)
if (i & 1ll << j) t *= fac[j + 1], k++;
if (k & 1) ans -= n / t;
else ans += n / t;
}
return ans;
}
int main()
{
IOS;
int cas = 1;
int T; cin >> T;
while (T--)
{
ll a, b, n; cin >> a >> b >> n;
div(n);
cout << "Case #" << cas++ << ": ";
cout << co_prime(b) - co_prime(a - 1) << endl;
}
return 0;
}