結果

問題 No.981 一般冪乗根
ユーザー chocoruskchocorusk
提出日時 2020-02-08 17:28:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5,296 ms / 6,000 ms
コード長 3,282 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 130,388 KB
実行使用メモリ 7,660 KB
最終ジャッジ日時 2023-07-30 20:01:12
合計ジャッジ時間 129,244 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4,614 ms
7,464 KB
testcase_01 AC 4,573 ms
7,628 KB
testcase_02 AC 4,555 ms
7,660 KB
testcase_03 AC 4,572 ms
4,380 KB
testcase_04 AC 4,560 ms
4,376 KB
testcase_05 AC 117 ms
4,380 KB
testcase_06 AC 118 ms
4,376 KB
testcase_07 AC 119 ms
4,376 KB
testcase_08 AC 118 ms
4,376 KB
testcase_09 AC 117 ms
4,376 KB
testcase_10 AC 119 ms
4,376 KB
testcase_11 AC 118 ms
4,376 KB
testcase_12 AC 118 ms
4,376 KB
testcase_13 AC 120 ms
4,380 KB
testcase_14 AC 119 ms
4,380 KB
testcase_15 AC 120 ms
4,380 KB
testcase_16 AC 118 ms
4,380 KB
testcase_17 AC 119 ms
4,376 KB
testcase_18 AC 117 ms
4,376 KB
testcase_19 AC 117 ms
4,380 KB
testcase_20 AC 117 ms
4,376 KB
testcase_21 AC 118 ms
4,380 KB
testcase_22 AC 119 ms
4,376 KB
testcase_23 AC 123 ms
4,376 KB
testcase_24 AC 120 ms
4,380 KB
testcase_25 AC 4,600 ms
4,380 KB
testcase_26 AC 5,296 ms
4,376 KB
testcase_27 AC 6 ms
4,376 KB
testcase_28 AC 4,262 ms
4,376 KB
evil_60bit1.txt RE -
evil_60bit2.txt MLE -
evil_60bit3.txt TLE -
evil_hack AC 3 ms
4,364 KB
evil_hard_random MLE -
evil_hard_safeprime.txt MLE -
evil_hard_tonelli0 TLE -
evil_hard_tonelli1 TLE -
evil_hard_tonelli2 MLE -
evil_hard_tonelli3 MLE -
evil_sefeprime1.txt MLE -
evil_sefeprime2.txt MLE -
evil_sefeprime3.txt TLE -
evil_tonelli1.txt MLE -
evil_tonelli2.txt MLE -
権限があれば一括ダウンロードができます

ソースコード

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<ll, int> P;
ll gcd(ll a, ll b){
	if(a==0) return b;
	if(b==0) return a;
	if(a<0) a=-a;
	if(b<0) b=-b;
	const int s=__builtin_ctzll(a|b);
	a>>=__builtin_ctzll(a);
	while(b){
		b>>=__builtin_ctzll(b);
		if(a>b) swap(a, b);
		b-=a;
	}
	return a<<s;
}
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;
}
mt19937 mt(334);
bool is_prime(ll n){
	if(n<=1 || !(n&1)) return (n==2);
	ll d=n-1;
	int k=0;
	while(!(d&1)){
		d>>=1;
		k++;
	}
	uniform_int_distribution<> rnd(1, n-1);
	for(int z=0; z<10; z++){
		ll a=rnd(mt);
		if(a==0) continue;
		bool comp=1;
		ll ap=powmod(a, d, n);
		if(ap==1 || ap==n-1) continue;
		for(int r=1; r<k; r++){
			ap=ap*ap%n;
			if(ap==n-1){
				comp=0;
				break;
			}
		}
		if(comp) return false;
	}
	return true;
}
ll rho(ll n, ll& c){
	if(!(n&1)) return 2;
	while(1){
		ll x=2, y=2, d=1;
		while(d==1){
			x=(x*x+c)%n;
			y=(y*y+c)%n;
			y=(y*y+c)%n;
			d=gcd(abs(x-y), n);
		}
		if(d==n){
			c++;
			continue;
		}
		return d;
	}
}
vector<pair<ll, int>> factorize(ll n){
	if(n<=1){
		vector<pair<ll, int>> ret;
		return ret;
	}
	map<ll, int> mp;
	vector<ll> v(1, n);
	ll c=1;
	while(!v.empty()){
		ll x=v.back();
		v.pop_back();
		if(is_prime(x)){
			mp[x]++;
			continue;
		}
		ll p=rho(x, c);
		v.push_back(p);
		v.push_back(x/p);
	}
	vector<pair<ll, int>> ret;
	for(auto p:mp) ret.push_back(p);
	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;
		for(int i=0; i<e; i++){
			rp*=r;
			if(powmod(a, (p-1)/rp, p)!=1){
				break;
			}
			d*=r;
		}
	}
	return (p-1)/d;
}
ll root(ll p){
	vector<P> v=factorize(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