luogu1226 取余运算||快速幂
题目大意:快速求$a^b\mod p$的值。
根据二进制,令$b=\sum t_k\cdot 2^k, t\in \{0,1\}$,那么$$a^b=a^{\sum t_k\cdot 2^k}\mod p=\prod a^{t_k \cdot 2^k}\mod p$$。$k$表示当前处理的$b$的二进制数的位数,$t_k$的取值取决于当前$b$的二进制位$k$上的值是$0$还是$1$。
同理,为了防止乘法越界,还要进行快速乘法。$$ab\mod p=\sum t_k\cdot a\cdot 2^k\mod p$$,各项解释与上相同。
#include <cstdio>
using namespace std; #define LL long long LL mul(LL a, LL b, LL p)
{
LL ans = 0;
while (b)
{
if (b & 1)
ans = (ans + a) % p;
a = (a + a) % p;
b >>= 1;
}
return ans;
} LL power(LL a, LL b, LL p)
{
LL ans = 1;
while (b)
{
if (b & 1)
ans = mul(ans, a, p);//更新
a = mul(a, a, p);//(a^2^k)^2=a^(2*2^k)=a^2^(k+1)
b >>= 1;//b的二进制下一位
}
return ans;
} int main()
{
LL a, b, p;
scanf("%lld%lld%lld", &a, &b, &p);
printf("%lld^%lld mod %lld=%lld\n", a, b, p, power(a, b, p));
return 0;
}
luogu1226 取余运算||快速幂的更多相关文章
- 洛谷——P1226 取余运算||快速幂
P1226 取余运算||快速幂 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod ...
- 洛谷 P1226 取余运算||快速幂
P1226 取余运算||快速幂 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod ...
- luogu P1226 取余运算||快速幂
题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 输入输 ...
- Luogu P1226 取余运算||快速幂_快速幂
超短代码 #include<iostream> #include<cstdio> using namespace std; long long b,p,k; long long ...
- 洛谷 P1226 【模板】快速幂||取余运算
题目链接 https://www.luogu.org/problemnew/show/P1226 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 ...
- 洛谷P1226 【模板】快速幂||取余运算
题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出“b^p mod k=s” s为运算结果 S1: ...
- LuoguP1226 【模板】快速幂||取余运算
题目链接:https://www.luogu.org/problemnew/show/P1226 第一次学快速幂,将别人对快速幂原理的解释简要概括一下: 计算a^b时,直接乘的话计算次数为b,而快速幂 ...
- 快速幂 cojs 1130. 取余运算
cojs 1130. 取余运算 ★ 输入文件:dmod.in 输出文件:dmod.out 简单对比时间限制:10 s 内存限制:128 MB [题目描述] 输入b,p,k的值,求b^p ...
- 洛谷 P1226 【模板】快速幂||取余运算 题解
Analysis 快速幂模板,注意在最后输出时也要取模. 快速幂模板 inline ll ksm(ll x,ll y) { ll ans=; ) { ) { ans*=x; ans%=k; } x*= ...
随机推荐
- iframe弹出窗体丢失焦点的问题
好像在不同的浏览器都有这个现象,用javascript弹出一个iframe的窗口,第一次input的焦点是正常的, 然后弹出第二次的时候,选择,按钮都可以获取到,但是input无法获得焦点,而且页面不 ...
- span标签对于margin-top,margin-bottom无效,但是margin-left,margin-right却是有效的。
首先,span是行内元素,是没有宽高的. 我们来做个简单的布局 <div> <span>内容内容内容内容内容内容内容内容</span> </div> 页 ...
- Leetcode0100--Same Tree 相同树
[转载请注明]http://www.cnblogs.com/igoslly/p/8707664.html 来看一下题目: Given two binary trees, write a functio ...
- telerik:RadAsyncUpload 使用 时不执行上传事件的解决办法AsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
一般是因为web.config没有配置的原因! 只要在<handlers>下加上 <add name="Telerik.Web.UI.WebResource" v ...
- Java+selenium+Firefox/ IE/ Chrome主流浏览器自动化环境搭建
一.java+selenium+firefox 1.环境准备:JDK1.8 2.安装firefox浏览器v59 3.下载驱动:https://github.com/mozilla/geckodrive ...
- vue.js的ajax和jsonp请求
首先要声明使用ajax 在 router下边的 Index.js中 import VueResource from 'vue-resource'; Vue.use(VueResource); ajax ...
- Day 18 hashlib,logging模块
hashlib 模块 作用:hash是一种算法,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法,该算法接受传入的内容,经过运算得到一串hash值 特点: 1.只要 ...
- PAT_A1125#Chain the Ropes
Source: PAT A1125 Chain the Ropes (25 分) Description: Given some segments of rope, you are supposed ...
- 使用canvas截图网页为图片并解决跨域空白以及模糊问题
前几天给了个需求对浏览器网页进行截图,把网页统计数据图形表等截图保存至用户本地. 首先对于网页截图,我用的是canvas实现,获取你需要截图的模块的div,从而使用canvas对你需要的模块进行截图. ...
- CentOS 7.2 安装Python3.5.2
系统环境: CentOS 7.2 x86_64 安装相关包 (1)# yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sq ...