結果

問題 No.981 一般冪乗根
ユーザー tko919
提出日時 2020-04-07 21:46:53
言語 C++14
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 3,109 ms / 6,000 ms
+ 686µs
コード長 3,400 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,147 ms
コンパイル使用メモリ 196,476 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-08-12 04:37:51
合計ジャッジ時間 50,180 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
外部呼び出し有り
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 29 TLE * 1 -- * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
void tostr(ll x,string& res){while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return;}
template<class T> inline bool chmax(T& a,T b){ if(a<b){a=b;return 1;}return 0; }
template<class T> inline bool chmin(T& a,T b){ if(a>b){a=b;return 1;}return 0; }
//end

ll mod=1e9+7;
struct Mint {
   ll val;
   ll inv() const{
      ll tmp,a=val,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0)x+=mod; return x;
   }
   Mint():val(0){}
   Mint(ll x):val(x>=0?x%mod:mod+(x%mod)){}
   Mint pow(ll t){Mint res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;}
   Mint& operator+=(const Mint& x){if((val+=x.val)>=mod)val-=mod;return *this;}
   Mint& operator-=(const Mint& x){if((val+=mod-x.val)>=mod)val-=mod; return *this;}
   Mint& operator*=(const Mint& x){val=val*x.val%mod; return *this;}
   Mint& operator/=(const Mint& x){val=val*x.inv()%mod; return *this;}
   Mint operator+(const Mint& x)const{return Mint(*this)+=x;}
   Mint operator-(const Mint& x)const{return Mint(*this)-=x;}
   Mint operator*(const Mint& x)const{return Mint(*this)*=x;}
   Mint operator/(const Mint& x)const{return Mint(*this)/=x;}
   bool operator==(const Mint& x)const{return val==x.val;}
   bool operator!=(const Mint& x)const{return val!=x.val;}
};
struct factorial {
   vector<Mint> Fact, Finv;
public:
   factorial(int maxx){
      Fact.resize(maxx+1),Finv.resize(maxx+1); Fact[0]=Mint(1); rep(i,0,maxx)Fact[i+1]=Fact[i]*(i+1);
      Finv[maxx]=Mint(1)/Fact[maxx]; rrep(i,maxx,0)Finv[i-1]=Finv[i]*i;
   }
   Mint fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];}
   Mint nPr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[n-r];}
   Mint nCr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[r]*Finv[n-r];}
};

int mpow(int x,ll t,int m){
   int res=1;
   while(t){
      if(t&1)res=(1LL*res*x)%m;
      x=(1LL*x*x)%m; t>>=1;
   } return res;
}

int phi(int n){
   int res=n;
   for(int i=2;i*i<=n;i++)if(n%i==0){
      res=res/i*(i-1); while(n%i==0)n/=i;
   } if(n!=1)res=res/n*(n-1); return res;
}

mt19937 mt;
unordered_map<int,int> memo_g;
int modroot(int k,int y){
   if(mod==2)return y&1;
   vector<int> ds; int tmp=mod-1;
   for(int p=2;p*p<=tmp;p++)if(tmp%p==0){
      ds.push_back(p); if(p*p!=tmp)ds.push_back(tmp/p);
   } int g;
   if(!memo_g.count(g)){
      while(1){
         g=mt()%(mod-2)+2; bool f=1;
         for(int d:ds)if(Mint(g).pow(d)==1){f=0; break;}
         if(f)break;
      } memo_g[mod]=g;
   }
   else g=memo_g[mod];
   unordered_map<int,int> mp;
   int cur=y,ig=Mint(g).inv(),block=sqrt(mod);
   rep(i,0,block+2){mp[cur]=i; cur=(1LL*cur*ig)%mod;}
   int gs=Mint(g).pow(block).val,x=-1,pos=0; cur=1;
   while(x==-1){
      if(mp.count(cur))x=pos+mp[cur];
      cur=(1LL*cur*gs)%mod; pos+=block;
   }
   int gcd=__gcd(tmp,k); if(x%gcd)return -1;
   tmp/=gcd; x/=gcd; k/=gcd;
   return Mint(g).pow(1LL*x*mpow(k,phi(tmp)-1,tmp)%tmp).val;
}

int main(){
   int q; cin>>q;
   while(q--){
      int k,y; cin>>mod>>k>>y;
      cout<<modroot(k,y)<<endl;
   }
   return 0;
}
0