P1592互質【數學】【歐拉函數】

題目鏈接:https://www.luogu.org/problem/P1592
AC代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
ll a[maxn];
int main()
{
    ll n,k;
    cin>>n>>k;
    ll cnt=0;
    for(ll i=1;i<n;i++){
        if(__gcd(n,i)==1){
            a[++cnt]=i;
        }
    }
//    for(int i=1;i<=cnt;i++){
//        cout<<a[i]<<"  ";
//    }
//    cout<<endl;
    ll s=k/cnt;//歐拉函數的週期性
    ll s1=k%cnt;
    ll ans=n*s+a[s1];
    cout<<ans<<endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章