#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2147483647
#define LL long long
#define clr(x) memset(x, 0, sizeof x)
#define digit (ch < '0' || ch > '9')
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
template <class T> inline void read(T &x) {
int flag = 1; x = 0;
register char ch = getchar();
while( digit) { if(ch == '-') flag = -1; ch = getchar(); }
while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
x *= flag;
}
const int maxm = 1e7+9;
int phi[maxm],t,p;
int Phi(int x) {
int re = x;
for(int i = 2; i*i <= x; i++) if(!(x%i)) {
re /= i; re *= i-1;
while(!(x%i)) x /= i;
}
if(x^1) re /= x, re *= x-1;
return re;
}
int powermod(int y, int p) {
LL tmp = 2, ans = 1;
while(y) {
if(y&1) ans = (ans*tmp)%p;
tmp = (tmp*tmp)%p; y >>= 1;
}
return ans;
}
int solve(int p) {
if(p == 1) return 0;
int tmp = 0;
while(~p&1) p >>= 1, ++tmp;
int phi_p = Phi(p), ans = solve(phi_p);
ans = (ans+phi_p-tmp%phi_p)%phi_p;
ans = powermod(ans, p)%p;
return ans<<tmp;
}
int main() {
read(t);
while(t--) read(p), printf("%d\n", solve(p));
return 0;
}
【BZOJ3884】【欧拉函数】上帝与集合的正确用法 题解
最新推荐文章于 2019-04-17 16:37:19 发布