FZU 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
Output
Sample Input
Sample Output
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(欧拉函数降幂)的更多相关文章
- FZU 1759 欧拉函数 降幂公式
Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000 ...
- HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂
装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...
- 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 ...
- 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 ...
- 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, ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- lightoj 1370 欧拉函数
A - Bi-shoe and Phi-shoe Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & % ...
- 51nod 1040最大公约数和(欧拉函数)
1040 最大公约数之和 题目来源: rihkddd 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 给出一个n,求1-n这n个数,同n的最大公约数 ...
- poj3696 快速幂的优化+欧拉函数+gcd的优化+互质
这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...
随机推荐
- 转载请注明:Windows 系统必备好用软件&工具合集跟推荐 | 老D博客
Windows 系统必备好用软件&工具合集跟推荐 97 63,371 A+ 所属分类:工具软件 一.浏览器 二.下载软件 三.播放软件 五.电子邮件客户端 六.图片/照片 浏览查看工具 七.文 ...
- python_56_递归
在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.(最大调用自己999次) def calc(n): print(n) if int(n/2)>0: retur ...
- Object类和String类
Object类 Object类是Java语言中的根类,即所有类的父类. equals方法 返回值类型为:boolean类型 用于比较两个对象是否相同,它其实就是使用两个对象的内存地址在比较. 例子: ...
- 安装mysql提示This application requires .NET framework 4.0.
问题描述:安装MySQL社区版时遇到This application requires .NET framework 4.0. 解决方法:在http://search.microsoft.com/zh ...
- PHP namespace、abstract、interface、trait使用介绍
小菜鸟一枚,一直搞不懂 namespace.abstract.interface.trait 这些关系,就抽出几天时间研究,做个总结,不足之处希望大家指正交流. namespace 命名空间 介绍:顾 ...
- js浮点数加减乘除
浮点数精确计算 /** ** 加法函数,用来得到精确的加法结果 ** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较为精确的加法结果. ** 调用:ac ...
- php 获取 今天、昨天、这周、上周、这月、上月、近30天
<?php //今天 $today = date("Y-m-d"); //昨天 $yesterday = date("Y-m-d", strtotime( ...
- 无线Web开发
http://am-team.github.io/amg/dev-exp-doc.html
- day 52 Django基础一之web框架的本质
Django基础一之web框架的本质 django第一天 本节目录 一 web框架的本质及自定义web框架 二 模板渲染JinJa2 三 MVC和MTV框架 四 Django的下载安装 五 基于D ...
- A * B Problem Plus HDU - 1402 (FFT)
A * B Problem Plus HDU - 1402 (FFT) Calculate A * B. InputEach line will contain two integers A and ...