結果

問題 No.1287 えぬけー
ユーザー beetbeet
提出日時 2020-11-13 21:31:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,386 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 217,620 KB
実行使用メモリ 9,292 KB
最終ジャッジ日時 2023-09-30 02:36:08
合計ジャッジ時間 6,076 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
9,292 KB
testcase_01 AC 21 ms
4,864 KB
testcase_02 AC 23 ms
4,792 KB
testcase_03 AC 34 ms
4,492 KB
testcase_04 AC 21 ms
4,980 KB
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=Int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


// find x s.t. a^x = b (x >= 0)
// return MOD if not found
// MOD can be composite numbers
template<typename T>
T mod_log(T a,T b,const T MOD){
  using ll = long long;
  ll g=1;
  {
    ll m=MOD;
    while(m){
      g=(ll)g*a%MOD;
      m>>=1;
    }
  }
  g=__gcd(g,(ll)MOD);
  ll c=0,t=1;
  while(t%g){
    if(t==b) return c;
    t=t*a%MOD;
    c++;
  }
  // not found
  if(b%g) return MOD;
  t/=g;b/=g;
  const ll n=MOD/g;
  ll h=0,gs=1;
  while(h*h<n){
    gs=gs*a%n;
    ++h;
  }
  unordered_map<ll, ll> bs;
  {
    ll s=0,e=b;
    while(s<h){
      e=e*a%n;
      bs[e]=++s;
    }
  }
  {
    ll s=0,e=t;
    while(s<n){
      e=e*gs%n;
      s+=h;
      if(bs.count(e))
        return c+s-bs[e];
    }
  }
  // not found
  return MOD;
}


// MOD can be composite numbers
template<typename T>
T mod_pow(T a,long long n,const T MOD){
  using ll = long long;
  T res(1);
  while(n){
    if(n&1) res=(ll)res*a%MOD;
    a=(ll)a*a%MOD;
    n>>=1;
  }
  return res;
}


// mod can be composite numbers
template<typename T>
T order(T x,const T MOD){
  static map<T, vector<T>> dp;
  static map<T, T> phi;

  vector<T> &ps=dp[MOD];
  if(ps.empty()){
    T res=MOD,n=MOD;
    for(T i=2;i*i<=n;i++){
      if(n%i) continue;
      while(n%i==0) n/=i;
      res=res/i*(i-1);
    }
    if(n!=1) res=res/n*(n-1);
    phi[MOD]=res;

    for(T i=2;i*i<=res;i++){
      if(res%i) continue;
      while(res%i==0) res/=i;
      ps.emplace_back(i);
    }
    if(res!=1) ps.emplace_back(res);
  }

  T res=phi[MOD];
  for(T p:ps){
    while(res%p==0){
      if(mod_pow(x,res/p,MOD)!=1) break;
      res/=p;
    }
  }
  return res;
}

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  const Int MOD = 1e9+7;
  Int r=2;
  while(order(r,MOD)!=MOD-1) r++;

  Int T;
  cin>>T;
  while(T--){
    Int X,K;
    cin>>X>>K;
    if(X==0) cout<<0<<newl;
    else cout<<mod_pow(r,mod_log(mod_pow(r,K,MOD),X,MOD),MOD)<<newl;
  }

  return 0;
}
0