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的更多相关文章

  1. fzou 1759 Super A^B mod C

    Problem 1759 Super A^B mod CAccept: 456    Submit: 1488Time Limit: 1000 mSec    Memory Limit : 32768 ...

  2. 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 ...

  3. 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 ...

  4. FZU Super A^B mod C(欧拉函数降幂)

    Problem 1759 Super A^B mod C Accept: 878    Submit: 2870 Time Limit: 1000 mSec    Memory Limit : 327 ...

  5. 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 ...

  6. Super A^B mod C (快速幂+欧拉函数+欧拉定理)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...

  7. FZU:1759-Problem 1759 Super A^B mod C (欧拉降幂)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 欧拉降幂是用来干啥的?例如一个问题AB mod c,当B特别大的时候int或者longlong装不下的时 ...

  8. fzu1759 Super A^B mod C 扩展欧拉定理降幂

    扩展欧拉定理: \[ a^x \equiv a^{x\mathrm{\ mod\ }\varphi(p) + x \geq \varphi(p) ? \varphi(p) : 0}(\mathrm{\ ...

  9. 欧拉降幂公式 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 ...

随机推荐

  1. 你得知道这3个最基础的APP技术框架

    出处:优设网作者:信籽链接:http://www.uisdc.com/3-basic-app-technical-framework 信籽(手淘设计师):不知道大家有没有遇到过这种情景,当你做好一个设 ...

  2. C#的Raw Socket实现网络封包监视

    同Winsock1相比,Winsock2最明显的就是支持了Raw Socket套接字类型,使用Raw Socket,可把网卡设置成混杂模式,在这种模式下,我们可以收到网络上的IP包,当然包括目的不是本 ...

  3. ComponentOne Studio for Enterprise 2015 v1 全新发布

    ComponentOne Studio 即将发布2015年的第一个版本.2015 v1版本聚焦于优化性能.提升数据可视化能力.加强数据管理以及更人性的输入方式及报表解决方案. 免费下载试用 WinFo ...

  4. Linux 安装 PHP 环境

    使用虚拟机玩linux时,发现CentOS中的php版本是5.1.6.如果要安装新版的php,需要把旧的版本删除. 先查看下php版本:# php -v 如果执行该命令提示该命令不存在,那么可以通过以 ...

  5. 3kb jQuery代码搞定各种树形选择。

    自制Jquery树形选择插件. 对付各种树形选择(省市,分类..)90行Jquery代码搞定,少说废话直接上插件代码.稍后介绍使用说明.是之前写的一个插件的精简版. 1.Jquery插件代码 /* * ...

  6. H5调用Android拨打电话

    1.AndroidAndJSInterface.java class AndroidAndJSInterface { /** * 该方法将被js调用,用于加载数据 */ @JavascriptInte ...

  7. copy 和 strong(或retain)的区别

    http://stackoverflow.com/questions/18526909/whether-i-should-use-propertynonatomic-copy-or-propertyn ...

  8. 关于ol有序裂变和ul无序列表前面的列表项标记的位置

    使用列表项标记的时候发现其对齐方式竟然从内容开始,于是发现了这个属性可以解决: list-style-position inside 列表项目标记放置在文本以内,且环绕文本根据标记对齐. outsid ...

  9. HTML 5 <mark> 标签

    一,定义和用法 <mark> 标签定义带有记号的文本.请在需要突出显示文本时使用 <m> 标签. 二,实例 突出显示部分文本: <!DOCTYPE HTML> &l ...

  10. javamail 发送邮件demo

    package com.suntray.test; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.M ...