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. mysql主备配置方法

    1. 选择两台机器(这里选的centos6.5 final),安装相同版本的mysql yum install mysql ; yum install mysql-server; 2. 启动mysql ...

  2. docker 打开监听端口

    安装docker的镜像ubuntu,链接:http://www.cnblogs.com/liqiu/p/4162719.html 一.登录ubuntu @~ $ docker run -i -t 9b ...

  3. 手把手教你用CAB发布OCX的简单办法

    CAB是一种文件压缩格式,把OCX文件(ActiveX插件)压缩成CAB文件是为了发布它,这种发布方式给人感觉“浏览器会自动安装该插件”,很多朋友钟情于此.目前流行很多压缩工具,但WINRAR这类工具 ...

  4. Nginx配置静态资源

    静态服务器 静态服务器概念非常简单:当用户请求静态资源时,把文件内容回复给用户. 但是,要把静态服务做到极致,需要考虑的方面非常多: 正确书写header:设置content-type.过期时间等 效 ...

  5. FFMPEG中关于ts流的时长估计的实现(转)

    最近在做H.265 编码,原本只是做编码器的实现,但客户项目涉及到ts的封装,搞得我不得不配合了解点ts方面的东西.下面技术文档不错,转一下. ts流中的时间估计 我们知道ts流中是没有时间信息的,我 ...

  6. PCA主成份分析学习记要

    前言 主成份分析,简写为PCA(Principle Component Analysis).用于提取矩阵中的最主要成分,剔除冗余数据,同时降低数据纬度.现实世界中的数据可能是多种因数叠加的结果,如果这 ...

  7. MAC OS X显示.开头的文件_苹果操作系统显示隐藏文件命令

    转自:http://dditblog.com/blog_446.html 今天在导入Eclipse项目的时候.发现导入不了.初步估计是因为项目没有.project的文件.在Mac OS X操作系统下面 ...

  8. vue模板的讲解

    1.项目目录 2.入口文件index.html 3.index.html默认调用的main.js 3.调用的组件app.vue 4.调用组件hello.vue

  9. Nginx配置WebService、MySQL、SQL Server、ORACLE等代理

    首先介绍一下Nginx的基本使用: 注意不要直接双击nginx.exe,这样会导致修改配置后重启.停止nginx无效,需要手动关闭任务管理器内的所有nginx进程 在nginx.exe目录,打开命令行 ...

  10. Hive数据倾斜解决办法总结

    数据倾斜是进行大数据计算时最经常遇到的问题之一.当我们在执行HiveQL或者运行MapReduce作业时候,如果遇到一直卡在map100%,reduce99%一般就是遇到了数据倾斜的问题.数据倾斜其实 ...