码迷,mamicode.com
首页 > Windows程序 > 详细

AcWing 311 .月之谜

时间:2020-02-28 22:45:13      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:cstring   res   build   name   com   ble   problem   namespace   mem   

大型补档补了一年

题目链接

考虑枚举月之数的数列和,然后展开dp预处理

设当前模数为 \(P\)
\(f[i][j][k]\) 表示一共有 i 位数字,数位和为 j,数值和 % P 的值为 K

\(f[1][i][i \% P]++\) 初始化 (\(0 <= i <= 9\)

枚举下一位数字 \(c\)
\(f[i + 1][j + c][(k + c * Pow[i]) % P] += f[i][j][k]\)

时间复杂度 \(O(N^2 * S * 10)\)

然后进行典型的数位 \(dp\)
总复杂度上限是 \(O(N ^ 3 * S * 10)\) 大约是 \(5e7\) 的总量级,可以跑过~

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int N = 83, S = 10;
int L, R, Pow[S] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
int f[S][N][N], d[N], n;
void build(int P) {
    memset(f, 0, sizeof f);
    f[0][0][0] = 1;
    for (int i = 0; i <= 9; i++) f[1][i][i % P]++; 
    for (int i = 1; i < S - 1; i++) {
        for (int j = 0; j <= i * 9; j++) {
            for (int k = 0; k < P; k++) {
                for (int c = 0; c <= 9; c++) {
                    f[i + 1][j + c][(k + c * Pow[i]) % P] += f[i][j][k];
                }
            }
        }
    }
}


int inline mod(int a, int b) { return (a % b + b) % b; }
int solve(int x, int P) {
    if (x == 0) return 0;
    n = 0;
    while (x) d[++n] = x % 10, x /= 10;
    int t = 0, q = 0, res = 0;
    for (int i = n; i; i--) {
        for (int j = 0; j < d[i]; j++) 
            if (P - t - j >= 0) res += f[i - 1][P - t - j][mod(P - q - Pow[i - 1] * j, P)];
        t += d[i]; (q += d[i] * Pow[i - 1]) %= P;
        if (i == 1 && q == 0 && t == P) res++;
    }
    return res;
}

int main() {
    scanf("%d%d", &L, &R);
    int ans = 0;
    for (int i = 1; i < N; i++) {
        build(i);
        ans += solve(R, i) - solve(L - 1, i);
    }
    printf("%d\n", ans);
    return 0;
}

AcWing 311 .月之谜

标签:cstring   res   build   name   com   ble   problem   namespace   mem   

原文地址:https://www.cnblogs.com/dmoransky/p/12380612.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!