码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-03-13 00:17:04      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:one   fine   names   view   const   long   lld   init   opened   

技术图片
#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", &n);
  init_prime();
  LL result = 0;
  for (int i = 1; i <= n; ++i) {
    result ^= f[i];
  }
  printf("%lld\n", 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

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

标签:one   fine   names   view   const   long   lld   init   opened   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!