FZU 1759 Super A^B mod C (欧拉函数,快速幂,降幂公式)
来源:互联网 发布:网络买重庆时时彩违法 编辑:程序博客网 时间:2024/05/18 03:02
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759
一道吓人的题。。
不禁再次感叹数学真伟大,使用下面的降幂公式很简单就写出来了。
phi是欧拉函数,如果不太清楚欧拉函数是什么,怎么求欧拉函数,可以看看下面这两个博客,或者参考维基百科。
http://blog.csdn.net/leolin_/article/details/6642096
http://blog.csdn.net/once_hnu/article/details/6302868 (直接法和筛选法求欧拉函数值)
学会了求欧拉函数值,我们就可以利用上面那个降幂公式来计算结果了。
参考博客:http://blog.csdn.net/t1019256391/article/details/37595993 感谢!
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;ll phi(ll n) { //直接法求欧拉函数值 int res = n, a = n;int i;for(i = 2; i * i <= a; i++) {if(a % i == 0) {res -= res / i;while(a % i == 0) a /= i;}}if(a > 1) res -= res / a;return res;}ll qpow(ll a, ll b, ll c) { //快速幂 ll res = 1;while(b) {if(b&1) res = res * a % c;a = a * a % c;b >>= 1;}return res;}int main() {ll a, c;char b[1000010];while(~scanf("%I64d %s %I64d", &a, b, &c)) {ll phic = phi(c);a %= c;int i, len = strlen(b);ll res = 0;for(i = 0; i < len; i++) {res = res * 10 + b[i] - '0';if(res > phic) break; //降幂公式的条件,只有指数大于phi(c)才可用 }if(i == len) {printf("%I64d\n", qpow(a, res, c)); //指数小于等于phi(c),直接计算 }else {res = 0; //降幂 for(i = 0; i < len; i++) {res = res * 10 + b[i] - '0';res %= phic;}printf("%I64d\n", qpow(a, res + phic, c));}}return 0;}
0 0
- FZU 1759 Super A^B mod C (欧拉函数,快速幂,降幂公式)
- FZU 1759-Super A^B mod C (欧拉函数+降幂公式)
- FZU 1759 Super A^B mod C (欧拉函数,降幂公式)
- FZU 1759-Super A^B mod C (快速幂+欧拉降幂+欧拉函数)
- FZU 1759-Super A^B mod C(快速幂+大整数取模+欧拉函数)
- FZU 1759 Super A^B mod C(数论+快速幂+欧拉函数)
- FZU oj 1759 Super A^B mod C (快速幂+指数循环节+欧拉函数)
- FZU - 1759 Super A^B mod C 降幂公式
- fzu Problem 1759 Super A^B mod C(快速幂取模+欧拉函数)
- FZU1759 Super A^B mod C 欧拉函数+快速幂+公式
- FZU 1759 Super A^B mod C (超级快速幂)
- Super A^B mod C 快速幂+欧拉函数降幂
- fzu1759 Super A^B mod C 【欧拉函数应用+快速幂】
- 欧拉函数 FZOJ 1759 Super A^B mod C
- FZU 1759 Super A^B mod C (快速幂+指数循环节)
- FZU Problem 1759 Super A^B mod C(幂次循环节+快速乘法)
- FZU 1759 Super A^B mod C
- FZU 1759Super A^B mod C
- hive优化之------控制hive任务中的map数和reduce数
- HashMap的工作原理
- 输入一个字符串,计算字符串中子串出现的次数
- Node.js开发入门(十)——使用cookie保持登录
- Qt之图形(绘制文本)
- FZU 1759 Super A^B mod C (欧拉函数,快速幂,降幂公式)
- Node.js开发入门(十一)——MongoDB与Mongoose
- Java enum的用法详解
- 在Linux中安装sqldeveloper
- 网络互联
- 数据挖掘工作指南(1)
- 关于用ObjectInputStream遍历读取文件中的对象,如何判断到达文件末尾
- 51nodLearning-dp入门之循环数组的最大子段和
- Node.js缓冲器