結果

問題 No.981 一般冪乗根
ユーザー tko919tko919
提出日時 2020-04-07 21:37:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5,891 ms / 6,000 ms
コード長 1,842 bytes
コンパイル時間 1,934 ms
コンパイル使用メモリ 177,788 KB
実行使用メモリ 8,768 KB
最終ジャッジ日時 2023-09-22 10:41:42
合計ジャッジ時間 190,924 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5,168 ms
8,196 KB
testcase_01 AC 5,232 ms
7,856 KB
testcase_02 AC 5,178 ms
8,400 KB
testcase_03 AC 5,182 ms
8,028 KB
testcase_04 AC 5,188 ms
8,256 KB
testcase_05 AC 156 ms
7,496 KB
testcase_06 AC 155 ms
7,820 KB
testcase_07 AC 157 ms
7,688 KB
testcase_08 AC 156 ms
7,632 KB
testcase_09 AC 156 ms
8,760 KB
testcase_10 AC 155 ms
7,692 KB
testcase_11 AC 155 ms
7,840 KB
testcase_12 AC 155 ms
7,548 KB
testcase_13 AC 157 ms
7,704 KB
testcase_14 AC 155 ms
7,608 KB
testcase_15 AC 156 ms
8,768 KB
testcase_16 AC 155 ms
7,684 KB
testcase_17 AC 155 ms
7,692 KB
testcase_18 AC 155 ms
4,384 KB
testcase_19 AC 155 ms
4,376 KB
testcase_20 AC 154 ms
4,380 KB
testcase_21 AC 155 ms
4,380 KB
testcase_22 AC 156 ms
4,380 KB
testcase_23 AC 155 ms
4,380 KB
testcase_24 AC 155 ms
4,376 KB
testcase_25 AC 5,256 ms
4,824 KB
testcase_26 AC 5,891 ms
5,204 KB
testcase_27 AC 6 ms
4,380 KB
testcase_28 AC 4,804 ms
5,116 KB
evil_60bit1.txt TLE -
evil_60bit2.txt TLE -
evil_60bit3.txt TLE -
evil_hack AC 3 ms
4,376 KB
evil_hard_random TLE -
evil_hard_safeprime.txt TLE -
evil_hard_tonelli0 TLE -
evil_hard_tonelli1 TLE -
evil_hard_tonelli2 TLE -
evil_hard_tonelli3 TLE -
evil_sefeprime1.txt TLE -
evil_sefeprime2.txt TLE -
evil_sefeprime3.txt TLE -
evil_tonelli1.txt TLE -
evil_tonelli2.txt TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
次のファイルから読み込み:  /usr/local/gcc7/include/c++/12.2.0/unordered_map:46,
         次から読み込み:  /usr/local/gcc7/include/c++/12.2.0/x86_64-pc-linux-gnu/bits/stdc++.h:117,
         次から読み込み:  main.cpp:2:
メンバ関数 ‘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 /usr/local/gcc7/include/c++/12.2.0/bits/unordered_map.h:902:26,
    inlined from ‘int modroot(int, int, int)’ at main.cpp:39:20:
/usr/local/gcc7/include/c++/12.2.0/bits/hashtable.h:1725:23: 警告: ‘g’ may be used uninitialized [-Wmaybe-uninitialized]
 1725 |       auto __it = find(__k);
      |                   ~~~~^~~~~
/usr/local/gcc7/include/c++/12.2.0/bits/hashtable.h: 関数 ‘int modroot(int, int, int)’ 内:
/usr/local/gcc7/include/c++/12.2.0/bits/hashtable.h:1663:5: 備考: by argument 2 of type ‘const std::_Hashtable<int, std::pair<const int, int>, std::allocator<std::pair<const int, int> >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_rang

ソースコード

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; cin>>q;
   while(q--){
      int k,y,m; cin>>m>>k>>y;
      cout<<modroot(k,y,m)<<endl;
   }
   return 0;
}
0