Bzoj3884: 上帝与集合的正确用法

题面

传送门

Sol

公式$$
a^b\equiv
\begin{cases}
a^{b%\phi(p)}~gcd(a,p)=1\
a^b
gcd(a,p)\neq1,b<\phi(p)\
a^{b%\phi(p)+\phi(p)}
gcd(a,p)\neq1,b\geq\phi(p)
\end{cases}
~~~(mod~p)

\[ 递归处理$\varphi$,一个数取$\varphi$约$log$次就成$1$了,暴力搞就行了 ```cpp # include <bits/stdc++.h> # define RG register # define IL inline # define Fill(a, b) memset(a, b, sizeof(a)) using namespace std; typedef long long ll; const int _(1e7 + 1); IL ll Read(){ RG ll x = 0, z = 1; RG char c = getchar(); for(; c < '0' || c > '9'; c = getchar()){ if(c == '#') exit(0); z = c == '-' ? -1 : 1; } for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * z; } int m, prime[_], num, phi[_]; bool isprime[_]; IL void Sieve(){ isprime[1] = 1; phi[1] = 1; for(RG int i = 2; i < _; ++i){ if(!isprime[i]) prime[++num] = i, phi[i] = i - 1; for(RG int j = 1; j <= num && prime[j] * i < _; ++j){ isprime[i * prime[j]] = 1; if(i % prime[j]) phi[i * prime[j]] = phi[i] * (prime[j] - 1); else{ phi[i * prime[j]] = phi[i] * prime[j]; break; } } } } IL int Pow(RG ll x, RG ll y, RG ll p){ RG int flg2 = 0, flg1 = 0; RG ll cnt = 1; for(; y; y >>= 1){ if(y & 1) flg1 |= (cnt * x >= p || flg2), cnt = cnt * x % p; flg2 |= (x * x >= p); x = x * x % p; } return cnt + flg1 * p; } IL int Calc(RG int p){ if(p == 1) return Pow(2, 1, p); return Pow(2, Calc(phi[p]), p); } int main(RG int argc, RG char* argv[]){ Sieve(); for(RG int T = Read(); T; --T) m = Read(), printf("%d\n", Calc(m) % m); return 0; } ```\]

posted @ 2018-01-19 20:57  Cyhlnj  阅读(164)  评论(0编辑  收藏  举报