题解:CF1327E Count The Blocks

· · 题解

题意:

你将所有从 0 到 10^n-1 的整数写了下来,并在它们的前面补零,使得它们的长度恰好为 n。例如,如果 n=3,你写下的数依次为 000,001,…,998,999。一段连续的极长数字叫做“块”,现在问你写下的数中对于 1 到 n 的整数 i,长度为 i 的块的数量。n\le 2\times10^5。

做法:

先打表找规律,设 ans_i 为长度为 i 的块数。

n ans
1 10
2 180 10
3 2610 180 10
4 34200 2610 180 10
5 423000 34200 2610 180 10

不难发现 ans_n 始终为 10,因为长度为 n 的合法方案只有 n 个 0,1,2, \cdots 9,还能发现 n 的答案就是 n-1 的答案前边加一个数。

接下来对于每个 i 分讨三种情况:

  1. 块在开头:1 \cdots i 都应该是一样的,10种方案,然后 i+1 不能填和前边一样的,9 种方案,i+2 \cdots n 没有限制,随便选,10^{n-i-1} 种方案。

  2. 块在结尾:同理,只是把在开头情况的整个序列翻转过来。

  3. 块在中间:块头不在整个序列开头或结尾,只有 n-i-1 个位置,这 i 个数相同,有 10 种方案,两边不能和中间一样,9\times9=81 种,其余随便放 10^{n-i-2}种。

全都加在一起得到:

180\times 10^{n-i-1}+810(n-i-1)\times10^{n-i-2}&i\le n-1\\10&i=n \end{cases}

Code.

把式子写成了一个更好看的形式。


#include<bits/stdc++.h>
const int mod=998244353;
#define int long long
using namespace std;
int n;
int sum=0;
int qpow(int a,int b){
    int res=1;
    while(b){
        if(b&1) res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=n;i>=2;i--){
        cout<<qpow(10,i-2)*(81*i+18)%mod<<' ';
    }
    cout<<10;
    return 0;
}