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;
}

FZU Super A^B mod C(欧拉函数降幂)的更多相关文章

  1. FZU 1759 欧拉函数 降幂公式

    Description   Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...

  2. HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

    装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...

  3. 2^x mod n = 1(欧拉定理,欧拉函数,快速幂乘)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu1395 2^x mod n = 1(欧拉函数)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. O(n)求素数,求欧拉函数,求莫比乌斯函数,求对mod的逆元,各种求

    筛素数 void shai() { no[1]=true;no[0]=true; for(int i=2;i<=r;i++) { if(!no[i]) p[++p[0]]=i; int j=1, ...

  6. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  7. lightoj 1370 欧拉函数

    A - Bi-shoe and Phi-shoe Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & % ...

  8. 51nod 1040最大公约数和(欧拉函数)

    1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 给出一个n,求1-n这n个数,同n的最大公约数 ...

  9. poj3696 快速幂的优化+欧拉函数+gcd的优化+互质

    这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...

随机推荐

  1. os.walk 模块

    os.walk()可以得到一个三元tupple(dirpath, dirnames, filenames),其中第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件. 其中dirpa ...

  2. python_8_guess

    #python3和2都可以 #方法1 age_of_oldboy=56 count=0 while True: if count==3: break guess_age=int(input('gues ...

  3. idea右侧的工具栏不见,包括maven以及其他

    通用,设置后右侧工具栏可见 可以通过view-->windows tool看到maven的

  4. 详解 JS 中 new 调用函数原理

    JavaScript 中经常使用构造函数创建对象(通过 new 操作符调用一个函数),那在使用 new 调用一个函数的时候到底发生了什么?先看几个例子,再解释背后发生了什么. 1)看三个例子 1.1 ...

  5. css3 横向拖拽

    css: .tab{         list-style-type: none;         display:-webkit-box;         display:-webkit-flex; ...

  6. nodejs实现前后端交互

    本人nodejs入门级选手,站在巨人(文殊)的肩膀上学习了一些相关知识,有幸在项目中使用nodejs实现了前后端交互,因此,将整个交互过程记录下来,方便以后学习. 本文从宏观讲述nodejs实现前后端 ...

  7. 数据存储之json文件处理和csv文件处理

    什么是json: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (w3c制定的js规范)的一个子集,采用 ...

  8. [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II

    [译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...

  9. python3 练习题100例 (七)

    题目七:将一个列表的数据复制到另一个列表中. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目七:将一个列表的数 ...

  10. Paul Zindel【保罗·金代尔】

    Paul Zindel Paul Zindel's death on March 27, 2003 ended the brilliant life of a famous write. 2003年3 ...