Harmony Pairs(数位 dp)

Harmony Pairs

题目描述

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 109 + 7 satisfying 0 ≤ A ≤ B ≤ N 0 ≤ A ≤ B ≤ N 0ABN.

输入描述:

The only line of input contains one integer N (1 ≤ N ≤ 10100).

输出描述:

Output one integer indicating the answer.

输入

100

输出

967

题意

给一个 N N N 满足几个条件, ( 1 ≤ N ≤ 1 0 100 ) (1 ≤ N ≤ 10^{100}) (1N10100)

  1. S ( A ) > S ( B ) S(A) > S(B) S(A)>S(B)
  2. 1 < A < B < N 1 < A < B < N 1<A<B<N
  3. 1 1 1 ~ n n n 中存在几组不同的 A , B A,B AB
#include<bits/stdc++.h>

#define ll long long
using namespace std;
const int mod = 1e9 + 7;
ll dp[110][2020][3][3];
char s[210];
int bit[210];
int n;

ll dfs(int pos, int sum, bool flaga, bool flagb) {
    if (pos < 0) {
        return sum > 1000;
    }
    if (dp[pos][sum][flaga][flagb] != -1) {
        return dp[pos][sum][flaga][flagb];
    }
    ll ans = 0;
    int aend = flaga ? bit[pos] : 9;
    // a
    for (int i = 0; i <= aend; i++) {
        // b
        int bend = flagb ? i : 9;
        for (int j = 0; j <= bend; j++) {
            ans = (ans + dfs(pos - 1,
                             sum - i + j,
                             flaga && (i == aend),
                             flagb && (j == bend)) % mod) % mod;
        }
    }
    return dp[pos][sum][flaga][flagb] = ans;
}

int main() {
    scanf("%s", s);
    n = strlen(s);
    for (int i = 0; i < n; i++) {
        bit[n - i - 1] = s[i] - '0';
    }
    memset(dp, -1, sizeof(dp));
    printf("%lld\n", dfs(n - 1, 1000, 1, 1));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值