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<=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

时间: 2024-08-03 21:05:19

Super A^B mod C的相关文章

FZU - 1759 Super A^B mod C 降幂公式

知道降幂公式这题就很好办了 B>=Phi(c)的时候可以降幂然后快速幂计算,否则就直接快速幂计算. 这里的大数对小数取模直接利用取模性质按位取就行了. //A^B %C=A^( B%phi(C)+phi(C) ) %C #include <cstdlib> #include <cstring> #include <cstdio> #include <iostream> #include<string> #include<cmath&g

FUZ 1759 Super A^B mod C (指数循环节/模板)

题意:求A^B mod C,其中(1<=A,C<=1000000000,1<=B<=10^1000000). 思路: 在有些题目中我们需要对指数进行降幂处理才能计算.比如计算 其中和 这里由于很大,所以需要进行降幂.那么实际上有如下降幂公式 有了上述公式,很多题目就可以迎刃而解了. 摘自ACdreamer博客 代码: #include <iostream> #include <string.h> #include <stdio.h> using

Super A^B mod C 快速幂+欧拉函数降幂

uper A^B mod C Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 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

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

扩展欧拉定理: \[ a^x \equiv a^{x\mathrm{\ mod\ }\varphi(p) + x \geq \varphi(p) ? \varphi(p) : 0}(\mathrm{\ mod\ }p)\] #include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long ll; ll aa, cc; char bb[100000

Fzu1759 Super A^B mod C 题解

题目大意 以若干行的形式给定若干组a,b,c(你不知道有多少组),输出若干行,每行代表相应的abmodc的值.其中1≤a,c≤1000000000,1≤b≤101000000 评测网址 http://acm.fzu.edu.cn/problem.php?pid=1759 题解 数据真的很大..... 不过我们有公式: abmodc=a(bmodφ(c))+φ(c)modc,b≥φ(c); 把它记为(*)式. 我们可以把b先用字符串ch读入,再从高位到低位处理: for(int i = 0; i

FZU-1759 Super A^B mod C---欧拉降幂&amp;指数循环节

题目链接: https://cn.vjudge.net/problem/FZU-1759 题目大意: 求A^B%C 解题思路: 注意,这里long long需要用%I64读入,不能用%lld 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 using namespace std; 6 typedef long long ll; 7 const in

FZU1759(SummerTrainingDay04-B 欧拉降幂公式)

Problem 1759 Super A^B mod C Accept: 1056    Submit: 3444Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description 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

[转] POJ数学问题

转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式. *简单题:(直接用套公式就可以了) pku2409 Let it Bead      http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 pku2154 Co

『转』数学专辑

1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能完全看懂了,理解了再去做题,不要只记个公式. *简单题:(直接用套公式就可以了) pku2409 Let it Bead   http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 pku2154 Color http://acm.pku.edu.cn/JudgeOnline/problem?id=2154 pku12