阶
阶
定义:满足 \(a^x \equiv1\;(mod\;m)\) 最小的 \(x\) 称为 \(a\) 模 \(m\) 的阶
性质:\(x\mid \phi(m)\)
求阶:
先将 \(\phi( m)\) 标准分解,\(phi(m)=p_1^{e_1}*p_2^{e_2}*...p_k^{e_k}\)
先设阶为 \(d= \phi(m)\), 每次除以一个素因子 \(p_i\)
当且仅当 \(p_i\mid d\) 且 \(a^{\frac d{p_i}} \equiv1\;(mod\;m)\) 时 \(d\) 可再除以一个 \(p_i\)
int solve(int a)
{
int d = p - 1;
for (int i = 1; i <= t; i++)
while(d % divisor[i] == 0 && qmi(a, d / divisor[i], p) == 1) d /= divisor[i];
return d;
}
阶 - 题目 - Daimayuan Online Judge
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 100;
int p, t;
int divisor[N];
ll qmi(ll a, ll b, ll p)
{
ll ans = 1;
while(b)
{
if (b & 1) ans = ans * a % p;
b >>= 1;
a = a * a % p;
}
return ans % p;
}
int solve(int a)
{
int d = p - 1;
for (int i = 1; i <= t; i++)
while(d % divisor[i] == 0 && qmi(a, d / divisor[i], p) == 1) d /= divisor[i];
return d;
}
int main()
{
int T;
scanf("%d%d", &p, &T);
int m = p - 1;
for (int i = 2; i <= m / i; i++)
{
if (m % i) continue;
divisor[++t] = i;
while(m % i == 0) m /= i;
}
if (m > 1) divisor[++t] = m;
while(T--)
{
int a;
scanf("%d", &a);
printf("%d\n", solve(a));
}
return 0;
}