我把自己演哭了...

心酸.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. PHP下进行XML操作(创建、读取)

    PHP下可以使用DOMDocument类对XML或者HTML文件进行读写操作 更为简单的方法使用simpleXML类操作XML DOM节点分为 元素节点 属性节点 值节点 注释节点 根节点(docum ...

  2. Git 全局配置查看修改

    查看 git config --list git config --global --list 新增 git config --global user.emal=123 删除 git config - ...

  3. pandas 笔记

    删除: del df["A"]  # 原地修改 df.drop("a")  # 返回修改后的新对象 df.drop(["a", " ...

  4. HashSet和LinkedHashSet解析

    一.简介 1.Set概念 Set可以理解为集合,非常类似数据概念中的集合,集合三大特征:1.确定性:2.互异性:3.无序性,因此Set实现类也有类似的特征. 2.HashSet HashSet继承自A ...

  5. HTTP、TCP、IP协议常见面试题

    前言:在看面试题之前,先了解一下基本定义. HTTP.TCP.IP协议基本定义 HTTP: (HyperText Transport Protocol)是超文本传输协议的缩写,它用于传送WWW方式的数 ...

  6. Linux:条件变量

    条件变量:     条件变量本身不是锁!但它也可以造成线程阻塞.通常与互斥锁配合使用.给多线程提供一个会合的场所. 主要应用函数:     pthread_cond_init函数     pthrea ...

  7. NGUI 背景图自适应

    背景图UISprite组件调整如下: UIRoot设置: 不保持比例自适应: 保持宽与屏幕宽一致,高度随宽的缩放比例进行缩放:

  8. VC中BSTR、Char和CString类型的转换

    1.char*转换成CString 若将char*转换成CString,除了直接赋值外,还可使用CString::format进行.例如: char chArray[] = "This is ...

  9. vue中嵌套页面(iframe)

    vue中嵌套iframe,将要嵌套的文件放在static下面.(要将打包文件整体放在statici里,我的文件名是canvas) src可以使用相对路径,也可使用服务器根路径http:localhos ...

  10. C++中文件读写的操作

    在C++中读读写文件一般指的就是磁盘中的文本文件和二进制文件: 文本文件:以字符序列组成的文件 二进制文件:由二进制组成的文件 读写文件采用ofstream和ifstream文件流,两者可用头文件&l ...