1 条题解
-
6
这道题我给大家三种解法
解法①:分配率
#include <iostream> using namespace std; int cnt(int n, int m, int k, int x) { int ans = 1, a = 10; while(k) { if((k) & (1)) { ans = ans * a % n; } a = a * a % n; k >>= 1; } return (x % n + m % n * ans % n) % n; } int main() { int n, m, k, x; cin >> n >> m >> k >> x; cout << cnt(n, m, k, x) << endl; return 0; }
解法②:递增取余
#include <iostream> using namespace std; int main() { long long n, m, k, x,y; cin >> n >> m >> k >> x; long long x1 = x, x2 = x; for (long long i = 0; i < 1e9; i++) { x += m; x %= n; if (x == x1) { x1 = i + 1; break; } }//x1圈后回到x位置 int j = 1; for (int i = 0; i < k / 4; i++) { j *= 10000; j %= x1; } for (int i = 0; i < k % 4; i++) { j *= 10; j %= x1; } //剩余j圈 for (int i = 0; i < j; i++) { x2 += m; x2 %= n; } cout << x2 << endl; return 0; }
解法③:快速幂
#include <iostream> using namespace std; long long n, m, k, x; int ksm(long long a, long long b, long long c) { long long d = 1; a %= c; while (b > 0) { if (b % 2 == 1) d = (d * a) % c; b /= 2; a = (a * a) % c; } return d; } int main() { cin >> n >> m >> k >> x; k = ksm(10, k, n * m); k *= m, x += k; cout << x % n; return 0; }
- 1
信息
- ID
- 1461
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 4
- 标签
- 递交数
- 101
- 已通过
- 46
- 上传者