首页 > 其他分享 >D. Same GCDs

D. Same GCDs

时间:2024-07-10 14:31:28浏览次数:15  
标签:frac GCDs ll varphi Same ans 互质 gcd

原题链接

题解

\(\gcd(a+x,m) = \gcd((a+x)mod\ m,m)\)
由于 \(x\in[0,m-1]\),所以 \((a+x)mod\ m\) 一定能遍历完 \([0,m-1]\) 里的所有数
所以 \(\gcd(a+x,m)\) 等价于 \(\gcd(x,m)\)
接下来,令 \(d=\gcd(a,m)\),则有 \(m=t_1d\),\(x=t_2d\),其中 \(t_1\) 与 \(t_2\) 互质(如果不互质,代表其还有公因子没有给到 \(d\) )
所以等价于求有多少 \(t\),使得 \(t\) 与 \(\frac{m}{d}\) 互质,且 \(t·d \lt m\),也就是求 \(\varphi(\frac{m}{d})\)

如何求 \(\varphi(\frac{m}{d})\)

首先,如果 \(n\),\(m\) 互质,则 \(\varphi(nm)=\varphi(n)·\varphi(m)\)

为什么 画一个 $n$ x $m$ 的表格,行坐标为 $[0,m-1]$,纵坐标为 $[1,n]$,则表格上每个点表示的数为 $x_in+y_i$

则有 \(\varphi(n)\) 个行的元素与 \(n\) 互质

同理,有 \(\varphi(m)\) 个列的元素与 \(m\) 互质

而与 \(nm\) 互质,当且仅当与 \(n\),\(m\) 均互质
所以 \(\varphi(nm)=\varphi(n)·\varphi(m)\)

接着,对于任意正整数 \(n\),其质因子分解为 \(n=p_1^{a_1}·p_2^{a_2}·...·p_k^{a_k}\),\(p\) 为质数
所以 \(\varphi(n)=\varphi(p_1^{a_1})·\varphi(p_2^{a_2})·...·\varphi(p_k^{a_k})\)
而对于 \(p^a\),有 \(\varphi(p^a)=p^a-p^{a-1}=p^a(1-\frac{1}{p})\)

为什么

由于 \(p\) 是质数,所以 \([1,p^a]\) 里,不与其互质的数只有
\(1p,2p,3p...p^{a-1}p\)
总共 \(p^{a-1}\) 个

所以 \(\varphi(n)=p_1^{a^1}(1-\frac{1}{p_1})p_2^{a^2}(1-\frac{1}{p_2})...p_k^{a_k}(1-\frac{1}{p_k})\)

code

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

ll phi(ll x)
{
    ll ans=x;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0) x/=i;
        }
    }

    if(x>1)
    {
        ans=ans/x*(x-1);
    }
    return ans;
}

ll solve()
{
    ll a,m;
    cin>>a>>m;

    int d=__gcd(a,m);
    return phi(m/d);
}
int main()
{
    int t;
    cin>>t;

    while(t--) cout<<solve()<<'\n';
    return 0;
}

标签:frac,GCDs,ll,varphi,Same,ans,互质,gcd
From: https://www.cnblogs.com/pure4knowledge/p/18293991

相关文章

  • not_the_same_3dsctf_2016
    not_the_same_3dsctf_2016查看保护就不说了IDA反编译发现为静态编译,不用找libc了这点节省不少功夫。打开main函数,发现栈溢出漏洞偏移为45,这个是根据gdb的cyclic算出来的发现后门函数get_secret,功能是把flag读取赋值给fl4g初步思路为通过栈溢出,调用get_secret,再通过write函......
  • Is a charger the same as an adaptor?
    Achargerandanadapterarenotthesame,althoughtheyareoftenusedtogetherandtheirfunctionscanoverlapdependingonthecontext.Herearethedistinctionsandrelationshipsbetweenthetwo:Charger:Function:Achargerisspecificallydesigned......
  • 解决vue项目报错 ERROR in Conflict:Multiple assets emit different content to the
    vue-cli创建项目ERROR in Conflict: Multiple assets emit different content to the same filename index.html问题的解决办法用vue-cli正常来创建新的项目在运行npmrundev或者npmrunserve有以下报错:ERRORinConflict:Multipleassetsemitdifferentco......
  • 100. Same Tree
    Giventherootsoftwobinarytreespandq,writeafunctiontocheckiftheyarethesameornot.Twobinarytreesareconsideredthesameiftheyarestructurallyidentical,andthenodeshavethesamevalue.Example1:Input:p=[1,2,3],q=[1,2......
  • CF1593D2. Half of Same
    题目链接:HalfofSame-洛谷|计算机科学教育新生态(luogu.com.cn)WA代码:#include<bits/stdc++.h>usingnamespacestd;#defineMAX44constintN=2e6+6;intarr[MAX];intcnt_1[N];//记录每个数出现的次数intcnt_2[N];//记录每个因数intmain(){intt;c......
  • error Conflict: Multiple assets emit different content to the same filename ind
    ERRORFailedtocompilewith1error20:32:04errorConflict:Multipleassetsemitdifferentcontenttothesamefilenameindex.htmlERRORinConflict:Multipleassetsemitdif......
  • Tensors of the same index must be on the same device and the same dtype except `
    避免使用 torch.set_default_dtype(torch.float64) 可以尝试采用model.Double或者model.to(torch.Double)m=torch().to(device).to(torch.float64)     参考:Tensorsofthesameindexmustbeonthesamedeviceandthesamedtypeexcept`step`t......
  • SpringBoot的Cookie sameSite之坑
    https://blog.csdn.net/weixin_38296425/article/details/111941318 CSDN上很多文章给出了解决CookiesameSite坑跨域之坑的解决办法,但是都忽略了一个问题,没有给出相关的依赖,我也是费了不少劲终于找到了解决办法,在这里记录下来。例如下面的代码:@ConfigurationpublicclassT......
  • Redis报错:CROSSSLOT Keys in request don't hash to the same slot的解决方案
    最近,项目上线的时候,出现了一个Redis的报错:CROSSSLOTKeysinrequestdon'thashtothesameslot,这个在内网环境下无法复现,因为正式环境的Redis是cluster集群模式,而我们内网环境是单机模式。(后面我在内网也部署了一个Redis集群,具体见我这一篇文章《使用Docker搭建RedisCluste......
  • because it set 'X-Frame-Options' to 'sameorigin'
    报错becauseitset'X-Frame-Options'to'sameorigin'.Refusedtodisplay'https://xxx.xxx.cn/'inaframebecauseitset'X-Frame-Options'to'sameorigin'.解决方法:修改页面,增加meta配置<head><!--CSP......