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

这题的b非常大,只能用字符串处理;

需要一个降幂公式

然后带入欧拉公式,快速幂,快速乘法就可以了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
typedef __int64 ll;
typedef long double ld;
//const ll mod=1e9+7;
using namespace std;
const double pi=acos(-1.0);
vector<int>v;
char x[1000005];
ll multi(ll a,ll b,ll c)//快速乘
{
ll ans=0;
while(b)
{
if(b&1)
ans=(ans+a)%c;
a=(a+a)%c;
b>>=1;
}
return ans;
}
ll pow(ll a,ll b,ll c)//快速幂
{
ll ans=1,bas=a;
while(b)
{
if(b&1)
ans=multi(ans,bas,c);
bas=multi(bas,bas,c);
b>>=1;
}
return ans;
}
ll p[1000100];
ll ola(ll n){ //欧拉函数
ll i, j, r, aa;
r = n;
aa = n;
mm(p,0);
for(i=2; i<=sqrt(n); i++)
{ if(aa%i==0)
{
r = r/i*(i-1);
while(aa%i==0)
aa /= i;
}
}
if(aa>1)
r = r/aa*(aa-1);
return r;
}
int main()
{
ll a,b,mod;
while(~sf("%I64d %s %I64d",&a,&x,&mod))
{
ll cas=ola(mod);
ll ans=0;
int num=strlen(x);
for(int i=0;i<num;i++)//求那个b%phi(c)
{
ans=ans*10+x[i]-48;
ans%=cas;
}
if(ans<0) ans+=mod;
pf("%I64d\n",pow(a,ans+cas,mod));
}
}

K - 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. 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. 微信小程序无法获取UnionId的情况及处理

    问题背景:做了微信小程序,一切都还正常,但是最后体验版放出去时,却发现很多用户无法绑定用户,后台返回:参数非法.经过多方排查,发现是微信拿到的code请求返回的数据里没有UnionId,也就是接口返回 ...

  2. set,env,export,source,exec傻傻分不清楚?

    https://segmentfault.com/a/1190000013356532

  3. 将cmd中命令输出保存为TXT文本文件

    转自:https://www.cnblogs.com/hongten/archive/2013/03/27/hongten_windows_cms.html 例如:将Ping命令的加长包输出到D盘的p ...

  4. C语言(C++语言)中##(两个井号)和#(一个井号)用法[转]

    文章来源:http://blog.csdn.net/starboybenben/article/details/49803315 C语言(C++语言)中的宏(Macro)属于编译器预处理的范畴,属于编 ...

  5. Java对象的内存实际占用

    一.打包和使用方法参考我之前的这篇文章,本文主要是更新了测量的类及方法,实际测试这个方法更准确. https://www.cnblogs.com/yoyotl/p/8421287.html 二.新的测 ...

  6. 详解nginx 配置多个tomcat共用80端口

    场景:项目1放在tomcat1中,项目2放在tomcat2中,两个tomcat放在同一台服务器上,需要共享80端口访问注意:这里和集群部署是不同的,集群部署是一个项目放在多个tomcat中.这里通过n ...

  7. 揭开Docker的神秘面纱

    Docker 相信在飞速发展的今天已经越来越火,它已成为如今各大企业都争相使用的技术.那么Docker 是什么呢?为什么这么多人开始使用Docker? 本节课我们将一起解开Docker的神秘面纱. 本 ...

  8. linux每日命令(37):top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...

  9. pivot 与 unpivot函数

    pivot 与 unpivot函数 pivot 与 unpivot 函数是SQL05新提供的2个函数 灰常灰常的实用 ----------------------------------------- ...

  10. egret3.x升级5.2

    第一步 先用新建项目向导新建一个5.2的项目 第二步 把3.x的代码和资源文件复制过来替换掉 修改资源加载代码 在3.x里 egret采用的是事件机制来加载资源,在5.2里则采用了await/asyn ...