結果
| 問題 |
No.1287 えぬけー
|
| コンテスト | |
| ユーザー |
beet
|
| 提出日時 | 2020-11-13 21:31:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,386 bytes |
| コンパイル時間 | 2,364 ms |
| コンパイル使用メモリ | 212,176 KB |
| 最終ジャッジ日時 | 2025-01-15 22:36:58 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 1 TLE * 4 |
ソースコード
#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;
}
beet