传送门
看到这道题后应该很容易想到矩阵加速递推, 但是构造矩阵时发现有一个lg,不知道怎么办,就只好每翻十倍做一次,结果竟然是正解,汗(⊙﹏⊙)b。(但是我不知道第一页的那些0ms怎么跑的……)
/**************************************************************
Problem: 2326
User: geng4512
Language: C++
Result: Accepted
Time:20 ms
Memory:808 kb
****************************************************************/
#include<cstdio>
#include<cstring>
#define min(a,b) (a)<(b)?(a):(b)
typedef long long LL;
const int MAXN = 18;
LL mod, n, P[MAXN+10];
struct Mat
{
LL a[3][3];
void init(int x)
{
memset(a, 0, sizeof a);
if(x == 1)
a[0][0] = a[1][1] = a[2][2] = 1;
}
Mat operator * (const Mat &t)
{
Mat ans; ans.init(0);
for(int k = 0; k < 3; k++)
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
ans.a[i][j] += a[i][k] * t.a[k][j] % mod;
ans.a[i][j] %= mod;
}
return ans;
}
};
Mat ksm(Mat a, LL k)
{
Mat ans;
ans.init(1);
while(k)
{
if(k & 1) ans = ans * a;
a = a * a;
k >>= 1;
}
return ans;
}
Mat solve(LL x, LL k)
{
Mat a;
a.init(0);
a.a[0][0] = k; a.a[1][0] = a.a[1][1] = a.a[2][0] = a.a[2][1] = a.a[2][2] = 1;
return ksm(a, x);
}
int main()
{
scanf("%lld%lld", &n, & mod);
P[0] = 1;
for(int i = 1; i <= 18; i++)
P[i] = P[i-1] * 10;
Mat a;
a.init(1);
for(int i = 1; ; i++)
{
LL L = P[i-1], R = min(P[i]-1, n);
a = a * solve(R - L + 1, P[i] % mod);
if(R == n) break;
}
printf("%lld", a.a[2][0]);
}