我把自己演哭了...

心酸.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. ubuntu14.04 rabbitmq安装与使用 --修改RabbitMQ数据存储位置

    参考:https://blog.csdn.net/tianjiewang/article/details/58383062 说明: ubuntu14.04   rabiitmq 默认 安装路径 /va ...

  2. mybatis 3.2.2_环境搭建

    1.创建一个工程 utf-8 2.导入jar mybatis-3.2.2.jar 核心包 依赖包: asm-3.3.1.jar cglib-2.2.2.jar commons-logging-1.1. ...

  3. day06-三元表达式

    python中没有其他语言中的三元表达式,不过有类似的实现方法 其他语言中,例如java的三元表达式是这样int a = 1;String b = "";b = a > 1? ...

  4. SAFESEH 映像是不安全的

    1.打开该项目的“属性页”对话框 2.然后单击“链接器”--“命令行”,出现如下界面 3.将 /SAFESEH:NO 复制到“其它选项(D)”框中,然后点击应用 返回VS2013,重新编译运行程序即可 ...

  5. Django--views(视图层)

    路径匹配后-----传给视图函数 一.视图函数 视图层,熟练掌握两个对象即可:请求对象(request)和响应对象(HttpResponse) 一个视图函数,简称视图,是一个简单的Python 函数, ...

  6. Django基础介绍

    1.web应用 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件. 应用程序有两种模式C/S.B/S.C/S是客户 ...

  7. 解决eclipse新建项目看不到src/main/java目录办法

    1.eclipse->window->preferences->java->compiler->选择本地要用的Java版本 2.eclipse->window-&g ...

  8. Oracle VM VirtualBox各种显示模式切换 热键

    初用VirtualBox, 几个显示切换快捷键还是要记一下的: Right Ctrl + F        -- 切换到全屏模式 Right Ctrl + L        -- 切换到无缝模式 Ri ...

  9. 转: HTML5之placeholder属性以及如何更改placeholder属性中文字颜色

    今天在群里看到群友问了一个这样的问题,就是如何更改placeholder属性中文字的颜色,以前用过这属性,却是没更改过颜色,于是便试了试,中途遇到些问题,查找资料后特来总结一下. 熟悉HTML5的人应 ...

  10. GIS案例学习笔记-明暗等高线提取地理模型构建

    GIS案例学习笔记-明暗等高线提取地理模型构建 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:针对数字高程模型,通过地形分析,建立明暗等高线提取模型,生成具有 ...