TOJ 3151: H1N1's Problem(欧拉降幂)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3151
时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
描述
H1N1 like to solve acm problems.But they are very busy, one day they meet a problem. Given three intergers a,b,c, the task is to compute a^(b^c))%317000011. 1412, ziyuan and qu317058542 don't have time to solve it, so the turn to you for help.
输入
The first line contains an integer T which stands for the number of test cases. Each case consists of three integer a, b, c seperated by a space in a single line. 1 <= a,b,c <= 100000
输出
For each case, print a^(b^c)%317000011 in a single line.
样例输入
2
1 1 1
2 2 2
样例输出
1
16
思路:
直接暴力用欧拉降幂2次来做的
欧拉降幂公式:
A^B%C=A^( B%Phi[C] + Phi[C] )%C (B>=Phi[C])
数学方面的证明可以去:http://blog.csdn.net/Pedro_Lee/article/details/51458773 学习
注意第一次降幂的时候Mod值取的是317000011的欧拉函数值
恩,这样用时是600MS,耗时还是很高的。
其实因为317000011是质数,它的欧拉函数值是本身减1.于是就可以转换到下式
a^(b^c) % p = a^( (b^c)%(p-1) )%p
直接搞个快速幂就好了
给出欧拉降幂的代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#define ll long long
using namespace std;
ll ol(ll x)
{
ll i,res=x;
for(i=;i*i<=x;i++)
{
if(x%i==)
{
res=res-res/i;
while(x%i==)
x/=i;
}
}
if(x>)res=res-res/x;
return res;
} //求某个值的欧拉函数值
ll q(ll x,ll y,ll MOD)
{
ll res=;
while(y){
if(y&)res=res*x%MOD;
x=(x*x)%MOD;
y>>=;
}
return res;
}//快速幂
char * change(ll a){
char s[];
int ans = ;
while(a){
s[ans++]=(a%)+'';
a/=;
}
s[ans]='\0';
strrev(s);
return s;
}//数字转字符串
char *solve(ll a,char s[],ll c){
ll i,ans,tmp,b;
ans=;b=;tmp=ol(c);
ll len=strlen(s);
for(i=;i<len;i++)b=(b*+s[i]-'')%tmp;
b += tmp;
ans=q(a,b,c);
return change(ans);
}//欧拉降幂
int main()
{
ll a,c = ,b,d;
char s[];
int t;
for(scanf("%d",&t);t--;){
scanf("%I64d %I64d %s",&a,&b,s);
printf("%s\n",solve(a,solve(b,s,ol(c)),c));//注意第一次降幂用的是 ol(c)
}
return ;
}
TOJ 3151: H1N1's Problem(欧拉降幂)的更多相关文章
- 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} ...
- HDU4704(SummerTrainingDay04-A 欧拉降幂公式)
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submi ...
- FZU:1759-Problem 1759 Super A^B mod C (欧拉降幂)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 欧拉降幂是用来干啥的?例如一个问题AB mod c,当B特别大的时候int或者longlong装不下的时 ...
- HDU - 4704 sum 大数取余+欧拉降幂
Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submi ...
- 2019-ACM-ICPC-南昌区网络赛-H. The Nth Item-特征根法求通项公式+二次剩余+欧拉降幂
2019-ACM-ICPC-南昌区网络赛-H. The Nth Item-特征根法求通项公式+二次剩余+欧拉降幂 [Problem Description] 已知\(f(n)=3\cdot f(n ...
- Power Tower(广义欧拉降幂)
题意:https://codeforc.es/contest/906/problem/D 计算区间的: ai ^ ai+1 ^ ai+2.......ar . 思路: 广义欧拉降幂: 注意是自下而上递 ...
- Codeforces Round #454 (Div. 1) CodeForces 906D Power Tower (欧拉降幂)
题目链接:http://codeforces.com/contest/906/problem/D 题目大意:给定n个整数w[1],w[2],……,w[n],和一个数m,然后有q个询问,每个询问给出一个 ...
- [数学][欧拉降幂定理]Exponial
Exponial 题目 http://exam.upc.edu.cn/problem.php?cid=1512&pid=4 欧拉降幂定理:当b>phi(p)时,有a^b%p = a^(b ...
- CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)
Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...
随机推荐
- linux 下使用 curl post
命令: curl -X POST -d @/etc/lazada/lazada_tracking.txt http://localhost:8080/booking/rs/LazadaServic ...
- electron安装到第一个实例
1.node.js下载,然后安装.下载地址:链接:http://pan.baidu.com/s/1o7TONhS 密码:fosa 2.cmd下输入:npm install electron-prebu ...
- 5分钟快速打造WebRTC视频聊天<转>
原文地址: 5分钟快速打造WebRTC视频聊天 百度一下WebRTC,我想也是一堆.本以为用这位朋友( 搭建WebRtc环境 )的SkyRTC-demo 就可以一马平川的实现聊天,结果折腾了半天,文本 ...
- openGL-------------别人的博客
https://blog.csdn.net/dcrmg/article/category/6505957 OpenGL(一)绘制圆.五角星.正弦曲线 ========================= ...
- ThinkPHP子类继承Controller类的注意事项
在实际的开发中,往往有很多子类都继承自同一个父类,然后该父类再继承自框架内置类的需求. 比如: class Init extends Controller{...} class son1 extend ...
- 如何查看一个class文件是否正确
今天碰到了个问题,左思右想就是找不出问题,试验多个路径来解决问题,错误依旧. 然后我拿到了现场的包,一个很大的问题让我忽略了,这个class文件用反编译程序打不开(jd-gui.exe),非常神奇,但 ...
- Kotlin语言编程技巧集
空语句 Kotlin 语言中的空语句有 {} Unit when (x) { 1 -> ... 2 -> ... else -> {} // else -> Unit } Wh ...
- mysql 远程 ip访问
默认情况下Linux内的mysql数据库mysql,user表内的用户权限只是对localhost即本机才能登陆.需要更改权限: 如下的方式确认: root#mysql -h localhost-u ...
- 吴裕雄 05-mysql删除数据库
drop database <数据库名>; 例如删除名为 RUNOOB 的数据库:drop database RUNOOB; 使用 mysqladmin 删除数据库你也可以使用 mysql ...
- Mysql字段类型与合理选择
字段类型 数值 MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小的数据,并且 MySQL 允许 ...