結果

問題 No.981 一般冪乗根
ユーザー tko919tko919
提出日時 2020-04-08 18:44:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 6,000 ms
コード長 2,171 bytes
コンパイル時間 1,740 ms
コンパイル使用メモリ 175,172 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 08:53:58
合計ジャッジ時間 44,758 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,380 KB
testcase_01 AC 5 ms
4,380 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 5 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 8 ms
4,376 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 14 ms
4,376 KB
evil_60bit1.txt WA -
evil_60bit2.txt WA -
evil_60bit3.txt WA -
evil_hack AC 2 ms
4,376 KB
evil_hard_random WA -
evil_hard_safeprime.txt WA -
evil_hard_tonelli0 WA -
evil_hard_tonelli1 WA -
evil_hard_tonelli2 WA -
evil_hard_tonelli3 WA -
evil_sefeprime1.txt WA -
evil_sefeprime2.txt WA -
evil_sefeprime3.txt WA -
evil_tonelli1.txt WA -
evil_tonelli2.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 mpow(ll x,ll k,ll m){
   ll res=1;
   while(k){
      if(k&1)res=(res*x)%m;
      x=(x*x)%m; k>>=1;
   } return res;
}

ll inv(ll a,ll m){
   ll b=m,u=1,v=0;
   while(b){
      ll t=a/b;
      a-=t*b; swap(a,b);
      u-=t*v; swap(u,v);
   }
   u%=m; if(u<0)u+=m; return u;
}

ll sub_root(ll p,int e,ll a,ll m){//x^(p^e)==a(mod m)
   ll q=m-1; int s=0; while(q%p==0){q/=p; s++;} int d=s-e;
   ll pe=mpow(p,e,m),res=mpow(a,((pe-1)*inv(q,pe)%pe*q+1)/pe,m),c=1;
   while(mpow(c,(m-1)/p,m)==1)c++; c=mpow(c,q,m);
   map<ll,ll> mp; ll v=1,block=sqrt(d*p)+1,bs=mpow(c,mpow(p,s-1,m-1)*block%(m-1),m);
   rep(i,0,block+1)mp[v]=i,v=v*bs%m;
   ll gs=inv(mpow(c,mpow(p,s-1,m-1),m),m);
   cerr<<res<<endl;
   rep(i,0,d){
      ll err=a*inv(mpow(res,pe,m),m)%m;
      ll pos=mpow(err,mpow(p,d-1-i,m-1),m);
      rep(j,0,block+1){
         if(mp.count(pos)){
            res=res*mpow(c,(block*mp[pos]+j)*mpow(p,i,m-1)%(m-1),m)%m;
            break;
         } pos=pos*gs%m;
      } 
   } return res;
}

ll mod_root(ll k,ll a,ll m){
   if(a==0)return k?0:-1;
   if(m==2)return a&1;
   k%=m-1; ll g=__gcd(k,m-1);
   if(mpow(a,(m-1)/g,m)!=1)return -1;
   a=mpow(a,inv(k/g,(m-1)/g),m);
   for(ll d=2;d*d<=g;d++)if(g%d==0){
      int sz=0;
      while(g%d==0){g/=d; sz++;}
      a=sub_root(d,sz,a,m);
   }
   if(g>1)a=sub_root(g,1,a,m); return a;
}

int main(){
   int q; cin>>q;
   while(q--){
      ll k,a,m; scanf("%lld%lld%lld",&m,&k,&a);
      ll res=mod_root(k,a,m); printf("%lld\n",res);
      //if(res!=-1 and mpow(res,k,m)!=a)cerr<<"-----------------------------------"<<endl;
   }
   return 0;
}
0