A^B mod C (快速幂+快速乘+取模)题解
2024-09-12 17:06:25
A^B mod C
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63).
Input
There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
Output
For each testcase, output an integer, denotes the result of A^B mod C.
Sample Input
3 2 4
2 10 1000
Sample Output
1
24
我用的unsigned_int64开数据。用快速幂和快速乘写,注意:不要用%,会超时(大佬说的,并且我已经验证过了...233)
代码:
#include<stdio.h>
#define ull unsigned __int64
ull mul(ull a,ull b,ull c){
ull res=0;
a=a%c;
while(b){
if(b & 1) res+=a;
if(res>=c) res-=c; //代替%
b/=2;
a+=a;
if(a>=c) a-=c; //代替%
}
return res;
}
int main(){
ull a,b,c,ans;
while(scanf("%I64u%I64u%I64u",&a,&b,&c)!=EOF){
ans=1;
a=a%c;
while(b){
if(b%2==1) ans=mul(ans,a,c);
a=mul(a,a,c);
b/=2;
}
printf("%I64u\n",ans);
}
return 0;
}
最新文章
- System.Security.Cryptography.CryptographicException,密钥集不存在
- Lisp中编写宏的步骤以及规范
- 初探JavaScript(三)——JS带我";碰壁";带我飞
- centos7 静态ip设置
- [整理]iOS开发学习
- 破解 RCA_CRACKME(解除隐藏按钮)
- 大并发连接的oracle在Linux下内存不足的问题的分析
- robotframework-FQA
- TableLayout
- Javascript判断数据类型与真假值隐形转换研究
- Redis与数据库同步问题
- 洛谷 P2073 送花【Treap】题解+AC代码
- CSS3选择器p:nth-child和p:nth-of-type之间的差异
- COMP9334 Project
- [GXOI/GZOI2019]旅行者
- ShowDoc上手
- Flask---第一个例子--使用Flask写的【Hello World !】的web程序
- qrCode二维码字符串长度太多压缩的问题
- JavaScript 函数式编程读书笔记1
- git从安装到使用