我把自己演哭了...

心酸.jpg

写了很多个版本的,包括数学公式暴力,快速幂TLE等等,最后想到了优化快速幂里的乘法,因为会爆longlong,但是和别人优化的效率简直是千差万别...?

  本题大意:

    给定三个longlongint范围内的正整数a, b, c,求出a^b mod c 的结果并输出。

  本题思路:

    见代码吧。

下面贴出我的各种版本的代码...

  参考代码:

//这道题数据有点水,不明白为啥数据里1^0 mod 1 == 1 ?魔鬼...

/*
数学公式 :: 超时版 #include <cstdio>
using namespace std; typedef long long int LL;
LL a, b, mod, ans; int main () {
while(~scanf("%lld %lld %lld", &a, &b, &mod)) {
ans = 1;
for(int i = 1; i <= b; i ++) {
ans = (ans * (a % mod)) % mod;
}
printf("%lld\n", ans);
}
return 0;
}
*/ /*
快速幂::爆longlong版 #include <cstdio>
using namespace std; typedef long long int LL;
LL a, b, c, ans; void quickpow() {
ans = 1;
while(b) {
if(b & 1)
ans = (ans * (a % c)) % c;
a = ((a % c) * (a % c)) % c;
b >>= 1;
}
} int main() {
while(~scanf("%lld %lld %lld", &a, &b, &c)) {
quickpow();
printf("%lld\n", ans);
}
}
*/ /*
快速幂改进版 ::又来一发TLE版 #include <cstdio>
using namespace std;
typedef long long int LL; LL quickpow(LL a, LL b, LL mod) {
LL ans = 1;
while(b) {//ans *= a;
if(b & 1) {
LL c = ans;
for(int i = 2; i <= a; i ++)
ans = (ans % mod + c % mod) % mod;
}
LL c = a;
for(int i = 2; i <= c; i ++)
a = (c % mod + a % mod) % mod;
b >>= 1;
}
return ans;
} int main () {
LL ans, a, b, mod;
while(~scanf("%I64d %I64d %I64d", &a, &b, &mod))
printf("%I64d\n", quickpow(a, b, mod));
return 0;
}
*/ /* 快速幂与快速乘改进 ::AC版
*/
#include <cstdio>
using namespace std; typedef long long int LL; LL quickmuti(LL a, LL b, LL mod) {
LL ret = ;
a %= mod;
while(b) {
if(b & ) {
ret += a;
if(ret > mod) ret -= mod;
}
a <<= ;
if(a >= mod) a -= mod;
b >>= ;
}
return ret;
} LL quickpow(LL a, LL b, LL mod) {
LL ret = ;
while(b) {
if(b & ) ret = quickmuti(ret, a, mod);
a = quickmuti(a, a, mod);
b >>= ;
}
return ret;
} int main () {
LL a, b, mod;
while(~scanf("%I64d %I64d %I64d", &a, &b, &mod)) {
printf("%I64d\n", quickpow(a, b, mod));
}
return ;
}

FZU-1752.(A^B mod C)(快速幂与快速乘优化)的更多相关文章

  1. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  2. 求幂大法,矩阵快速幂,快速幂模板题--hdu4549

    hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...

  3. 快速幂 & 矩阵快速幂

    目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分 ...

  4. 快速幂 ,快速幂优化,矩形快速幂(java)

    快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ...

  5. FZU 1752 A^B mod C(快速加、快速幂)

    题目链接: 传送门 A^B mod C Time Limit: 1000MS     Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...

  6. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  7. jiulianhuan 快速幂--矩阵快速幂

    题目信息: 1471: Jiulianhuan 时间限制: 1 Sec  内存限制: 128 MB 提交: 95  解决: 22 题目描述 For each data set in the input ...

  8. Luogu P1226 取余运算||快速幂_快速幂

    超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...

  9. 【数论】 快速幂&&矩阵快速幂

    首先复习快速幂 #include<bits/stdc++.h> using namespace std; long long power(long long a,long long b,l ...

随机推荐

  1. elcipse 安装lombok插件解决 @Slf4j 等找不到log变量问题

    参考:http://blog.51cto.com/4925054/2127840 <dependency> <groupId>org.projectlombok</gro ...

  2. NRF51822之使用外部32Mhz晶振

    硬件平台为微雪BLE400的(将原来的16mhz晶振改为32mhz.两个旁电容改为22pf) 以nRF51_SDK_10.0.0_dc26b5e\examples\ble_peripheral\ble ...

  3. 机器学习入门-文本数据-构造Tf-idf词袋模型(词频和逆文档频率) 1.TfidfVectorizer(构造tf-idf词袋模型)

    TF-idf模型:TF表示的是词频:即这个词在一篇文档中出现的频率 idf表示的是逆文档频率, 即log(文档的个数/1+出现该词的文档个数)  可以看出出现该词的文档个数越小,表示这个词越稀有,在这 ...

  4. 现学现卖——VS2013 C#测试

    VS2013 C#测试 首先安装Unit Test Generator.方法为:工具->扩展和更新->联机->搜索“Unit Test Generator”,图标为装有蓝色液体的小试 ...

  5. tfs对接数据-File

    在使用tfs时,数据结构 /** * getFile */ @RequestMapping("/tfs/{fileName}") public ResponseEntity< ...

  6. css3-animate

    常用动画设置: effect easing duration  effect: <select name="effects" id="effectTypes&quo ...

  7. 构建模式--Adapter模式(JAVA)

    适配器模式: 适配器就相当于我们的转接头,比如手机充电器插头(小米和华为的手机充电器不能共用,这时候就可以给华为的充电器按一个转接头,就可以给小米手机充电). 同理,当一个类(充电器 HuaweiCh ...

  8. 人脸识别68个点<转>

    [Opencv] 于仕琪 人脸68个特征点分布情况 // 鼻尖 30 // 鼻根 27 // 下巴 8 // 左眼外角 36 // 左眼内角 39 // 右眼外角 45 // 右眼内角 42 // 嘴 ...

  9. pbft流程深层分析和解释(转)

    <1>pbft五阶段请求解释 Request  pre-prepare   prepare   commit  执行并reply (1)pre-prepare阶段: 主节点收到客户端请求, ...

  10. git 合并多个commit

    1,查看提交历史,git log 首先你要知道自己想合并的是哪几个提交,可以使用git log命令来查看提交历史,假如最近4条历史如下: commit 3ca6ec340edc66df13423f36 ...