CodeForces - 1295D Same GCDs(欧拉函数)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1295D Same GCDs(欧拉函数)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:點(diǎn)擊查看
題目大意:給出兩個(gè)正整數(shù) a 和 m ,再給出 x 的范圍為[ 0 , m ),現(xiàn)在要求滿足 gcd ( a , m ) = gcd ( a + x , m ) 時(shí) x 的個(gè)數(shù)
題目分析:因?yàn)檩氜D(zhuǎn)相除法的定義:gcd(a,b) = gcd(b,a mod b)可知,當(dāng) i ∈ [ a , m ] 和 i ∈ [ m , 2 * m ] 時(shí),gcd( i ,m ) 相等的 i 的個(gè)數(shù)相同,因?yàn)槊看味际菍?duì)第二個(gè)數(shù)取模,所以具有周期性,而題目要求我們求得是 i ∈ [ a , m + a -1 ] 時(shí)的個(gè)數(shù),因?yàn)殚L(zhǎng)度為一個(gè)完整的周期,所以我們不妨將其問(wèn)題轉(zhuǎn)換為 i ∈ [ 0 , m ] 時(shí),滿足gcd( i , m ) = = gcd ( a , m ) 的個(gè)數(shù)
剩下的就能用歐拉函數(shù)解決了:
也就是求m / gcd ( a , m ) 的歐拉函數(shù)
代碼:
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;LL eular(LL n) {LL ans=n;for(LL i=2;i*i<=n;i++){if(n%i==0){ans=ans/i*(i-1);while(n%i==0)n/=i;}}if(n>1) ans=ans/n*(n-1);return ans; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int w;cin>>w;while(w--){LL a,m;scanf("%lld%lld",&a,&m);m/=__gcd(a,m);printf("%lld\n",eular(m));}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的CodeForces - 1295D Same GCDs(欧拉函数)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CodeForces - 1295C O
- 下一篇: 回文自动机模板