Codeforces 628D Magic Numbers 【数位dp】


题目链接:点我


定义 d-数:数字d有且只在偶数位上,奇数位不能出现数字d。(0<=d<=9)

题意:给定一个区间[L, R],问区间里面可以被m整除的d-数个数。


思路:数位dp,dp[i][j]表示处理到第i位%m == j的方案数。套个记忆化外壳就OK了。

10^2000太大了,端点需要特判一下。


AC代码:


#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#define CLR(a, b) memset(a, (b), sizeof(a))
#define PI acos(-1.0)
using namespace std;
typedef long long LL;
typedef double DD;
const int MAXN = 1e5+10;
const int MOD = 1e9+7;
int m, d, L;
int bit[2001];
LL dp[2001][2001];
void add(LL &x, LL y) {x = (x + y) % MOD;}
LL DFS(int pos, int preyu, bool yes)
{
    if(pos == L) return preyu == 0;
    if(!yes && dp[pos][preyu] != -1) return dp[pos][preyu];
    LL ans = 0;
    int End = yes ? bit[pos] : 9;
    for(int i = 0; i <= End; i++)
    {
        if(pos % 2 == 0 && i == d) continue;
        if(pos % 2 == 1 && i != d) continue;
        add(ans, DFS(pos+1, (preyu*10+i)%m, yes&&i==End));
    }
    if(!yes) dp[pos][preyu] = ans;
    return ans;
}
LL Count(string str)
{
    int len = str.size(); int cnt = 0;
    for(int i = 0; i < len; i++) bit[i] = str[i] - '0';
    return DFS(0, 0, 1);
}
bool judge(string str)
{
    int len = str.size(); int yu = 0;
    for(int i = 0; i < len; i++)
    {
        int v = str[i] - '0';
        yu = yu * 10 + v; yu %= m;
        if((i + 1) & 1 && v == d) return false;
        if(i & 1 && v != d) return false;
    }
    return yu == 0;
}
int main()
{
    CLR(dp, -1);
    cin >> m >> d;
    string a, b; cin >> a >> b; L = a.size();
    //cout << judge(a) << ' ';
    cout << (Count(b) - Count(a) + judge(a) + MOD) % MOD << endl;
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值