C. Add One

http://codeforces.com/contest/1513/problem/C

dp

\(f[i][j]:\)表示当前数位上为i(0~9), 加法次数为j最终形成的数位.

f[i][j] = f[i - 1][j + 1];
f[9][j] = f[1][j + 1] + f[0][j + 1]
// 由依赖关系的先后次序:我们应该先循环 j 再循环 i 

那么我们拿出n中的每一位x, 最终的答案为f[x][k]之和.

#include <iostream>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f 
const int N = 2e5 + 10;
const int p = 1e9 + 7;
int f[10][N];

void init() {
    for (int i = 0; i < 10; ++i) f[i][0] = 1;
    for (int j = 1; j < N; ++j) {
        for (int i = 0; i <= 8; ++i)
            f[i][j] = f[i + 1][j - 1];
        f[9][j] = (f[1][j - 1] + f[0][j - 1]) % p;
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    IO;
    init();
    int _;
    cin >> _;
    while (_--) {
        int n, k;
        cin >> n >> k;
        int ans = 0;
        while (n) {
            ans = (ans + f[n % 10][k]) % p;
            n /= 10;
        }
        cout << ans << '\n';
    }
    return 0;
}
posted @ 2021-04-12 18:37  phr2000  阅读(146)  评论(1编辑  收藏  举报