# Educational Codeforces Round 81 (Rated for Div. 2) D. Same GCDs （容斥）

g c d ( k 1 ∗ g + k 3 ∗ g , k 2 ∗ g ) = g gcd(k1*g+k3*g,k2*g)=g ，有 g c d ( k 1 + k 3 , k 2 ) = 1 gcd(k1+k3,k2)=1 k 1 > = 1 k1>=1

g c d ( k 1 + k 3 , k 2 ) = 1 gcd(k1+k3,k2)=1 等价于 g c d ( ( k 1 + k 3 )   m o d   k 2 , k 2 ) = 1 gcd((k1+k3)\ mod\ k2,k2)=1 ，又 k 3 ∈ [ 0 , k 2 ] k3\in[0,k2]

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<ctime>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x)  (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;

const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const double alpha=0.75;
const int mod=998244353;
const double eps=1e-8;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=100100;
const int maxm=100100;
const int maxp=100100;
const int up=1100;

ll fac[maxn],cnt[maxn],tot=0;

ll gcd(ll a,ll b)
{
if(b==0) return a;
return gcd(b,a%b);
}

void gao(ll n)
{
tot=0;
for(ll i=1;i*i<=n;i++)
{
if(n%i) continue;
fac[++tot]=i;
if(i*i!=n) fac[++tot]=n/i;
}
}

int main(void)
{
int tt;
scanf("%d",&tt);
while(tt--)
{
ll a,m;
scanf("%lld%lld",&a,&m);
ll g=gcd(a,m);
a/=g,m/=g;
gao(m);
sort(fac+1,fac+tot+1);
for(int i=1;i<=tot;i++)
cnt[i]=m/fac[i];
for(int i=tot;i>=1;i--)
{
for(int j=i+1;j<=tot;j++)
if(fac[j]%fac[i]==0)
cnt[i]-=cnt[j];
}
printf("%lld\n",cnt[1]);
}
return 0;
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

_Shmily

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文