BZOJ 3884: 上帝与集合的正确用法

欧拉降幂与广义欧拉降幂
\[a^x = \begin{cases} a^{x \bmod \varphi(p)} && \gcd(a,p)=1 \\ a^x && \gcd(a,p)\neq 1 \wedge x < \varphi(p) \\ a^{x \bmod \varphi(p) + \varphi(p)} && \gcd(a,p)\neq 1 \wedge x \geq \varphi(p) \end{cases}\]
\(f(p)=2^{2^{2^{...}}} \pmod p\)
那么 \(f(p) = 2^{f( \varphi(p) )+ \varphi(p)} \pmod p\)

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) >> 1)
#define lowbit(i) ((i) & (-i))
#define ll long long
#define ull unsigned long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[N*2],ne[N*2];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[N*2],ne[N*2],c[N*2];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2, int mod = MOD) {int ans = 1; for (; b; a = 1LL * a * a % mod, b >>= 1)if (b & 1)ans = 1LL * ans * a % mod; return ans % mod;}
int gcd(int a, int b) { while (b) { a %= b; std::swap(a, b); } return a; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
    int x = 0, f = 1; char ch = getc();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
    while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
    return x * f;
}

struct HashTable {
    static const int mod = 1e6 + 7;
    int head[mod + 7], cnt;
    struct E { int x, ne, ans; } e[mod + 7];
    void ins(int x, int ans) {
        int u = x % mod;
        e[++cnt].x = x; e[cnt].ne = head[u]; e[cnt].ans = ans; head[u] = cnt;
    }
    int query(int x) {
        int u = x % mod;
        for (int i = head[u]; i; i = e[i].ne) if (e[i].x == x) return e[i].ans;
        return -1;
    }
} H;

int phi(int n) {
    int ans = n;
    for (int i = 2; i * i <= n; i++) {
        if (n % i) continue;
        ans = ans - ans / i;
        while (n % i == 0) n /= i;
    }
    if (n > 1) ans = ans - ans / n;
    return ans;
}

int f(int p) {
    if (p <= 1) return 0;
    int res = H.query(p);
    if (~res) return res;
    int pp = phi(p);
    res = qp(2, f(pp) + pp, p);
    H.ins(p, res);
    return res;
}

int main() {
    int T = _();
    while (T--) {
        int x = _();
        printf("%d\n", f(x));
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Mrzdtz220/p/12380267.html