[Educational Codeforces Round 81 (Rated for Div. 2)] D. Same GCDs (数论,因子分解,容斥定理)
D. Same GCDs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two integers aa and mm. Calculate the number of integers xx such that 0≤x<m0≤x<m and gcd(a,m)=gcd(a+x,m)gcd(a,m)=gcd(a+x,m).
Note: gcd(a,b)gcd(a,b) is the greatest common divisor of aa and bb.
Input
The first line contains the single integer TT (1≤T≤501≤T≤50) — the number of test cases.
Next TT lines contain test cases — one per line. Each line contains two integers aa and mm (1≤a<m≤10101≤a<m≤1010).
Output
Print TT integers — one per test case. For each test case print the number of appropriate xx-s.
Example
input
Copy
3
4 9
5 10
42 9999999967
output
Copy
6
1
9999999966
Note
In the first test case appropriate xx-s are [0,1,3,4,6,7][0,1,3,4,6,7].
In the second test case the only appropriate xx is 00.
题意:
T组数据,
每一组数据给定两个整数a和m
问有多少个x满足:
\(0 \le x < m\) 和\(\gcd(a, m) = \gcd(a + x, m)\)
思路:
设\(b=\gcd(a,m)\),那么问题等同于问:
区间\([a,a+m-1]\) 中有多少个数x使\(\gcd(x,m)=b\)
又因为\(\gcd(x/b,m/b)=1\) , 所以问题可以转化为 :
区间\([a/b,(a+m-1)/b]\) 中有多少个数x使\(\gcd(x,m/b)=1\)
即区间\([a/b,(a+m-1)/b]\)中有多少个数x与m/b互质。
我们知道这是一个因子分解+容斥定理的经典问题。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
vector<ll> list;
void breakdown(ll x)
{
list.clear();
for (ll i = 2; i * i <= x; ++i) {
int cnt = 0;
while (x % i == 0) {
++cnt;
x /= i;
}
if (cnt) {
list.push_back(i);
}
}
if (x > 1) {
list.push_back(x);
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int t;
t = readint();
ll a, m;
while (t--)
{
a = readll();
m = readll();
ll b = gcd(a, m);
breakdown(m / b);
ll l = a;
ll r = a + m - 1;
l /= b;
r /= b;
ll ans = 0;
int len = list.size();
for (int i = 0; i < (1 << len); ++i) {
int cnt = 0;
ll pp = 1ll;
for (int j = 0; j < len; ++j) {
if (i & (1 << j)) {
++cnt;
pp *= list[j];
}
}
ans += (r / pp - (l - 1) / pp) * ((cnt & 1) ? -1 : 1);
}
printf("%lld\n", ans );
}
return 0;
}