Skip to content

Commit

Permalink
求解原根
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-XYS committed Jan 19, 2017
1 parent abe387d commit 7803ba6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Primitive-Root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <cstdio>

typedef long long ll;

using namespace std;

ll p;

ll qpow(ll x, ll y)
{
if (y == 1) return x;
ll t = qpow(x, y >> 1);
t = t * t % p;
if ((y & 1) == 0) return t;
return t * x % p;
}

int main()
{
ll x;
while (true)
{
scanf("%lld", &p);
for (ll k = 2; ; k++)
{
x = p - 1;
for (ll i = 2; i * i <= x; i++)
{
if (x % i == 0)
{
if (qpow(k, (p - 1) / i) == 1) goto NEXT;
while (x % i == 0) x /= i;
}
}
if (x != 1)
{
if (qpow(k, (p - 1) / x) == 1) goto NEXT;
}
printf("%lld\n", k);
break;
NEXT:;
}
}
return 0;
}

0 comments on commit 7803ba6

Please sign in to comment.