quickly calc pow(i, n) since i in [1~n]

#include <bits/stdc++.h>
using namespace std;

#define inf (0x3f3f3f3f)
typedef long long int LL;
const int mod = 1e9 + 7;
const int maxn = 13000000 + 2;
LL f[maxn];
int prime[maxn];
bool check[maxn];
int n;

int qp(int a, int b) {
  LL result = 1;
  LL base = a;
  while (b) {
    if (b & 1) {
      result *= base;
      result %= mod;
    }
    b >>= 1;
    base *= base;
    base %= mod;
  }
  return result;
}

void init_prime() {
  f[1] = 1;
  int total = 0;
  for (int i = 2; i <= n; ++i) {
    if (!check[i]) {
      prime[++total] = i;
      f[i] = qp(i, n);
    }
    for (int j = 1; j <= total; ++j) {
      if (i * prime[j] > n) break;
      check[i * prime[j]] = true;
      f[i * prime[j]] = f[prime[j]] * f[i] % mod;
      if (i % prime[j] == 0) break;
    }
  }
}


void work() {
  scanf("%d
", &n);
  init_prime();
  LL result = 0;
  for (int i = 1; i <= n; ++i) {
    result ^= f[i];
  }
  printf("%lld
", result);
}


int main(int argc, char *argv[]) {
  // freopen("data.txt", "r", stdin);
  work();
  return 0;
}
View Code

https://ac.nowcoder.com/acm/contest/392/C

原文地址:https://www.cnblogs.com/liuweimingcprogram/p/10520420.html