【CF908G】New Year and Original Order(数位DP)

版权声明:湘 ICP 证 66666666 号 | Copyright © 2002-2019, ♂Hany01♂, All Rights Reserved 难道会有人转蒟蒻的博文?? https://blog.csdn.net/hhaannyyii/article/details/82155079

Description

S ( n ) 表示将 n 的十进制各位从小到大排序后的值,求 i = 1 n S ( i )


Solution

f i , j , k , l i m i t 表示前 i 位、有 k 位的值大于 j 、是否达到上界的方案数,转移很显然。
最后计算答案的时候,对于 f i , j , k , l ,我们将答案加上 11...11 k 1 即可。

还是太菜了,根本想不到哇。。。


Code

/************************************************
 * Au: Hany01
 * Date: Aug 28th, 2018
 * Prob: CF908G Original Order
 * Email: [email protected] & [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 705, MOD = 1e9 + 7;

char s[maxn];
int n, f[maxn][10][maxn][2], Ans;

inline void ad(int& x, int y) { if ((x += y) >= MOD) x -= MOD; }

int main()
{
#ifdef hany01
    freopen("cf908g.in", "r", stdin);
    freopen("cf908g.out", "w", stdout);
#endif

    scanf("%s", s + 1), n = strlen(s + 1);
    For(i, 1, 9) f[0][i][0][0] = 1;
    rep(i, n) For(j, 1, 9) For(k, 0, i) For(l, 0, 1) For(m, 0, l ? 9 : (s[i + 1] ^ 48))
        ad(f[i + 1][j][k + (m >= j)][l | (m < (s[i + 1] ^ 48))], f[i][j][k][l]);
    For(i, 1, 9) {
        int t = 1;
        For(j, 1, n) ad(Ans, (LL)t * (f[n][i][j][0] + f[n][i][j][1]) % MOD), t = ((LL)t * 10 + 1) % MOD;
    }
    printf("%d\n", Ans);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/82155079