FZU 1759 Super A^B mod C(数论)
来源:互联网 发布:android源码 编辑:程序博客网 时间:2024/05/22 01:40
Description
给出三个正整数A,B,C,求A^B mod C
Input
多组用例,每组用例输入三个正整数A,B,C
(1<=A,C<=1000000000,1<=B<=10^1000000)
Output
Sample Input
3 2 4
2 10 1000
Sample Output
1
24
Solution
由指数循环定理,
当B<=phi(C)时,直接用快速幂计算A^B mod C
当B>phi(C)时,用快速幂计算A^(B mod phi(C)+phi(C)) mod C
Code
#include<cstdio>#include<iostream>#include<cstring>using namespace std;typedef long long ll;#define maxn 1111111ll get_euler(ll n){ ll ans=n; for(ll i=2;i*i<=n;i++) if(n%i==0) { ans=ans/i*(i-1); while(n%i==0)n/=i; } if(n>1)ans=ans/n*(n-1); return ans;}ll mod_pow(ll a,ll b,ll c){ a%=c; ll ans=1; while(b) { if(b&1)ans=ans*a%c; a=a*a%c; b>>=1; } return ans;}ll a,b,c;char s[maxn];int main(){ while(~scanf("%lld%s%lld",&a,s,&c)) { ll d=get_euler(c); int len=strlen(s),flag=0; b=0; for(int i=0;i<len;i++) { b=b*10+s[i]-'0'; if(b>d)flag=1,b%=d; } ll ans=flag?mod_pow(a,b+d,c):mod_pow(a,b,c); printf("%lld\n",ans); } 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 1759Super 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 Super A^B mod C
- Super A^B mod C FZU
- fzu - Problem 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 Problem 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 (快速幂+欧拉降幂+欧拉函数)
- 安卓数据库SQLite详解
- Poj1185状态压缩
- Unity3D的一些操作技巧
- Javascript grid汇总值
- .NET2.0程序 在.net 4.0 中运行的解决方案
- FZU 1759 Super A^B mod C(数论)
- 腾讯云安装wordpress
- android------bootAnimation
- HDOJ 1232畅通工程
- java时间处理
- android 实现发送短信功能
- Linux 密码验证
- ArrayList之三种迭代方法
- HTML滚动文字代码 marquee标签