虽然AC,蒟蒻仍有一个小问题,求助dalao
查看原帖
虽然AC,蒟蒻仍有一个小问题,求助dalao
393190
aldol_reaction楼主2020/11/11 16:40

第一次进入循环后b和ans都没有取模啊,为什么不需要取模呢(请看代码注释。。) 不应该是(A×B)modb=((Amodb)×(Bmodb))modb 那么在第一次 ans *= b;和b *= b;的时候为何没有取模操作呢?

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<set>
using namespace std;

#define debug(x)std::cout << "Line" << __LINE__ << "	" << #x << "=" << x << std::endl
typedef long long ll;

ll b, p, k, ans = 1, B, P;

int main() {
	scanf("%lld %lld %lld", &b, &p, &k);
	B = b, P = p;
//	ans %= k, b %= k;//为什么加不加这一行都能AC呢
	while(p > 0) {
		if(p & 1) {
			ans *= b;
			ans %= k;
		}
		b *= b;
		b %= k;
		p >>= 1;
	}
	printf("%lld^%lld mod %lld=%lld", B, P, k, ans%k);
	return 0;
}

2020/11/11 16:40
加载中...