Description
求 22222..modp 。
Solution
欧拉定理:
an≡anmodφ(p)
(modp)
,
(gcd(n,p)=1)
EXT:
an≡anmodφ(p)+φ(p)
(modp)
这道题就一直递归地求解就行了,因为一个数一直求
φ
会在
O(logn)
的时间复杂度内变成1。
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10110;
typedef long long ll;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; bool sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
int test, p;
int Phi(int x) {
int phi = x, m = sqrt(x);
for (int i = 2; i <= m; i++)
if (x % i == 0) {
while (x % i == 0) x /= i;
phi /= i; phi *= i - 1;
}
if (x != 1) {
phi /= x; phi *= x - 1;
}
return phi;
}
inline int Pow(int a, int b, int p) {
int c = 1;
while (b) {
if (b & 1) c = (ll)c * a % p;
b >>= 1; a = (ll)a * a % p;
}
return c % p;
}
inline int Pow2(int p) {
if (p == 1) return 0;
int k = 0, phi, res;
while (~p & 1) {
p >>= 1; k++;
}
phi = Phi(p);
res = Pow2(phi);
res = (res + phi - k % phi) % phi;
res = Pow(2, res, p);
return res << k;
}
int main(void) {
freopen("1.in", "r", stdin);
read(test);
while (test--) {
read(p);
printf("%d\n", Pow2(p));
}
return 0;
}