Super A^B mod C
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 4
2 10 1000
Sample Output
1
24
处理大数的方法挺经典的,把别人代码贴出来以示膜拜

view code//A^B %C=A^( B%phi(C)+phi(C) ) %C
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include<string>
#include<cmath>
using namespace std;
typedef __int64 ll;
int phi(int x)
{
int i,j;
int num = x;
for(i = 2; i*i <= x; i++)
{
if(x % i == 0)
{
num = (num/i)*(i-1);
while(x % i == 0)
{
x = x / i;
}
}
}
if(x != 1) num = (num/x)*(x-1);
return num;
}
ll quickpow(ll m,ll n,ll k)
{
ll ans=1;
while(n)
{
if(n&1) ans=(ans*m)%k;
n=(n>>1);
m=(m*m)%k;
}
return ans;
}
char tb[1000015];
int main()
{
ll a,nb;
int c;
while(scanf("%I64d%s%d",&a,tb,&c)!=EOF)
{
int PHI=phi(c);
ll res=0;
for(int i=0;tb[i];i++)
{
res=(res*10+tb[i]-'0');
if(res>c)break;
}
if(res<=PHI)
{
printf("%I64d\n",quickpow(a,res,c));
}
else
{
res=0;
for(int i=0;tb[i];i++)
{
res=(res*10+tb[i]-'0')%PHI;
}
printf("%I64d\n",quickpow(a,res+PHI,c));
}
}
return 0;
}
Super A^B mod C的更多相关文章
- fzou 1759 Super A^B mod C
Problem 1759 Super A^B mod CAccept: 456 Submit: 1488Time Limit: 1000 mSec Memory Limit : 32768 ...
- FZU 1759 Super A^B mod C 指数循环节
Problem 1759 Super A^B mod C Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description G ...
- FOJ ——Problem 1759 Super A^B mod C
Problem 1759 Super A^B mod C Accept: 1368 Submit: 4639Time Limit: 1000 mSec Memory Limit : 32 ...
- FZU Super A^B mod C(欧拉函数降幂)
Problem 1759 Super A^B mod C Accept: 878 Submit: 2870 Time Limit: 1000 mSec Memory Limit : 327 ...
- K - Super A^B mod C
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B ...
- Super A^B mod C (快速幂+欧拉函数+欧拉定理)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...
- FZU:1759-Problem 1759 Super A^B mod C (欧拉降幂)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 欧拉降幂是用来干啥的?例如一个问题AB mod c,当B特别大的时候int或者longlong装不下的时 ...
- fzu1759 Super A^B mod C 扩展欧拉定理降幂
扩展欧拉定理: \[ a^x \equiv a^{x\mathrm{\ mod\ }\varphi(p) + x \geq \varphi(p) ? \varphi(p) : 0}(\mathrm{\ ...
- 欧拉降幂公式 Super A^B mod C
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=100000 ...
随机推荐
- ASP.NET MVC中错误日志信息记录
MVC中有一个处理异常的过滤器 HandleErrorAttribute 1.新建一个类继承自 HandleErrorAttribute,然后重写OnException这个方法 public clas ...
- How to manage the certificates in the PC
1.open Run command. 2.enter 'mmc' . 3.Click File, and Add or Remove Snap-in. 4.Select Certificates, ...
- WebApi传参总动员(填坑)
本以为系列文章已经Over,突然记起来前面留了个大坑还没填,真是自己给自己挖坑. 这个坑就是: (body 只能被读取一次)Only one thing can read the body MVC和W ...
- WebApi传参总动员(二)
上篇,从最简单的string入手.本篇演示了从请求的输入流中获取实体.api: public class ValuesController : ApiController { [HttpPost] p ...
- Go eclipse plugin
Installation Requirements: Eclipse 4.5 (Mars) or later. Java VM version 8 or later. Gocode and Go or ...
- [Xamarin.Android] 自定义控件
[Xamarin.Android] 自定义控件 前言 软件项目开发的过程中,免不了遇到一些无法使用内建控件就能满足的客户需求,例如:时速表.折线图...等等.这时开发人员可以透过自定义控件的方式,为项 ...
- ASP.NET MVC自定义AuthorizeAttribute篇知识点讲解—登录限制
1.前言 a.微软对ASP.NET的开发从WebForm到MVC的转变,已经正式过去5,6个年头,现在WebForm和MVC也都越来越完善,小小算来我也已经工作了将近三年,从大学的时候学习ASP.NE ...
- html,xhtml和xml
html,xhtml和xml的定义: 1.html即是超文本标记语言(Hyper Text Markup Language),是最早写网页的语言,但是由于时间早,规范不是很好,大小写混写且编码不规范: ...
- SharePoint 自定义WebPart之间的连接
1.创建SharePoint解决方案,添加两个WebPart分别用来发送和接收: 2.发送值的WebPart需要继承自IWebPartField(当然,根据需要还可以选择IWebPartField,I ...
- SharePoint固定的Footer
原文地址:http://www.eliostruyf.com/sticky-footer-solution-for-sharepoint-2013/ 照搬全文: OFFICE 365 & SH ...