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 ...
随机推荐
- HTML5 图片宽高自适应,居中裁剪不失真
一,使用 JS,先上效果图,右图为定死宽高的效果,左图为处理之后的 1, 主要思路是,在图片 onload 后,将图片的宽高比和 div 容器的宽高比进行比较, 2, 从而确定拉伸或者压缩之后是宽还是 ...
- UIPanGestureRecognizer translateInView, locationInView
假设在panGesture的回调事件里已经拿到了panGestureRecognizer CGPoint point = [panGestureRecognizer locationInView:se ...
- System.net.mail 使用ssl发送邮件失败
我采用了.net 的自带组件System.Net.Mail发送邮件,主要是在客户注册网站成功的时候发条欢迎邮件,最近邮件无法发送了,看了下腾讯smtp邮件配置,所有的邮件发送都换成ssl了,之前用的是 ...
- javascript_ajax 地址三级联动
1.三级地址联动思路如下: 2.建立数据库.这里直接使用网上的地址数库,最后一个字段无用,先不去管它 3.建立一个server.php 文件 <?php // 数据库连接 mysql_conne ...
- CAS单点登录原理解析
转自: https://www.cnblogs.com/lihuidu/p/6495247.html 1.基于Cookie的单点登录的回顾 基于Cookie的单点登录核心原理: 将用户名 ...
- java.lang.ClassNotFoundException: org.hibernate.engine.SessionFactoryImplementor
Hibernate4.x与spring3.x整合,有关事务的处理,用Junit4测试,出现org.springframework.beans.factory.BeanCreationException ...
- centos7下找不到iptables文件
最近在centos7下,搭建ftp服务,按照步骤一步一步来,发现 etc/sysconfig/iptables这个文件并不存在,然后去找解决方案, 原文地址:http://blog.csdn.net/ ...
- genymotion使用学习
1 安装 直接去其官网(https://www.genymotion.com/#!/download)下载安装包安装即可,安装中会附带安装VirtualBox. 2 注册 必须使用帐号登录后,方可下载 ...
- 7.final关键字.md
1.final类型变量 定义:被final修饰的变量,一旦被赋初值后,则final类型变量的值就不能再改变. 1.1成员变量 final修饰的成员变量必须显式的赋初值. 赋值的位置: •类变量:静态初 ...
- 26.如何使用python操作我们自己创建的docker image呢?
因为逻辑复杂 我们建个文件来 python #是单行注释 '''是多行注释 或者””” 我们想使用python来操作docker 那么就要一个api https://github.com/docker ...