FZU-1752.(A^B mod C)(快速幂与快速乘优化)
我把自己演哭了...
心酸.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)(快速幂与快速乘优化)的更多相关文章
- 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式
矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b * A B = a*A+b*C a*c+b*D c d ...
- 求幂大法,矩阵快速幂,快速幂模板题--hdu4549
hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...
- 快速幂 & 矩阵快速幂
目录 快速幂 实数快速幂 矩阵快速幂 快速幂 实数快速幂 普通求幂的方法为 O(n) .在一些要求比较严格的题目上很有可能会超时.所以下面来介绍一下快速幂. 快速幂的思想其实是将数分解,即a^b可以分 ...
- 快速幂 ,快速幂优化,矩形快速幂(java)
快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ...
- FZU 1752 A^B mod C(快速加、快速幂)
题目链接: 传送门 A^B mod C Time Limit: 1000MS Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...
- 【板子】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 ...
- jiulianhuan 快速幂--矩阵快速幂
题目信息: 1471: Jiulianhuan 时间限制: 1 Sec 内存限制: 128 MB 提交: 95 解决: 22 题目描述 For each data set in the input ...
- Luogu P1226 取余运算||快速幂_快速幂
超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...
- 【数论】 快速幂&&矩阵快速幂
首先复习快速幂 #include<bits/stdc++.h> using namespace std; long long power(long long a,long long b,l ...
随机推荐
- asp:Repeater控件使用
Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行. Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出格式.D ...
- linux驱动开发(三) 字符设备驱动框架
还是老规矩先上代码 demo.c #include <linux/init.h> #include <linux/module.h> #include <linux/ke ...
- eclipse插件spket安装
1.
- babel 基本
babel的大概知识点 . babel常用的转译器是babel-preset-env. 常用的配置选项是plugins和presets 常用的使用场景是在webpack中 https://www.cn ...
- java后端实习生面试题目
1.编程题:java从10000到99999找到AABB类型 public class Test1 { public static void main(String[] args) { String ...
- Linux命令:chmod
https://baijiahao.baidu.com/s?id=1616750933810368135&wfr=spider&for=pc
- Dos命令快速设置ip、网关、dns地址
netsh interface ip set address name="本地连接" source=static 192.168.1.8 255.255.255.0 192.168 ...
- oracle vm突然黑屏了
装完mongodb-compass后,我重启了下虚拟机,发现突然进不到ubuntu系统了,一开起来就黑屏.网上查了下有的说是显卡问题的,有说是内核问题的,但我啥都没干突然间就黑屏了.折腾了一下午没搞定 ...
- 【转】WinDbg调试器:启动程序时自动连接调试器方法
当我们调试服务进程或子进程时,无法直接用调试加载进程的方式启动,此时需要在启动程序时自动连接调试器方法: 第一步:注册表展开到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft ...
- 10.mysql-触发器.md
目录 定义 语法 定义 当操作了某张表时,希望同时触发一些动作/行为,可以使用触发器完成 语法 -- 需求: 当向员工表插入一条记录时,希望mysql自动同时往日志表插入数据 -- 创建触发器(添加) ...