在刷 CSP-J 2022 普及组第一题(传送门)时,我提交了如下代码:
#include <bits/stdc++.h>
#define ULL unsigned long long
using namespace std;
ULL a, b, ans = 1, i;
int main() {
//freopen("pow.in", "r", stdin);
//freopen("pow.out", "w", stdout);
cin >> a >> b;
for (i = 1; i <= b; i++) {
ans *= a;
if (ans > 1e9) {
cout << -1 << endl;
return 0;
}
}
cout << ans;
return 0;
}
但是,Subtask #0 的 #18 和 #1 的 #7 TLE 了。
但是如果我在第 15 行的 1e9 改为 (int)1e9,它就 AC 了,j蒟蒻太蒻,各位大牛可以帮我讲一下吗?
AC 代码:
#include <bits/stdc++.h>
#define ULL unsigned long long
using namespace std;
ULL a, b, ans = 1, i;
int main() {
//freopen("pow.in", "r", stdin);
//freopen("pow.out", "w", stdout);
cin >> a >> b;
for (i = 1; i <= b; i++) {
ans *= a;
if (ans > (int)1e9) {
cout << -1 << endl;
return 0;
}
}
cout << ans;
return 0;
}