【欧拉降幂】Super_log
In Complexity theory, some functions are nearly O(1)O(1), but it is greater then O(1)O(1). For example, the complexity of a typical disjoint set is O(nα(n))O(nα(n)). Here α(n)α(n) is Inverse Ackermann Function, which growth speed is very slow. So in practical application, we often assume α(n) \le 4α(n)≤4.
However O(α(n))O(α(n)) is greater than O(1)O(1), that means if nn is large enough, α(n)α(n) can greater than any constant value.
Now your task is let another slowly function log*log∗ xx reach a constant value bb. Here log*log∗ is iterated logarithm function, it means “the number of times the logarithm function iteratively applied on xx before the result is less than logarithm base aa”.
Formally, consider a iterated logarithm function log_{a}^*loga∗
Find the minimum positive integer argument xx, let log_{a}^* (x) \ge bloga∗(x)≥b. The answer may be very large, so just print the result xx after mod mm.
Input
The first line of the input is a single integer T(T\le 300)T(T≤300) indicating the number of test cases.
Each of the following lines contains 33 integers aa , bb and mm.
1 \le a \le 10000001≤a≤1000000
0 \le b \le 10000000≤b≤1000000
1 \le m \le 10000001≤m≤1000000
Note that if a==1, we consider the minimum number x is 1.
Output
For each test case, output xx mod mm in a single line.
Hint
In the 4-th4−th query, a=3a=3 and b=2b=2. Then log_{3}^* (27) = 1+ log_{3}^* (3) = 2 + log_{3}^* (1)=3+(-1)=2 \ge blog3∗(27)=1+log3∗(3)=2+log3∗(1)=3+(−1)=2≥b, so the output is 2727 mod 16 = 1116=11.
样例输入复制
5
2 0 3
3 1 2
3 1 100
3 2 16
5 3 233
样例输出复制
1
1
3
11
223
题解:求a^a^...(b次)%n的结果。因为n与a不一定互质,所以要利用广义欧拉定理进行降幂。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<ctime>
#include<stack>
using namespace std;
#define mm(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5 + ;
#define inf 0x3f3f3f3f
const double PI = acos(-1.0); ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);} #define Mod(a,b) a<b?a:a%b+b //根据欧拉定理重定义mod ll fpow(ll a,ll n,ll mod)
{
ll res=;
while(n)
{
if(n&) res=Mod(res*a,mod);
a=Mod(a*a,mod);
n>>=;
}
return res;
} ll phi(ll x) //求x的欧拉函数
{
ll ans=x,tp=sqrt(x);
for(ll i=;i<=tp;++i)
{
if(x%i==)
{
ans=ans-ans/i;
while(x%i==) x/=i;
}
}
if(x>) ans=ans-ans/x;
return ans;
} ll solve(ll a,ll b,ll m)
{
if(m==) return ;
if(b<=) return fpow(a,b,m);
ll p=phi(m);
ll t=solve(a,b-,p); //递归求解
ll g=gcd(a,m);
if(g==||b<p) return fpow(a,t,m);
else return fpow(a,t+p,m);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ll a,b,m;
scanf("%lld %lld %lld",&a,&b,&m);
ll ans=solve(a,b,m)%m;
printf("%lld\n",ans);
}
return ;
}
【欧拉降幂】Super_log的更多相关文章
- 2019ICPC网赛南京站B题 super_log(欧拉降幂
https://nanti.jisuanke.com/t/41299 题意:让算a^(a^(a^(...))),一共b个a, (mod p)的结果. 思路:这是个幂塔函数,用欧拉降幂公式递归求解. # ...
- hdu4549 矩阵快速幂 + 欧拉降幂
R - M斐波那契数列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- bzoj3884: 上帝与集合的正确用法 欧拉降幂公式
欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert ...
- Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
- D - Power Tower欧拉降幂公式
题意:给你一个数组a,q次查询,每次l,r,要求 \(a_{l}^{a_{l+1}}^{a_{l+2}}...{a_r}\) 题解:由欧拉降幂可知,最多log次eu(m)肯定变1,那么直接暴力即可,还 ...
- Applese涂颜色-欧拉降幂公式
链接:https://ac.nowcoder.com/acm/contest/330/E来源:牛客网 题目描述 精通程序设计的 Applese 叕写了一个游戏. 在这个游戏中,有一个 n 行 m 列的 ...
- TOJ 3151: H1N1's Problem(欧拉降幂)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3151 时间限制(普通/Java): ...
- HDU4704(SummerTrainingDay04-A 欧拉降幂公式)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submi ...
- 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]
题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...
- HDU 2814 斐波那契循环节 欧拉降幂
一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节.数据范围出的很那啥..unsigned long long注意用防爆的乘法 /** @Date : 2017-09- ...
随机推荐
- http状态码 500-599
类比: 客户端:客人 服务器:便利店 http报文:中文语言+钱 500:服务器内部错误,无法完成请求 客户端:给我一瓶可乐 服务器:对不起,不能给你服务,本店昨天起火烧了 501:服务器不支持请求的 ...
- 【iOS】更换证书遇到的问题
今天给一个项目换证书的时候遇到了这个问题: Code Sign error: Provisioning profile does not match bundle identifier: The pr ...
- Struts完成用户新增操作
点击新增客户出现该页面并完成前后台交互 代码逻辑分析: jsp 页面部分代码 <TABLE id=table_1 style="DISPLAY: none" cellSpac ...
- spark shuffle的写操作之准备工作
前言 在前三篇文章中,spark 源码分析之十九 -- DAG的生成和Stage的划分 剖析了DAG的构建和Stage的划分,spark 源码分析之二十 -- Stage的提交 剖析了TaskSet任 ...
- 二叉查找树(查找、插入、删除)——C语言
二叉查找树 二叉查找树(BST:Binary Search Tree)是一种特殊的二叉树,它改善了二叉树节点查找的效率.二叉查找树有以下性质: (1)若左子树不空,则左子树上所有节点的值均小于它的根节 ...
- java8(一)Lambda表达式
其实很久前已经学习过了Lambda表达式,但是学习后没有多少使用的机会,久而久之也就忘记(惭愧).最近新的项目用的jdk8所以准备再学习一次,写下文章也是为了记录,方便以后再忘的时候,不用到处找资料( ...
- 基于 kubeadm 部署单控制平面的 k8s 集群
单控制平面不符合 HA 要求,但用于开发/测试环境不会有任何问题,如果资源足够的话(10台以上服务器,3台用于APIserver.3台用于 etcd 存储.至少3台用于工作节点.1台作为负载均衡),可 ...
- Java学习|HTTP请求头
https://www.cnblogs.com/honghong87/articles/6941436.html 常见http请求报文头属性 Accept:告诉服务端,客户端接受什么类型的响 ...
- 洛谷 P4124 [CQOI2016]手机号码
题意简述 求l~r之间不含前导零,至少有三个相邻的相同数字,不同时含有4和8的11位正整数的个数 题解思路 数位DP,注意在l,r位数不够时补至11位 代码 #include <cstdio&g ...
- vscode 支持 threejs 的智能提示
VSCode Typings and Intellisense: Dummy Learning VS-Code 1 Jun 20, 2016 Updated on Jun 20 2016 for 1. ...