第一道 CH0102
https://www.acwing.com/problem/content/description/92/
求 a 乘 b 对 p 取模的值。
输入格式
第一行输入整数a,第二行输入整数b,第三行输入整数p。
输出格式
输出一个整数,表示a*b mod p的值。
数据范围
1≤a,b,p≤1018
输入样例:
3
4
5
输出样例:
2
先用c++写了一发,思路类似于快速幂。
#include <iostream>
#define LL long long
using namespace std;
LL mul(LL a,LL b,LL p){
LL ans = 0;
for (;b;b>>=1){
if (b&1) ans = (ans+a) % p;
a=a * 2 %p;
}
return ans;
}
int main(){
LL a,b,p;
cin >> a >> b >> p;
cout << mul(a,b,p);
}
想到用python解决高精度问题有奇效,于是写了一发Python,第一次用Python交acm
a = int(input())
b = int(input())
c = int(input())
d = int (a*b %c)
print (d)
当然,时间上有点差距: