很过分的事更何况是
2026-09-23 16:58:17
发布于:浙江
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i128 = __int128;
ll M;
struct Mat {
ll a[3][3];
Mat() { memset(a, 0, sizeof(a)); }
Mat operator*(const Mat& other) const {
Mat res;
for (int i = 0; i < 3; ++i)
for (int k = 0; k < 3; ++k) {
if (a[i][k] == 0) continue;
for (int j = 0; j < 3; ++j) {
res.a[i][j] = (res.a[i][j] + a[i][k] * other.a[k][j]) % M;
}
}
return res;
}
};
Mat mat_pow(Mat base, ll exp) {
Mat res;
for (int i = 0; i < 3; ++i) res.a[i][i] = 1;
while (exp) {
if (exp & 1) res = res * base;
base = base * base;
exp >>= 1;
}
return res;
}
int get_len(i128 x) {
if (x == 0) return 1;
int len = 0;
while (x > 0) {
++len;
x /= 10;
}
return len;
}
ll pow10_mod(int d) {
ll res = 1 % M;
ll base = 10 % M;
while (d) {
if (d & 1) res = res * base % M;
base = base * base % M;
d >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll L, A, B;
cin >> L >> A >> B >> M;
i128 s = A;
ll ans = 0;
ll idx = 0;
while (idx < L) {
int d = get_len(s);
i128 pow10_val = 1;
for (int i = 0; i < d; ++i) pow10_val *= 10;
ll k;
if (B == 0) {
k = L - idx;
} else {
i128 diff = pow10_val - s;
i128 m = (diff + B - 1) / B;
if (m > L - idx) m = L - idx;
k = (ll)m;
}
ll p10 = pow10_mod(d);
Mat trans;
trans.a[0][0] = p10; trans.a[0][1] = 1; trans.a[0][2] = 0;
trans.a[1][0] = 0; trans.a[1][1] = 1; trans.a[1][2] = B % M;
trans.a[2][0] = 0; trans.a[2][1] = 0; trans.a[2][2] = 1;
Mat Tk = mat_pow(trans, k);
ll s_mod = s % M;
ll new_ans = (Tk.a[0][0] * ans + Tk.a[0][1] * s_mod + Tk.a[0][2]) % M;
ans = new_ans;
s = s + (i128)k * B;
idx += k;
}
cout << ans << '\n';
return 0;
}
这里空空如也








有帮助,赞一个