BZOJ3884【数论】

来源:互联网 发布:电脑桌面有什么软件 编辑:程序博客网 时间:2024/06/06 00:11
/* I will wait for you*/#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <algorithm>#include <iostream>#include <fstream>#include <vector>#include <queue>#include <deque>#include <map>#include <set>#include <string>#define make make_pair#define fi first#define se secondusing namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> pii;const int maxn = 10000010;const int maxm = 1010;const int maxs = 26;const int inf = 0x3f3f3f3f;const int P = 1000000007;const double error = 1e-9;inline int read(){int x = 0, f = 1;char ch = getchar();while (ch <= 47 || ch >= 58)f = (ch == 45 ? -1 : 1), ch = getchar();while (ch >= 48 && ch <= 57)x = x * 10 + ch - 48, ch = getchar();return x * f;}int pri[maxn], in[maxn], fai[maxn];void init(){memset(pri, inf, sizeof pri);fai[1] = pri[1] = 1;for (int i = 2; i * i < maxn; i++)if (!in[i]) {pri[i] = i;for (int j = i * i; j < maxn; j += i)       in[j] = 1, pri[j] = min(pri[j], i);}for (int i = 2; i < maxn; i++) {if (!in[i])fai[i] = i - 1;else {if (pri[i] == pri[i / pri[i]])fai[i] = fai[i / pri[i]] * pri[i];elsefai[i] = fai[i / pri[i]] * (pri[i] - 1);}}}int t;int qpow(int n, int p){ll ans = 1;for (ll a = 2; n; (a *= a) %= p, n /= 2)(n & 1) ? (ans *= a) %= p : 0;return ans;}int solve(int n){if (n == 1)return 0;int k = 0, p = n;for (; p % 2 == 0; k++, p /= 2);ll tmp = solve(fai[p]) - k;while (tmp < 0)tmp += fai[p];tmp = n / p * qpow(tmp, p) % n;return (int) tmp;}int main(){t = read(), init();while (t--) {int n = read(), m = solve(n);printf("%d\n", m);}return 0;}

0 0
原创粉丝点击