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. C#设计模式——工厂方法模式(Factory Method Pattern)

    一.概述在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口.如何应对这种变化?如何提供一种封装机制来隔离出“这个易变对象 ...

  2. [爬虫学习笔记]基于Bloom Filter的url去重模块UrlSeen

            Url Seen用来做url去重.对于一个大的爬虫系统,它可能已经有百亿或者千亿的url,新来一个url如何能快速的判断url是否已经出现过非常关键.因为大的爬虫系统可能一秒钟就会下载 ...

  3. Winform屏幕截图保存C#代码

    代码如下: using System.Runtime.InteropServices; using System.Drawing.Imaging; [System.Runtime.InteropSer ...

  4. KMP的原理详细讲解

    1.kmp算法的原理: 本部分内容转自:http://www.cnblogs.com/c-cloud/p/3224788.html及                           http:// ...

  5. mysql 5.7.15发布

    2016-09-06,mysql发布了5.7更新5.7.15,修复的bug数项目之前的版本已经大大减少,说明越来越稳定了.估计再过三四个版本,就会有很多公司开始考虑生产中使用了.

  6. mysql init-file参数中语句限制

    mysql 启动选项中的init-file文件的内容目测只能是dml语句,不能包含ddl,否则执行就会报错,但不影响启动本身..太扯了..

  7. linux线程控制&线程分离

    线程概念 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元. 线程是程序中一个单一的顺序控制流程.进程内一个相对独立的.可调度的执行单元,是系统独立 ...

  8. 「C语言」int main还是void main?

    从大一入学刚接触C到现在已满7个月了,虽然刚开始就知道```int main```才是标准的写法,但一直没有深刻理解为什么不能用```void main```而必须使用```int main```. ...

  9. C#枚举类型和结构体

    注意:枚举类型和结构体都属于值类型. 结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 一.定义的方法: struct student { public int nianl ...

  10. java响应微信用户信息(wechat4j)

    你的微信应用程序接收到用户发送的消息事件之后,可以进行响应.wechat4j支持多种消息的响应. wechat4j响应消息的部分在wechat4j的入口WechatSupport.java中定义,直接 ...