前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >D. Same GCDs

D. Same GCDs

作者头像
某些人
发布2020-04-09 18:04:10
2140
发布2020-04-09 18:04:10
举报
文章被收录于专栏:一Wa哇一天一Wa哇一天

题目链接:D. Same GCDs

time limit per test:2 seconds memory limit per test:256 megabytes inputstandard input outputstandard output

You are given two integers a and m. Calculate the number of integers x such that 0≤x<m and gcd(a,m)=gcd(a+x,m). Note: gcd(a,b) is the greatest common divisor of a and b. Input The first line contains the single integer T (1≤T≤50) — the number of test cases. Next T lines contain test cases — one per line. Each line contains two integers a and m (1≤a<m≤1010). Output Print T integers — one per test case. For each test case print the number of appropriate x-s. input

3
4 9
5 10
42 9999999967

output

6
1
9999999966

Note In the first test case appropriate x-s are [0,1,3,4,6,7]. In the second test case the only appropriate x is 0.

题目大意

给你两个数 a,m。从[a,a+m)中取出任意一个数x,使得gcd(x,m)=gcd(a,m)成立,问你这样的的x的个数一共有几个。

解题思路

我们设 p=gcd(a,m); 则p=gcd(x,m);然后同时除以p得1=gcd(x/p,m/p);这时候我们发现其实要求的的也就是[a/p,a/p+m/p)中与m/p互质的数有几个。同时减去a/p的,也就是[1,m/p)中于m/p互质的的个数,直接用欧拉函数就可以了;

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    int t;
    ll a,m;
    cin>>t;
    while(t--)
    {
        cin>>a>>m;
        ll p=__gcd(a,m);
        ll ans=m/p;
        ll q=ans;
        //欧拉函数
        for(ll i=2;i*i<=q;i++)
        {
            if(q%i==0) ans=ans-ans/i;
            while(q%i==0) q/=i;
        }
        if(q!=1) ans-=ans/q;
        cout<<ans<<"\n";
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目链接:D. Same GCDs
    • 题目大意
      • 解题思路
        • 代码
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档