[luoguP4139]上帝与集合的正确用法
\(\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}}}}.\)
\]
\(\text{Use recursion algorithm.Let }f(i)=x\bmod i.\)
\]
\(\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]上帝与集合的正确用法的更多相关文章
- 【BZOJ3884】上帝与集合的正确用法(欧拉定理,数论)
[BZOJ3884]上帝与集合的正确用法(欧拉定理,数论) 题面 BZOJ 题解 我们有欧拉定理: 当\(b \perp p\)时 \[a^b≡a^{b\%\varphi(p)}\pmod p \] ...
- 洛谷P4139 上帝与集合的正确用法 [扩展欧拉定理]
题目传送门 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”. ...
- 扩展欧拉定理【洛谷P4139】 上帝与集合的正确用法
P4139 上帝与集合的正确用法 \(2^{2^{2^{\dots}}}\bmod p\) 卡最优解倒数第一祭. 带一下扩展欧拉定理就好了. code: #include <iostream&g ...
- 【BZOJ3884】上帝与集合的正确用法 [欧拉定理]
上帝与集合的正确用法 Time Limit: 5 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description Input 第一行一个T ...
- bzoj 3884 上帝与集合的正确用法 指数循环节
3884: 上帝与集合的正确用法 Time Limit: 5 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 根据一些 ...
- 洛谷 P4139 上帝与集合的正确用法 解题报告
P4139 上帝与集合的正确用法 题目描述 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做"元". 第二天, 上帝创造了一个新 ...
- BZOJ 3384 上帝与集合的正确用法
上帝与集合的正确用法 [问题描述] [输入格式] 第一行一个T,接下来T行,每行一个正整数p,代表你需要取模的值. [输出格式] T行,每行一个正整数,为答案对p取模后的值. [样例输入] 3236 ...
- 题解-洛谷P4139 上帝与集合的正确用法
上帝与集合的正确用法 \(T\) 组数据,每次给定 \(p\),求 \[\left(2^{\left(2^{\left(2^{\cdots}\right)}\right)}\right)\bmod p ...
- 【数学】[BZOJ 3884] 上帝与集合的正确用法
Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“元” ...
随机推荐
- 【LeetCode】930. Binary Subarrays With Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 字典 相似题目 参考资料 日期 题目地址: ...
- Variational Autoencoders and Nonlinear ICA: A Unifying Framework
目录 概 主要内容 本文的模型 Identifiability Khemakhem I., Kingma D. P., Monti R. P. and Hyv"{a}rinen A. Var ...
- [opencv]白平衡算法中的灰度世界法,改善图像发红发蓝发绿的现象
#include<iostream> #include <opencv2/opencv.hpp> #include <math.h> using namespace ...
- C#自定义转换(implicit 或 explicit)
C#的类型转换分为显式转换和隐式转换,显式转换需要自己声明转换类型,而隐式转换由编译器自动完成,无需我们声明,如: //long需要显式转换成int long l = 1L; int i = (int ...
- [Azure DevOps] 管理测试计划、测试套件和测试用例
我喜欢测试计划,它能让团队清楚测试进度,还能妥善分配测试人员,更重要的是它能保证测试质量和效率.Azure DevOps 里提供了 Test Plans 这个模块用于管理测试计划. 1. Azure ...
- golang mongodb 驱动二次封装
mongodb 官方的go驱动包 go.mongodb.org/mongo-driver 使用起来比较繁琐,最近对其进行了二次封装 github地址:https://github.com/w3liu/ ...
- MongoDB分片设计
#### 如何做好分片集群 * 合理的架构 * 是否需要分片? * 要分多少片? * 数据分布规则? * 正确的姿势 * 选择需要分片的表 * 选择正确的片键 * 使用合适的均衡策略 * 足够的资源 ...
- vsconde launch.json配置 调试本地文件
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing ...
- kubeadm 安装Kubernetes 1.16.3 (CentOS7+IPVS+Calico)
目录 · . 一.更新系统内核(全部节点) · . 二.基础环境设置(全部节点) · . 1.修改 Host · . 2.修改 Hostname · . 3.主机时间同步 · . 4.关闭 ...
- [转]Vue之引用第三方JS插件
1.绝对路径引入,全局使用. 在index.html文件中使用script标签引入插件. 该种方式就是上面演示ckplayer插件使用的方式. 备注: 这种方式的引用,会在开启ESLint时,报错,可 ...