結果

問題 No.981 一般冪乗根
ユーザー chocoruskchocorusk
提出日時 2020-02-08 17:19:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5,221 ms / 6,000 ms
コード長 2,337 bytes
コンパイル時間 1,401 ms
コンパイル使用メモリ 118,840 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2023-07-30 19:56:56
合計ジャッジ時間 196,959 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
ll gcd(ll a, ll b){
    if(b==0) return a;
    return gcd(b, a%b);
}
ll powmod(ll a, ll k, ll mod){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=mod;
        }
        ap=ap*ap;
        ap%=mod;
        k>>=1;
    }
    return ans;
}
ll inv(ll a, ll m){
    ll b=m, x=1, y=0;
    while(b>0){
        ll t=a/b;
        swap(a-=t*b, b);
        swap(x-=t*y, y);
    }
    return (x%m+m)%m;
}
ll discretelog(ll x, ll y, ll m){
	ll d=1;
	while(d*d<m) d++;
	vector<P> v(d);
	ll invx=inv(x, m);
	ll y1=y;
	for(int i=0; i<d; i++){
		v[i]=P(y1, i);
		(y1*=invx)%=m;
	}
	sort(v.begin(), v.end());
	ll xd=powmod(x, d, m);
	ll q=1;
	for(int i=0; i<d; i++){
		int k=lower_bound(v.begin(), v.end(), P(q, 0))-v.begin();
		if(v[k].first==q){
			return i*d+v[k].second;
		}
		(q*=xd)%=m;
	}
    return -1;
}
vector<P> fac(ll x){
	vector<P> ret;
	for(ll i=2; i*i<=x; i++){
		if(x%i==0){
			int e=0;
			while(x%i==0){
				x/=i;
				e++;
			}
			ret.push_back({i, e});
		}
	}
	if(x>1) ret.push_back({x, 1});
	return ret;
}
ll order(ll p, ll a, vector<P> v){
	ll d=1;
	for(auto q:v){
		ll r=q.first; int e=q.second;
		ll rp=1;
		bool myon=0;
		for(int i=1; i<=e; i++){
			rp*=r;
			if(powmod(a, (p-1)/rp, p)!=1){
				d*=(rp/r);
				myon=1;
				break;
			}
		}
		if(!myon) d*=rp;
	}
	return (p-1)/d;
}
ll root(ll p){
	vector<P> v=fac(p-1);
	for(ll r=2; ; r++){
		if(order(p, r, v)==p-1){
			return r;
		}
	}
}
ll solve(ll p, int k, ll a){
	if(p==2){
		return 1;
	}
	if(a==1){
		return 1;
	}
	ll r=root(p);
	ll d=discretelog(r, a, p);
	ll g=gcd(k, p-1);
	if(d%g!=0){
		return -1;
	}
	d/=g;
	return powmod(r, inv(k/g, (p-1)/g)*d%((p-1)/g), p);
}
int main()
{
	int t;
	cin>>t;
	while(t--){
		ll p, a;
		int k;
		cin>>p>>k>>a;
        cout<<solve(p, k, a)<<endl;
	}
	return 0;
}
0