[bzoj3884]上帝与集合的正确用法——欧拉函数

题目大意

题解

出题人博客

代码

#include <bits/stdc++.h>
using namespace std;
const int M = 10001000;
int phi[M];
int Phi(int x) {
  int i, ret = x;
  for (i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      ret /= i;
      ret *= (i - 1);
      while (x % i == 0)
        x /= i;
    }
  }
  if (x ^ 1)
    ret /= x, ret *= x - 1;
  return ret;
}
int pow(long long x, int y, int p) {
  long long ret = 1;
  while (y) {
    if (y & 1)
      ret = (ret * x) % p;
    x = (x * x) % p;
    y >>= 1;
  }
  return ret;
}
int solve(int p) {
  if (p == 1)
    return 0;
  int tmp = 0;
  while (~p & 1)
    p >>= 1, ++tmp;
  int phi_p = Phi(p);
  int ret = solve(phi_p);
  (ret += phi_p - tmp % phi_p) %= phi_p;
  ret = pow(2, ret, p) % p;
  return ret << tmp;
}
int main() {
  int T, p;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &p);
    printf("%d\n", solve(p));
  }
}

posted on 2017-02-24 07:47  蒟蒻konjac  阅读(189)  评论(0编辑  收藏  举报