原来矩阵快速幂还可以分段玩。
/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL n;
int p;
struct mat {
int num[3][3];
} E, ans;
inline LL mul(LL a, LL b) {
return a * b % p;
}
inline mat matmul(mat A, mat B) {
mat res;
for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) {
res.num[i][j] = 0;
for(int k = 0; k < 3; k++) (res.num[i][j] += mul(A.num[i][k], B.num[k][j])) %= p;
}
return res;
}
inline void calc(LL k, LL n) {
mat trans;
trans.num[0][0] = k; trans.num[0][1] = 0; trans.num[0][2] = 0;
trans.num[1][0] = 1; trans.num[1][1] = 1; trans.num[1][2] = 0;
trans.num[2][0] = 1; trans.num[2][1] = 1; trans.num[2][2] = 1;
mat res = E;
for(; n; n >>= 1, trans = matmul(trans, trans)) if(n & 1) res = matmul(res, trans);
ans = matmul(ans, res);
}
int main() {
scanf("%lld%d", &n, &p);
for(int i = 0; i < 3; i++) E.num[i][i] = 1;
ans = E;
LL cnt = 10;
for(; cnt <= n; cnt *= 10) calc(cnt % p, cnt - cnt / 10);
calc(cnt % p, n - cnt / 10 + 1);
printf("%d\n", ans.num[2][0]);
return 0;
}