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. WPF 实际国际化多语言界面

    前段时候写了一个WPF多语言界面处理,个人感觉还行,分享给大家.使用合并字典,静态绑定,动态绑定.样式等东西 效果图 定义一个实体类LanguageModel,实际INotifyPropertyCha ...

  2. Studio for WPF:定制 C1WPFChart 标记

    在本篇文章中,我们将阐述如何定制 C1WPFChart 数据点的标记. 下面分步讲解实现: 1.定制自定义样式: 1: <Window.Resources> 2: <Style x: ...

  3. Python轻量Web框架Flask使用

    http://blog.csdn.net/jacman/article/details/49098819 目录(?)[+] Flask安装 Python开发工具EclipsePyDev准备 Flask ...

  4. 后缀数组---New Distinct Substrings

    Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...

  5. Maven多模块项目使用MyBatis Generator

    开发环境: JDK:8u102 Maven:3.3.9 MySQL:5.7.10 MySQL Connector:5.1.40 IDE:IntelliJ IDEA 2016 MyBatis:3.4.1 ...

  6. nginx服务器是怎么执行php脚本的?

    简单的说: fastCGI是nginx和php之间的一个通信接口,该接口实际处理过程通过启动php-fpm进程来解 析php脚本,即php-fpm相 当于一个动态应用服务器,从而实现nginx动态解析 ...

  7. Webform(分页、组合查询)

    一.分页 1.写查询方法: public List<Student> Select(int PageCount, int PageNumber) {//PageCount为每页显示条数,P ...

  8. How to Install Hadoop on Ubuntu

    安装教程,https://www.digitalocean.com/community/tutorials/how-to-install-hadoop-on-ubuntu-13-10

  9. JY游戏之毁经典《扫雷》

    JY游戏之毁经典<扫雷> 这是一个经典的pc端游戏,一定的运气加一点数学常识,讲的是一个速度,这次,我利用js JY库重做了这款游戏,加了三次生命,过关难度,也兼容了移动端的触摸事件. 它 ...

  10. Vue列表渲染

    gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson09 一 for循环数组 <!DOCTYPE html> ...