Harmony Pairs

链接:https://ac.nowcoder.com/acm/contest/5671/H
来源:牛客网

题目描述
Roundgod is obsessive about numbers. Let S ( x ) S(x) S(x) be the sum of the digits of xx in decimal notation, ( A , B ) (A,B) (A,B) is a harmony pair if and only if S ( A ) > S ( B ) S(A)>S(B) S(A)>S(B). Roundgod is given N N N, and she wants to count the number of harmony pairs ( A , B ) (A,B) (A,B) modulo 1 0 9 + 7 10^9+7 109+7 satisfying 0 ≤ A ≤ B ≤ N 0\le A\le B\le N 0ABN.
输入描述:
The only line of input contains one integer N   ( 1 ≤ N ≤ 1 0 100 ) N\ (1\le N\le 10^{100} ) N (1N10100).
输出描述:
Output one integer indicating the answer.
示例1

输入
100
输出
967

数位dp。
f [ u ] [ d ] [ l i m a ] [ l i m b ] f[u][d][lima][limb] f[u][d][lima][limb] u u u表示当前为从高向低数第 u u u位; d d d表示前 u u u位的 S ( A ) − S ( B ) + b a s e = d S(A)-S(B)+base=d S(A)S(B)+base=d l i m a lima lima表示 A A A的第 u u u位的最大值是 B B B的第 u u u位还是 9 9 9 l i m b limb limb表示 B B B的第 u u u位的最大值是 N N N的第 u u u位还是 9 9 9

#include

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector
#define pii pair
#define mii unordered_map
#define msi unordered_map
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int base = 1000, N = 105, MOD = 1e9 + 7;
char a[N];
int f[N][base << 1][2][2], n;
vi seq;

int dfs(int u, int d, int lima, int limb) {
    if (u == seq.size())return d > base;
    if (f[u][d][lima][limb] != -1)return f[u][d][lima][limb];
    int lb = limb ? seq[u] : 9;
    ll res = 0;
    repi(i, 0, lb) repi(j, 0, (lima ? i : 9))
            res = (res + dfs(u + 1, d + j - i, lima && (j == i), limb && (i == seq[u]))) % MOD;
    return f[u][d][lima][limb] = res;
}

int main() {
    ss(a + 1);
    n = strlen(a + 1);
    repi(i, 1, n)seq.pb(a[i] - '0');
    memset(f, -1, sizeof(f));
    pi(dfs(0, base, 1, 1));
    return 0;
}

你可能感兴趣的:(数位dp,ACM)