CF908G New Year and Original Order(数位dp)

题面

CodeForces
luogu

题解

数位dp

\(f[i][j][k][l]\)表示当前在第\(i\)位有\(j\)位大于等于\(k\),当前有没有卡上界的方案数
则枚举新加的数\(p\)
\(f[i+1][j+(p≥k)][k][l|(p<a_i)]=∑f[i][j][k][l]\)

我们最后统计答案的时候枚举\(k\)
\(ans=111...11(j个1)∗(f[n][j][k][0]+f[n][j][k][1])\)

重点讲一下为什么是乘以\(j\)\(1\)

首先一个排了序后的数可以看作阶梯状的东西, 可以用 一些\(111...11\)的和来表示

例如: \(1144455 = 1111111+11111*3+11\)

然后我们发现对于\(f[n][j][k][0/1]\)这个状态的贡献就是\(11...11(j个1)\)

例如上面那个例子可以由

\(f[n][7][1][0/1],f[n][5][2][0/1],f[n][5][3][0/1],f[n][5][4][0/1],f[n][2][5][0/1]\)

这几种情况表示

那么 就变成了 \(1111111+11111+11111+11111+11 = 1144455\)

神仙吧!

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register

using namespace std;
template<class T> inline void read(T &x) {
    x = 0; RG char c = getchar(); bool f = 0;
    while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
    while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
    x = f ? -x : x;
    return ;
}
template<class T> inline void write(T x) {
    if (!x) {putchar(48);return ;}
    if (x < 0) x = -x, putchar('-');
    int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
    for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}

const int N = 750, Mod = 1000000007;
char s[N];
int a[N];
int f[N][N][10][2];
//设f[i][j][k][l]表示当前在第i位有j位大于等于k,当前有没有卡上界的方案数
//0表示卡上界, 1表示没卡上界
int main() {
    //freopen(".in", "r", stdin);
    //freopen(".out", "w", stdout);
    scanf("%s", s);
    int n = strlen(s);
    for (int i = 1; i <= n; i++)
        a[i] = s[i-1]-'0';
    for (int i = 0; i <= 9; i++) f[0][0][i][0] = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= i; j++)
            for (int k = 1; k <= 9; k++)
                for (int l = 0; l <= 1; l++) {
                    int x = l ? 9 : a[i+1];
                    for (int p = 0; p <= x; p++)
                        (f[i+1][j+(p>=k)][k][l|(p < a[i+1])] += f[i][j][k][l]) %= Mod;
                }
    int ans = 0;
    for (int k = 1; k <= 9; k++) {
        int res = 1;
        for (int i = 1; i <= n; i++)
            (ans += 1ll*res*(f[n][i][k][0]+f[n][i][k][1])%Mod) %= Mod, res = (10ll*res+1)%Mod;
    }
    printf("%d\n", ans);		  
    return 0;
}


posted @ 2019-01-07 20:55  zzy2005  阅读(145)  评论(0编辑  收藏  举报