题目

对于100%的数据,T<=1000,p<=10^7

题解

来捉这道神题

欧拉定理的一般形式:

\[a^{m} \equiv a^{m \mod \varphi(p) + [m \ge \varphi(p)]\varphi(p)} \pmod p
\]

我们令

\[ans(p) = 2^{2^{2^{...}}} \mod p
\]

那么有

\[ans(p) = 2^{ans(\varphi(p)) + \varphi(p)} \mod p
\]

\(O(\log p)\)递归即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<bitset>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 10000005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
bitset<maxn> isn;
int p[maxn],phi[maxn],pi;
void init(){
phi[1] = 1;
for (int i = 2; i <= 10000000; i++){
if (!isn[i]) p[++pi] = i,phi[i] = i - 1;
for (int j = 1; j <= pi && i * p[j] <= 10000000; j++){
isn[i * p[j]] = true;
if (i % p[j] == 0){
phi[i * p[j]] = phi[i] * p[j];
break;
}
phi[i * p[j]] = phi[i] * (p[j] - 1);
}
}
}
int qpow(int a,int b,int p){
int ans = 1;
for (; b; b >>= 1,a = 1ll * a * a % p)
if (b & 1) ans = 1ll * ans * a % p;
return ans;
}
int Ans(int p){
if (p == 1) return 0;
return qpow(2,Ans(phi[p]) + phi[p],p);
}
int main(){
init();
int T = read(),p;
while (T--){
p = read();
printf("%d\n",Ans(p));
}
return 0;
}

BZOJ3884 上帝与集合的正确用法 【欧拉定理】的更多相关文章

  1. 【BZOJ3884】上帝与集合的正确用法 [欧拉定理]

    上帝与集合的正确用法 Time Limit: 5 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description Input 第一行一个T ...

  2. BZOJ3884: 上帝与集合的正确用法 拓展欧拉定理

    Description   根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...

  3. BZOJ3884: 上帝与集合的正确用法(欧拉函数 扩展欧拉定理)

    Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 3860  Solved: 1751[Submit][Status][Discuss] Descripti ...

  4. bzoj3884: 上帝与集合的正确用法 扩展欧拉定理

    题意:求\(2^{2^{2^{2^{...}}}}\%p\) 题解:可以发现用扩展欧拉定理不需要很多次就能使模数变成1,后面的就不用算了 \(a^b\%c=a^{b\%\phi c} gcd(b,c) ...

  5. bzoj3884上帝与集合的正确用法

    Description   根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...

  6. [BZOJ3884] 上帝与集合的正确用法 (欧拉函数)

    题目链接:  https://www.lydsy.com/JudgeOnline/problem.php?id=3884 题目大意: 给出 M, 求 $2^{2^{2^{2^{...}}}}$ % M ...

  7. bzoj3884 上帝与集合的正确用法

    a^b mod P=a^(b mod phi(p)) mod p,利用欧拉公式递归做下去. 代码 #pragma comment(linker,"/STACK:1024000000,1024 ...

  8. bzoj3884: 上帝与集合的正确用法 欧拉降幂公式

    欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert ...

  9. bzoj千题计划264:bzoj3884: 上帝与集合的正确用法

    http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...

随机推荐

  1. GB MB KB B 关系

    1KB=1024Bytes=2的10次方Bytes 1MB=1024KB=2的20次方Bytes 1GB=1024MB=2的30次方Bytes 1TB=1024GB=2的40次方Bytes

  2. Oracle 数据处理

    1. 对维度按照度量值的排名进行统计得分,第一名100分,第二名99分,第三名98……可以先进行排名,然后用 得分值+1,减去排名既是所得分数. -- 建表 create table province ...

  3. react属性校验

    https://reactjs.org/docs/typechecking-with-proptypes.html 1.安装:cnpm i prop-types -S import PropTypes ...

  4. Uva 填充正方形

    暴力出奇迹 #include<iostream> #include<cstdio> using namespace std; +; int T,n; char S[maxn][ ...

  5. 设计模式基础--Java接口和抽象类

    最近在看设计模式,感觉需要先好好区分下抽象类和接口. 一.抽象类 <Java编程思想>中这样定义:包含抽象方法的类叫做抽象类. 解释: 1.包含,说明抽象类中可以有其他的具体方法. 2.因 ...

  6. 破解studio 3T

    方法一: 打开注册表:regedit 计算机\HKEY_CURRENT_USER\Software\JavaSoft\Prefs\3t\mongochef\enterprise 将里面得数据清零,又是 ...

  7. React学习记录一

    半路出家直接上手React,其实有点吃力,所以开始研究create-react-app,从这里下手吧. create-react-app 官方网站:https://github.com/faceboo ...

  8. Ubuntu设置代理上网

    代理服务器(Proxy Server)是个人网络和Internet服务商之间的中间代理机构,它负责转发合法的网络信息,对转发进行控制和登记.代理服务器作为连接Internet(广域网)与Intrane ...

  9. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

  10. 实验一 查看CPU和内存,用机器指令和汇编指令编程

    (1):使用debug,将下面的程序段写入内存,逐条执行,观察每条指令执行后,CPU中相关寄存器中内存的变化. 机器码        汇编指令 b8 20 4e     mov ax,4E20H 05 ...