Problem 1759 Super A^B mod C

Accept: 878    Submit: 2870
Time 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 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

 Source

FZU 2009 Summer Training IV--Number Theory

题目链接:FZU 1759

参考博客:http://blog.csdn.net/acdreamers/article/details/8236942,本来在搞蛇精病的十进制快速幂的时候做的这个,结果超时了,后来测试发现十进制快速幂还没二进制快……是我太天真了……于是膜一下正确解法,主要利用了这个欧拉定理的扩展公式,当然最重要的是求出一个数的欧拉函数值$phi(x)$,这个可以用埃式筛的思想求得。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1000010; LL bpow(LL a, LL b, LL mod)
{
LL r = 1LL;
while (b)
{
if (b & 1)
r = ((r % mod) * (a % mod)) % mod;
a = ((a % mod) * (a % mod)) % mod;
b >>= 1;
}
return r;
}
LL phi(LL n)
{
LL r = n, sz = sqrt(n);
for (LL i = 2; i <= sz; ++i)
{
if (n % i == 0)
{
r = r * (i - 1) / i;
while (n % i == 0)
n /= i;
}
}
if (n > 1)
r = r * (n - 1) / n;
return r;
}
int main(void)
{
LL a, c;
char B[N];
while (~scanf("%I64d%s%I64d", &a, B, &c))
{
LL phi_c = phi(c);
int len = strlen(B);
LL b;
if (len <= 20)
{
sscanf(B, "%I64d", &b);
if (b >= phi_c)
b = b % phi_c + phi_c;
}
else
{
b = 0LL;
for (int i = 0; i < len; ++i)
{
b = b * 10LL + B[i] - '0';
if (b > phi_c)
b %= phi_c;
}
b += phi_c;
}
printf("%I64d\n", bpow(a, b, c));
}
return 0;
}

最新文章

  1. UILabel加载HTML
  2. [bzoj1911][Apio2010]特别行动队
  3. JVM内存简单理解
  4. 如何把Eclipse工程导入到Android Studio
  5. 【安全测试】 WebScarab安装方法
  6. spring junit
  7. Beaglebone Black&ndash;智能家居控制系统 LAS - 用 UART 连接 ESP8266 (ESP-01 版)
  8. buffer busy wait
  9. Raising Modulo Numbers(POJ 1995 快速幂)
  10. html、css、js实现轮播图
  11. js的学习(window对象的使用)
  12. AM335x(TQ335x)学习笔记——使用dtb方式启动内核
  13. 【HADOOP】| 环境搭建:从零开始搭建hadoop大数据平台(单机/伪分布式)-下
  14. 盖洛普Q12在团队中的应用
  15. CF700E Cool Slogans
  16. Mac影音多媒体工具软件推荐
  17. git 提交丢失Warning, you are leaving 2 commits behind,
  18. 3A - Holding Bin-Laden Captive!
  19. python文件读取操作、序列化
  20. (转)ASP.NET MVC 第五个预览版和表单提交场景

热门文章

  1. 如何在python中读写和存储matlab的数据文件(*.mat)
  2. React报错 :browserHistory doesn&#39;t exist in react-router
  3. 51nod——1640 天气晴朗的魔法 有边权限制的最大生成树
  4. Python_常用模块
  5. 3 个用于数据科学的顶级 Python 库
  6. 学习Pytbon第三天,用户输入
  7. Paper Folding UVA - 177 模拟+思路+找规律
  8. Intellij Idea 创建JavaWeb项目
  9. 常用Style
  10. jq相关细节-1