\(\text{Description}\)

  • \(\text{Given a number }p(p\leqslant10^7).\)
  • \(\text{Output }2^{2^{2^{2^{\cdots}}}}\bmod p.\)

\(\text{Method}\)

\(\text{Use ex-Euler's Theorem}\quad b\geqslant\varphi(m)\Rightarrow a^b\equiv a^{b\bmod\varphi(m)+\varphi(m)}\pmod{m}.\)

\(\text{Let }x=2^{2^{2^{2^{\cdots}}}}.\)

\[\begin{aligned}x\bmod p& =2^x\bmod p\\& =2^{x\bmod \varphi(p)+\varphi(p)}\bmod p\\& =2^{2^x\bmod \varphi(p)+\varphi(p)}\bmod p\\&=\cdots\end{aligned}
\]

\(\text{Use recursion algorithm.Let }f(i)=x\bmod i.\)

\[f(i)=\begin{cases}0&i=1\\2^{f(\varphi(i))+\varphi(i)}\bmod i&i>1\end{cases}
\]

\(\text{Code}\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int qmul(int a,int b,int mod)
{
if(a==0||b==0||mod==1ll)return 0;
if(b==1ll)return a%mod;
int ans=qmul(a,b/2ll,mod);
ans+=ans,ans%=mod;
if(b%2ll)ans+=a,ans%=mod;
return ans;
}
int qpow(int a,int b,int mod)
{
if(a==0||mod==1ll)return 0;
if(b==0)return 1ll;
int ans=qpow(a,b/2ll,mod);
ans=qmul(ans,ans,mod),ans%=mod;
if(b%2ll)ans=qmul(ans,a,mod),ans%=mod;
return ans;
}
int v[10000010],prime[10000010],phi[10000010];
void lineareuler(int n)
{
memset(v,0,sizeof(v));
int cnt=0;
for(int i=2;i<=n;i++)
{
if(v[i]==0)
{
v[i]=i;
prime[++cnt]=i;
phi[i]=i-1;
}
for(int j=1;j<=cnt;j++)
{
if(prime[j]>v[i]||prime[j]>n/i)break;
v[i*prime[j]]=prime[j];
phi[i*prime[j]]=phi[i]*(i%prime[j]?prime[j]-1:prime[j]);
}
}
return;
}
int calc(int xx)
{
if(xx==1)return 0;
else return qpow(2,calc(phi[xx])+phi[xx],xx);
}
int t,p;
int main()
{
scanf("%d",&t);
lineareuler(10000000);
for(int qwerty=1;qwerty<=t;qwerty++)
{
scanf("%d",&p);
printf("%d\n",calc(p));
}
return 0;
}

[luoguP4139]上帝与集合的正确用法的更多相关文章

  1. 【BZOJ3884】上帝与集合的正确用法(欧拉定理,数论)

    [BZOJ3884]上帝与集合的正确用法(欧拉定理,数论) 题面 BZOJ 题解 我们有欧拉定理: 当\(b \perp p\)时 \[a^b≡a^{b\%\varphi(p)}\pmod p \] ...

  2. 洛谷P4139 上帝与集合的正确用法 [扩展欧拉定理]

    题目传送门 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”. ...

  3. 扩展欧拉定理【洛谷P4139】 上帝与集合的正确用法

    P4139 上帝与集合的正确用法 \(2^{2^{2^{\dots}}}\bmod p\) 卡最优解倒数第一祭. 带一下扩展欧拉定理就好了. code: #include <iostream&g ...

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

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

  5. bzoj 3884 上帝与集合的正确用法 指数循环节

    3884: 上帝与集合的正确用法 Time Limit: 5 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description   根据一些 ...

  6. 洛谷 P4139 上帝与集合的正确用法 解题报告

    P4139 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做"元". 第二天, 上帝创造了一个新 ...

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

    上帝与集合的正确用法 [问题描述] [输入格式] 第一行一个T,接下来T行,每行一个正整数p,代表你需要取模的值. [输出格式] T行,每行一个正整数,为答案对p取模后的值. [样例输入] 3236 ...

  8. 题解-洛谷P4139 上帝与集合的正确用法

    上帝与集合的正确用法 \(T\) 组数据,每次给定 \(p\),求 \[\left(2^{\left(2^{\left(2^{\cdots}\right)}\right)}\right)\bmod p ...

  9. 【数学】[BZOJ 3884] 上帝与集合的正确用法

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

随机推荐

  1. 【LeetCode】790. Domino and Tromino Tiling 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/domino-a ...

  2. Probabilistic Principal Component Analysis

    目录 引 主要内容 EM算法求解 附录 极大似然估计 代码 Tipping M E, Bishop C M. Probabilistic Principal Component Analysis[J] ...

  3. 【Azure 应用服务】探索在Azure上设置禁止任何人访问App Service的默认域名(Default URL)

    问题描述 总所周知,Azure App Service服务会默认提供一个 ***.chinacloudsites.cn为后缀的域名,但是该域名由上海蓝云网络科技有限公司备案,仅用于向其客户提供 Azu ...

  4. uniapp使用uni.openDocument打开文件时,安卓打开成功,iOS打开失败【原因:打开的文件的文件名是中文】

    解决办法:使用escape进行文件名编码 uni.downloadFile({ url: url, success: function(res) { var filePath = res.tempFi ...

  5. 编写Java程序,使用日期处理类实现日期的格式化输出

    返回本章节 返回作业目录 需求说明: 按"yyyy-MM-dd"格式输入一个字符串型日期,然后输出这个日期为本年中的第几周. 实现思路: 使用SimpleDateFormat格式化 ...

  6. playwright--自动化(一):快速上手

    Playwright为现代 Web 应用程序提供可靠的端到端测试. 在JavaScript 和 TypeScript.Python..NET和Java 中都可以使用 Playwright 本人选择py ...

  7. 疯狂的类构造器Builder模式,链式调用

    疯狂的类构造器 最近栈长在做 Code Review 时,发现一段创建对象的方法: Task task = new Task(112, "紧急任务", "处理一下这个任务 ...

  8. DES对称加密算法实现:Java,C#,Golang,Python

    数据加密标准(DES,Data Encryption Standard,简写DES)是一种采用块密码加密的对称密钥算法,加密分组长度为64位,其中8.16.24.32.40.48.56.64 等8位是 ...

  9. Gitlab如何进行备份恢复与迁移?

    https://blog.csdn.net/ouyang_peng/article/details/77070977 1.Gitlab 创建备份 1.1 创建备份文件 首先我们得把老服务器上的Gitl ...

  10. Centos7 selinux关闭

    getenforce ---查看selinux服务是否开启 setenforce 0|1 0:Permissive 1:Enforcing 上面使用setenforce是临时的效果 永久关闭方法: v ...