QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#481773 | #144. Primitive root / 原根 | ucup-team1004 | 0 | 0ms | 4184kb | C++17 | 1.9kb | 2024-07-17 14:05:19 | 2024-07-17 14:05:19 |
answer
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include"debug.h"
#else
#define debug(...) void()
#endif
#define all(x) (x).begin(),(x).end()
template<class T>
auto ary(T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using LL=__int128;
vector<int>test={2,3,5,7,11,13,17,19,23,29,31,37};
ll mod;
ll qpow(LL x,ll y,ll mod,LL ans=1){
for(;y;(x*=x)%=mod,y>>=1)if(y&1)(ans*=x)%=mod;
return ans;
}
bool isprime(ll n){
if(n<=2)return n==2;
if(n%2==0)return 0;
ll a=n-1;
int k=0;
for(;a%2==0;a/=2)k++;
auto chk=[&](int p){
p%=n;
if(p==0)return 1;
ll x=qpow(p,a,n);
if(x==1)return 1;
for(int i=0;i<k;i++){
if(x==n-1)return 1;
x=(LL)x*x%n;
}
return 0;
};
for(int x:test)if(!chk(x))return 0;
return 1;
}
vector<ll>p;
mt19937_64 rnd(time(0));
ll find(ll n){
ll x=rnd()%(n-2)+1;
auto f=[&](ll a){
return ((LL)a*a+x)%n;
};
ll a=rnd()%n,b=f(a);
for(;a!=b;a=f(a),b=f(f(b))){
ll x=__gcd(abs(a-b),n);
if(x>1)return x;
}
return n;
}
void divide(ll n){
if(isprime(n))return p.push_back(n);
ll x;
do x=find(n);while(x==n);
divide(x),divide(n/x);
}
ll phi;
bool chk(ll x){
if(__gcd(x,mod)>1)return 0;
for(ll y:p){
if(qpow(x,phi/y,mod)==1)return 0;
}
return 1;
}
int main(){
cin>>mod;
if(mod==2)puts("1"),exit(0);
if(mod==4)puts("3"),exit(0);
divide(mod),sort(all(p));
if(p[0]==2){
for(int i=1;i<p.size();i++){
if(p[i]!=p[1])puts("-1"),exit(0);
}
p.erase(p.begin());
ll x=p.back()-1;
p.pop_back();
divide(x);
}else{
for(ll x:p){
if(x!=p.back())puts("-1"),exit(0);
}
ll x=p.back()-1;
p.pop_back();
divide(x);
}
phi=1;
for(ll x:p)phi*=x;
sort(all(p)),p.erase(unique(all(p)));
for(ll g=1;g<mod;g++){
if(chk(g))printf("%lld\n",g),exit(0);
}
puts("-1");
return 0;
}
#ifdef DEBUG
#include"debug.hpp"
#endif
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 1
Accepted
time: 0ms
memory: 3772kb
input:
433
output:
5
result:
ok good solution
Test #2:
score: 0
Accepted
time: 0ms
memory: 4088kb
input:
197
output:
2
result:
ok good solution
Test #3:
score: 0
Accepted
time: 0ms
memory: 3920kb
input:
733
output:
6
result:
ok good solution
Test #4:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
859
output:
2
result:
ok good solution
Test #5:
score: 0
Accepted
time: 0ms
memory: 4184kb
input:
449
output:
3
result:
ok good solution
Test #6:
score: 0
Accepted
time: 0ms
memory: 3772kb
input:
263
output:
5
result:
ok good solution
Test #7:
score: -1
Wrong Answer
time: 0ms
memory: 3884kb
input:
683
output:
2
result:
wrong answer wrong solution
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Skipped
Dependency #2:
0%
Subtask #6:
score: 0
Skipped
Dependency #3:
0%
Subtask #7:
score: 0
Skipped
Dependency #4:
0%
Subtask #8:
score: 0
Skipped
Dependency #5:
0%
Subtask #9:
score: 0
Skipped
Dependency #6:
0%
Subtask #10:
score: 0
Skipped
Dependency #9:
0%