[英]CSES exponentiation 2 problem in handling mod=1000000007(10^9+7)
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
#define tt ull tt; cin>>tt; while(tt--)
ull calExpo(ull x, ull y, ull mod){
ull res=1;
while(y>0){
if(y&1){
res=(res*x)%mod;
}
y=y>>1;
x=(x*x)%mod;
}
return res;
}
void solve(void){
tt{
ull x,y,z;
cin>>x>>y>>z;
ull mod=1000000007;
ull t= calExpo(y,z,mod-1); // doubt is in this line
cout<<calExpo(x,t,mod)<<endl;
}
}
int main(void){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
solve();
return 0;
}
i wrote code for CSES problem https://cses.fi/problemset/task/1712/ (problem description in short: give n test cases for each n there are 3 integer values a,b,c we have to calculate a^b ^c(a 到 b 次方,b 到 c 次方)给定 mod=10^9+7 )当我第一次调用 function(calExpo)时,我只传递(mod)作为第三个参数时出现错误(在线ull t= calExpo(y,z,mod-1); ) 然后我通过了 mod-1 并且它通过了所有测试用例。 请告诉我这背后的原因是什么?
之所以取mod-1
是正确的,是因为费马小定理:
所以:
在您的情况下,您将b
^ c
表示为r * (p-1) + q
,这实际上是取模p-1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.