zoukankan      html  css  js  c++  java
  • BZOJ3884 上帝与集合的正确用法

    Description

    给定(p),求(2^{2^{2^{2^{2^{cdots}}}}}mod p)(pleq 10^7)

    Solution

    (f(p)=2^{2^{2^{2^{2^{cdots}}}}}mod p)。由于(b>phi(p))(a^bequiv a^{(b\,mod\,phi(p))+phi(p)}(mod;p))。所以(f(p)=2^{f(phi(p))+phi(p)}mod p)

    由于易证(phi(phi(p))leqfrac p2),所以时间为(O(log p))

    Code

    #include <cstdio>
    typedef long long LL;
    const int N = 10000050;
    int phi[N], pr[N / 10], cnt;
    int pow_mod(int a, int b, int p) {
      int ans = 1;
      for (; b; b >>= 1, a = (LL)a * a % p)
        if (b & 1) ans = (LL)ans * a % p;
      return ans;
    }
    int calc(int p) {
      // 2 ^ (2 ^ (...)) % p
      if (p == 1) return 0;
      return pow_mod(2, calc(phi[p]) + phi[p], p);
    }
    int main() {
      for (int i = 1; i < N; ++i) phi[i] = i;
      for (int i = 2; i < N; ++i) {
        if (phi[i] == i)
          --phi[pr[cnt++] = i];
        for (int j = 0; j < cnt && (LL)pr[j] * i < N; ++j) {
          if (i % pr[j])
            phi[i * pr[j]] = phi[i] * phi[pr[j]];
          else {
            phi[i * pr[j]] = phi[i] * pr[j];
            break;
          }
        }
      }
      int T, p;
      scanf("%d", &T);
      while (T--) {
        scanf("%d", &p);
        printf("%d
    ", calc(p));
      }
    }
    
  • 相关阅读:
    Python-Re正则表达式库
    杂记
    Python 信息提取-beautifulsoup实例
    Python 中国大学排名定向爬虫
    matlab-汉字unicode编码转换
    Python-beautifulsoup库
    python 安装resquest
    python--数字灯管
    Python time库
    Python random库
  • 原文地址:https://www.cnblogs.com/y-clever/p/8513943.html
Copyright © 2011-2022 走看看