FZU1759Super A^B mod C(快速幂取模) 公式
来源:互联网 发布:手机淘宝订单号查询 编辑:程序博客网 时间:2024/05/16 09:10
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
There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
Output
For each testcase, output an integer, denotes the result of A^B mod C.
Sample Input
3 2 42 10 1000
Sample Output
124
对于A^B%C 有一个公式 即
A^x = A^(x % Phi(C) + Phi(C)) (mod C)
公式的具体证明:http://hi.baidu.com/aekdycoin/item/e493adc9a7c0870bad092fd9
// A^x = A^(x % Phi(C) + Phi(C)) (mod C),其中x≥Phi(C)#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;char bb[1000001];LL a,m;LL phi(LL n){ LL rea=n; for(int i=2;i*i<=n;i++){ if(n%i==0){ rea=rea-rea/i; while(n%i==0) n/=i; } } if(n>1) rea=rea-rea/n; return rea;}LL quick_mod(LL a,LL b,LL m){ LL ans=1; a%=m; while(b){ if(b&1){ ans=ans*a%m; b--; } b>>=1; a=a*a%m; } return ans;}int main(){ while(~scanf("%lld",&a)){ scanf("%s",bb); scanf("%lld",&m); LL t=phi(m); int l=strlen(bb); LL b=0; for(int i=0;i<l;i++){ b=b*10+bb[i]-'0'; while(b>=t) b-=t; } b+=t; printf("%lld\n",quick_mod(a,b,m)); } return 0;}
0 0
- FZU1759Super A^B mod C(快速幂取模) 公式
- fzu 1752 A^B mod C(快速乘+快速幂取模)
- FZU1759 Super A^B mod C 欧拉函数+快速幂+公式
- FZU 1759 Super A^B mod C (欧拉函数,快速幂,降幂公式)
- FZU1752&1650--A^B mod C--快速幂取模
- 51Nod-1046 A^B Mod C(快速幂取模)
- 快速幂,a^b mod c
- A^B Mod C(快速幂算法)
- A^B mod C(快速幂模板)
- 求A^B mod C的主要公式
- FZU - 1759 Super A^B mod C 降幂公式
- A^B mod C
- A^B mod C
- A^B mod C
- 快速幂算法 取余运算 a^b mod c
- FZU 1752 A^B mod C (快速幂)
- 数论——快速幂算法 a^b mod c
- 51 nod 1046 A^B Mod C(快速幂取余)
- 怎么恢复被误删硬盘文件
- java4android36集到38集,线程
- hdu 1255 覆盖的面积(线段树&扫描线&重复面积)
- Hibernate学习笔记
- 01背包、完全背包、多重背包 总结小讲
- FZU1759Super A^B mod C(快速幂取模) 公式
- 两台服务器同步数据 rsync
- 如何恢复数码相机存储卡上已被删除的照片,数码相机存储卡照片数据恢复教程
- 虚拟化、大数据、云计算、物联网、移动互联网以及传统互联网之间的关系
- TOMCAT内存溢出及大小调整
- 使用SDL播放YUV图像数据
- java中static、final、static final的区别
- 判断屏幕大小
- android实现TextView垂直或水平滚动