我把自己演哭了...

心酸.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. [记录] 解决img的1px空白问题

    第一种解决方案:把img变成块元素:display:block: 第二种解决方案:修改一下它的垂直对齐方式:vertical-align:middle: 第三种解决方案:使用浮动,让他漂浮起来:flo ...

  2. react-native获取设备信息app版本信息,react-native-device-info

    安装 yarn add react-native-device-info react-native link react-native-device-info link 之后就可以直接使用了,ios ...

  3. vue格式化显示json数据

    已经是json格式数据的,直接用标签 <pre></pre>展示. 参考:https://www.jianshu.com/p/d98f58267e40

  4. SVM标记学习

    # -*- coding: utf-8 -*- """ Created on Mon Oct 1 09:32:37 2018 @author: ""& ...

  5. php中显示数组与对象的实现代码

    1. 使用 print_r ( $array/$var ) print 是打印的意思,而r则取自Array的单词,那么该函数的功能就是打印数组内容,它既可以打印数组内容,也可以打印普通的变量. pri ...

  6. Ajax技术剖析

    Ajax的全称是Asynchronous JavaScript and XML,是JS的特有功能,它作用是异步JS数据交互,即在不进行页面刷新的情况下进行部分数据的获取,性能较高.值得注意的是,仅有A ...

  7. ABAP-邮件发送

    *&---------------------------------------------------------------------* *& Report ZRICO_TES ...

  8. JSP基本_JSTL

    自定义标签是,用户定义自己的处理的tag的机制. JSTL是,JSP用标准自定义标签.从JSTL Ver.1.2开始成为JavaEE5的子集.比较有名的是Glassfish.Tomcat上开发的话,需 ...

  9. 数据结构:Queue

    Queue设计与实现 Queue基本概念 队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操 ...

  10. centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod

    centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod ...