結果

問題 No.981 一般冪乗根
ユーザー tko919tko919
提出日時 2020-04-07 21:59:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,862 bytes
コンパイル時間 2,062 ms
コンパイル使用メモリ 179,092 KB
実行使用メモリ 6,816 KB
最終ジャッジ日時 2024-07-08 05:33:15
合計ジャッジ時間 51,230 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
evil_60bit1.txt RE -
evil_60bit2.txt RE -
evil_60bit3.txt RE -
evil_hack RE -
evil_hard_random RE -
evil_hard_safeprime.txt RE -
evil_hard_tonelli0 RE -
evil_hard_tonelli1 RE -
evil_hard_tonelli2 RE -
evil_hard_tonelli3 RE -
evil_sefeprime1.txt RE -
evil_sefeprime2.txt RE -
evil_sefeprime3.txt RE -
evil_tonelli1.txt RE -
evil_tonelli2.txt RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/unordered_map:46,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:117,
                 from main.cpp:2:
In member function 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::size_type std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::count(const key_type&) const [with _Key = int; _Value = std::pair<const int, int>; _Alloc = std::allocator<std::pair<const int, int> >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _Hash = std::hash<int>; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>]',
    inlined from 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::size_type std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::count(const key_type&) const [with _Key = int; _Tp = int; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, int> >]' at /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/unordered_map.h:902:26,
    inlined from 'int modroot(int, int, int)' at main.cpp:39:20:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/hashtable.h:1725:23: warning: 'g' may be used uninitialized [-Wmaybe-uninitialized]
 1725 |       auto __it = find(__k);
      |                   ~~~~^~~~~
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/hashtable.h: In function 'int modroot(int, int, int)':
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/hashtable.h:1663:5: note: by argument 2 of type 'const std::_Hashtable<int, std::pair<const int, int>, std::allocator<std::pair<const int, int> >, std::__detail::_Sele

ソースコード

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

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,int m){
   if(m==2)return y&1;
   vector<int> ds; int tmp=m-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()%(m-2)+2; bool f=1;
         for(int d:ds)if(mpow(g,d,m)==1){f=0; break;}
         if(f)break;
      } memo_g[m]=g;
   }
   else g=memo_g[m];
   unordered_map<int,int> mp;
   int cur=y,ig=mpow(g,m-2,m),block=sqrt(m);
   rep(i,0,block+2){mp[cur]=i; cur=(1LL*cur*ig)%m;}
   int gs=mpow(g,block,m),x=-1,pos=0; cur=1;
   while(x==-1){
      if(mp.count(cur))x=pos+mp[cur];
      cur=(1LL*cur*gs)%m; pos+=block;
   }
   int gcd=__gcd(tmp,k); if(x%gcd)return -1;
   tmp/=gcd; x/=gcd; k/=gcd;
   return mpow(g,(1LL*x*mpow(k,phi(tmp)-1,tmp))%tmp,m);
}

int main(){
   int q; scanf("%d",&q);
   while(q--){
      int k,y,m; scanf("%d%d%d",m,k,y);
      printf("%d\n",modroot(k,y,m));
   }
   return 0;
}
0