輸入正整數 \(x\), \(y\) 與 \(p\),計算 \(x^y \bmod p\)。\(x\), \(y\), \(p\) 皆不超過 \(10^9 + 9\)。例如 \(x=2,y=5, p=11\),則答案是\(10\)。
輸入 \(x\), \(y\) 與 \(p\) 在同一行,以空白間隔,行尾可能有空格。
輸出計算結果。
2 5 11
10
typedef long long LL; using namespace std;
LL exp(LL x, LL y, LL p){ LL result = 1;
while (y > 0){ if (y%2 == 1) result *= x;
x *= x; //先平方(下一輪備用) y >>= 1;
} return result % p; }
int main(){ LL a,b,c; cin >> a >> b >> c; cout << exp(a,b,c) << endl; return 0; }
等一下對不起我看錯我以為這裡是 submit 的地方
留言
include <bits/stdc++.h>
typedef long long LL; using namespace std;
LL exp(LL x, LL y, LL p){ LL result = 1;
while (y > 0){ if (y%2 == 1) result *= x;
} return result % p; }
int main(){ LL a,b,c; cin >> a >> b >> c; cout << exp(a,b,c) << endl; return 0; }
等一下對不起我看錯我以為這裡是 submit 的地方