6 条题解
-
4
使用快速幂算法计算a(分之b)取模1000的值。但是,在最后输出结果时,应该使用
%03lld
,而不是%03d
。其中,%03lld
表示输出一个3位的长整数,若长度不足则在前面补0。另外,为了避免数据溢出,可以将s对1000取模,即:
for (int i = 0; i < b; i++) { s *= a; s %= 1000; }
上述代码中,每次计算完s×a之后,都会对1000取模,保证s始终小于或等于1000。
完整代码如下:
#include <iostream> #include <cstdio> using namespace std; long long a, b, s = 1; int main() { cin >> a >> b; for(int i = 0;i < b;++i) s *= a, s %= 1000; printf("%03lld", s); return 0; }
-
2
1.不能用pow()(数据范围大,函数返回数据类型为double)
所以只能用循环实现幂的运算
2.求余运算需要每次循环进行一次(根据求余分配律... 因为数据范围太大)
#include <bits/stdc++.h> using namespace std; int main(){ int a,b; cin >> a >> b; int sum = 1; // for (int i = 1; i <= b; i++) { sum *= a; sum %= 1000; // 每次进行求余 } printf("%03d", sum); // 补足三位 }
- 1
信息
- ID
- 181
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 4
- 标签
- 递交数
- 202
- 已通过
- 88
- 上传者