Super A^B mod C (快速幂+欧拉函数+欧拉定理)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759
题目:Problem Description
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).
Input
Output
Sample Input
Sample Output
思路:由于B的数据太大,因而我们需要借助欧拉定理来进行降幂,然后再用快速幂解决。
代码实现如下:
#include <cstdio>
#include <cstring> const int maxn = 1e6 + ;
long long a, b, c;
char s[maxn]; int euler(int x) {
int ans = x;
for(int i = ; i * i <= x; i++) {
if(x % i == ) {
ans = ans / i * (i - );
while(x % i == ) x /= i;
}
}
if(x > ) ans = ans / x * (x - );
return ans;
} long long ModPow(int n, int p, int mod) {
long long rec = ;
while(p) {
if(p & ) rec = rec * n % mod;
n = (long long) n * n % mod;
p >>= ;
}
return rec;
} int main() {
while(~scanf("%lld%s%lld", &a, s, &c)) {
b = ;
int rec = euler(c);
int len = strlen(s);
for(int i = ; i < len; i++) {
b = b % rec;
b = ((s[i] - '') + * b) % rec;
}
b += rec;
printf("%lld\n", ModPow(a, b, c));
}
return ;
}
Super A^B mod C (快速幂+欧拉函数+欧拉定理)的更多相关文章
- HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 4549 矩阵快速幂+快速幂+欧拉函数
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Sub ...
- HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂
装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...
- [bzoj 1409] Password 矩阵快速幂+欧拉函数
考试的时候想到了矩阵快速幂+快速幂,但是忘(bu)了(hui)欧拉定理. 然后gg了35分. 题目显而易见,让求一个数的幂,幂是斐波那契数列里的一项,考虑到斐波那契也很大,所以我们就需要欧拉定理了 p ...
- hdu 5895(矩阵快速幂+欧拉函数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895 f(n)=f(n-2)+2*f(n-1) f(n)*f(n-1)=f(n-2)*f(n-1)+2 ...
- hdu 2814 快速求欧拉函数
/** 大意: 求[a,b] 之间 phi(a) + phi(a+1)...+ phi(b): 思路: 快速求欧拉函数 **/ #include <iostream> #include & ...
- 欧拉函数&&欧拉定理
定义和简单性质 欧拉函数在OI中是个非常重要的东西,不知道的话会吃大亏的. 欧拉函数用希腊字母φ表示,φ(N)表示N的欧拉函数. 对φ(N)的值,我们可以通俗地理解为小于N且与N互质的数的个数(包含1 ...
- 欧拉函数&欧拉定理&降幂 总结
欧拉函数&欧拉定理&降幂 总结 标签:数学方法--数论 阅读体验:https://zybuluo.com/Junlier/note/1300214 这年头不总结一下是真的容易忘,老了老 ...
- hdu1395 2^x mod n = 1(欧拉函数)
2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- 相机上的P,S,A,M分别是什么单词的缩写?
程序曝光 Programmed Auto快门优先 Shutter Priority光圈优先 aperture-priority 全手动模式 Manual Mode
- 【Docker】- 基本命令
1.docker ps -a 显示所有容器 2.doker ps -l 显示最近一次启动的容器 3.docker ps 显示正在运行的容器 4.docker start [容器ID] 启动 ...
- Jenkins系列-Jenkins通过Publish over SSH插件实现远程部署
配置ssh免秘钥登录 安装Publish over SSH插件 插件使用官网:https://wiki.jenkins.io/display/JENKINS/Publish+Over+SSH+Plug ...
- 在vue项目中使用monaco-editor
monaco-editor: https://github.com/Microsoft/monaco-editor 在ESM中的使用官方也有对应文档:https://github.com/Micros ...
- matlab padarray
功能:填充图像或填充数组 使用:B = padarray(A,padsize,padval,direction) A表示输入图像,B是填充后的图像,padsize给出了填充的行数和列数,通常用[r c ...
- 2018 杭电多校1 - Chiaki Sequence Revisited
题目链接 Problem Description Chiaki is interested in an infinite sequence $$$a_1,a_2,a_3,...,$$$ which i ...
- Codeforces Round #510 Div. 2 Virtual Participate记
这场打的顺手到不敢相信.如果不是vp的话估计肯定打不到这个成绩. A:最大显然,最小的话每次暴力给最小的+1. #include<iostream> #include<cstdio& ...
- YY的GCD 莫比乌斯反演
---题面--- 题解: $ans = \sum_{x = 1}^{n}\sum_{y = 1}^{m}\sum_{i = 1}^{k}[gcd(x, y) == p_{i}]$其中k为质数个数 $$ ...
- BZOJ1012:[JSOI2008]最大数——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1012 https://www.luogu.org/problemnew/show/P1198 现在 ...
- BZOJ1060:[ZJOI2007]时态同步——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1060 https://www.luogu.org/problemnew/show/P1131 小Q在 ...