QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#91936 | #144. Primitive root / 原根 | Minion# | 0 | 0ms | 0kb | C++23 | 2.3kb | 2023-03-29 22:37:14 | 2023-03-29 22:37:16 |
Judging History
answer
#include<cstdio>
#include<random>
#include<algorithm>
#define fo(i,x,y) for(int i = x;i <= y;++i)
#define ll long long
#define sub(x,y) (x < y ? x + n - y : x + y)
using namespace std;
ll n,f[70],g[70];
mt19937_64 gen(114514);
inline ll mul(ll x,ll y,ll n)
{
ll z = x * y - (ll)(1.0L * x * y / n) * n;
if(z > n) z -= n;
return z < 0 ? z + n : z;
}
ll ksm(ll x,ll y,ll n)
{
ll res = 1;
for(;y;y >>= 1,x = mul(x,x,n)) if(y & 1) res = mul(res,x,n);
return res;
}
bool wit(ll a,ll n)
{
ll u = n - 1,t = 0;
while(u + 1 & 1) u >>= 1,++t;
a = ksm(a,u,n);
fo(i,1,t)
{
ll A = mul(a,a,n);
if(A == 1) return a == 1 || a == n - 1;
a = A;
}
return a == 1;
}
bool mr(ll n)
{
if(n < 2) return 0;
if(n == 2) return 1;
fo(i,1,20)
{
ll a = gen() % (n - 2) + 2;
if(wit(a,n) == 0) return 0;
}
return 1;
}
inline ll F(ll x,ll c,ll n)
{
ll res = mul(x,x,n) + c;
return res < n ? res : res - n;
}
void factor(ll n)
{
if(n == 1) return;
if(n + 1 & 1) {f[++f[0]] = 2;return factor(n >> 1);}
if(mr(n)) {f[++f[0]] = n;return;}
while(1)
{
ll c = gen() % (n - 1) + 1,x = gen() % (n - 1) + 1,y = x,M = 1;
while(1)
{
fo(i,0,127)
{
x = F(x,c,n),y = F(F(y,c,n),c,n);
ll nM = mul(sub(x,y),M,n);
if(nM == 0) break;
M = nM;
}
ll g = __gcd(M,n);
if(g >= 2 && g <= n - 1) {factor(g),factor(n / g);return;}
if(x == y) break;
}
}
}
int main()
{
scanf("%lld",&n);
if(n == 2) return puts("1"),0;
else if(n == 4) return puts("3"),0;
else if(n == 1 || n % 4 == 0) return puts("-1"),0;
factor(n),sort(f + 1,f + f[0] + 1);
bool bz = 1;
fo(i,3,f[0]) if(f[i] != f[i - 1]) {bz = 0;break;}
if(bz == 0) return puts("-1"),0;
ll ph = n;
if(f[1] == 2)
{
ph >>= 1;
if(f[0] > 2) g[++g[0]] = f[2];
f[0] = 0,ph = ph / f[2] * (f[2] - 1);
factor(f[2] - 1);
}
else
{
if(f[0] > 1) g[++g[0]] = f[1];
f[0] = 0,ph = ph / f[1] * (f[1] - 1);
factor(f[1] - 1);
}
fo(i,1,g[0]) f[++f[0]] = g[i];
g[0] = 0,sort(f + 1,f + f[0] + 1),g[++g[0]] = f[1];
fo(i,2,f[0]) if(f[i] != f[i - 1]) g[++g[0]] = f[i];
fo(i,2,10000)
{
if(__gcd((ll)(i),n) > 1) continue;
bool bz = 1;
fo(j,1,g[0]) if(ksm(i,ph / g[j],n) == 1) {bz = 0;break;}
if(bz) return printf("%d\n",i),0;
}
}
詳細信息
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
433
output:
result:
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%