博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速幂算法模板
阅读量:3950 次
发布时间:2019-05-24

本文共 763 字,大约阅读时间需要 2 分钟。

a 的 b 次方对 p 取模的值。

输入格式

三个整数 a,b,p ,在同一行用空格隔开。

输出格式

输出一个整数,表示a^b mod p的值。

数据范围

0≤a,b,p≤109
数据保证 p≠0
输入样例:
3 2 7
输出样例:
2

ll  fast_pow(ll a,ll b, int p) {
if (b==0) return 1; ll x = fast_pow(a, b/2, p); ll res = x*x %p; if ( b%2==1 ) res = res*a % p; return res;}
#include 
using namespace std;int main(void) {
int a,b,p;cin>>a>>b>>p; int res = 1 % p; while (b) {
if (b & 1) res = res*1ll * a % p; a = a*1ll * a % p; b>>=1; } cout << res << endl; return 0; }
long long int mod = 100;long long int fast_power(long long int x, long long int y){
if (y < 0) return 0; long long int ans = 1; while (y) {
if (y & 1) ans = ans * x %mod; x = x * x %mod; y >>= 1; } return ans;}

转载地址:http://myyzi.baihongyu.com/

你可能感兴趣的文章
CoreLocation笔记 by STP
查看>>
Application Transport Security has blocked a cleartext HTTP (http://) 解决方案
查看>>
The identity used to sign the executable is no longer valid.解决方案
查看>>
Xcode增加pch文件
查看>>
CocoaPods安装和使用笔记 by STP
查看>>
Could not find developer disk image-解决方案
查看>>
升级Xcode之后VVDocumenter-Xcode不能用的解决办法
查看>>
iOS开发常见报错及解决方案 by STP
查看>>
SVN(Cornerstone)屏蔽/忽略不需要版本控制的UserInterfaceState.xcuserstate
查看>>
IOS 8 以上版本 设置applicationIconBadgeNumber和消息推送
查看>>
git常用命令
查看>>
Java 基本数据类型笔记by STP
查看>>
IDEA创建Maven项目时 loading archetype list转菊花转十年解决方案
查看>>
Mac启动tomcat
查看>>
报错: java.sql.SQLException: The server time zone value '�й�' is unrecognized or represents more ...
查看>>
使用xshell对服务器上的sql文件进行操作(mysql导入Linux)
查看>>
Spirngboot 后台操作一切正常并无报错,但是前端出现404错误
查看>>
java错误:java.lang.String can not be cast to java.math.BigDecimal
查看>>
Linux导出数据库文件mysql
查看>>
xshell查看程序代码后台的动态日志
查看>>