[HNOI2011]数学作业
作者:互联网
嘟嘟嘟
令\(dp[i]\)表示到第\(i\)个数时的答案,很容易列出:
其中\(num[i]\)表示\(i\)的位数。
然后看数据范围,知道这一定得用矩乘优化。可是\(num[i]\)是一个变量啊,这怎么办。
A了后我问坐在旁边的学姐,然后学姐一眼秒:按位数分段啊。
嗯,没了,按位数分段,多次矩乘。
我的写法得开\(unsigned\) \(long\) \(long\),因为枚举的位数\(i\)最大为\(1e19\)。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef unsigned long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
//const int maxn = ;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
ll n, mod;
const int N = 3;
struct Mat
{
ll a[N][N];
Mat operator * (const Mat& oth)const
{
Mat ret; Mem(ret.a, 0);
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
for(int k = 0; k < N; ++k)
ret.a[i][j] += a[i][k] % mod * oth.a[k][j] % mod, ret.a[i][j] %= mod;
return ret;
}
}F, A;
void init(ll x)
{
Mem(F.a, 0);
F.a[0][0] = x; F.a[0][1] = F.a[0][2] = 1;
F.a[1][1] = F.a[1][2] = F.a[2][2] = 1;
}
Mat quickpow(Mat A, ll b)
{
Mat ret; Mem(ret.a, 0);
for(int i = 0; i < N; ++i) ret.a[i][i] = 1;
for(; b; b >>= 1, A = A * A)
if(b & 1) ret = ret * A;
return ret;
}
ll solve(ll L, ll R, ll x, ll las)
{
init(x % mod);
A = quickpow(F, R - L);
L %= mod;
return ((A.a[0][0] * las % mod + A.a[0][1] * L % mod) % mod + A.a[0][2]) % mod;
}
int main()
{
n = read(); mod = read();
ll las = 0, i = 10;
for(; i - 1 <= n; i *= 10) las = solve(i / 10 - 1, i - 1, i, las);
if(i / 10 <= n) las = solve(i / 10 - 1, n, i, las);
write(las), enter;
return 0;
}
标签:Mat,int,ll,作业,ret,HNOI2011,数学,include,mod 来源: https://blog.51cto.com/u_15234622/2831200