codeforces 628D - Magic Numbers(数位dp)

本文深入解析了D.MagicNumbers题目,通过数位DP算法解决寻找特定区间内满足特定条件的d-magic数的问题。文章详细介绍了数位DP的状态定义、转移过程以及如何处理大数情况,提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Magic Numbers

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.

For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic.

Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).

Input

The first line contains two integers m, d (1 ≤ m ≤ 2000, 0 ≤ d ≤ 9) — the parameters from the problem statement.

The second line contains positive integer a in decimal presentation (without leading zeroes).

The third line contains positive integer b in decimal presentation (without leading zeroes).

It is guaranteed that a ≤ b, the number of digits in a and b are the same and don't exceed 2000.

Output

Print the only integer a — the remainder after dividing by 109 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.

Examples

input

Copy

2 6
10
99

output

Copy

8

input

Copy

2 0
1
9

output

Copy

4

input

Copy

19 7
1000
9999

output

Copy

6

Note

The numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96.

The numbers from the answer of the second example are 2, 4, 6 and 8.

The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747.

题意:定义一个数为好数同时满足下面三个条件:

1、所有奇数位上的数字都不为d

2、所有偶数位上的数字都为d

3、这个数能整除m

现在给你m,d以及区间[a,b],让你求a,b之间有多少好数。

思路:显然是数位dp,但是比赛的时候我没调出来。先看大佬的总结:

1、因为是大数,所以边界不能直接-1,可以用字符串模拟大数-1,也可以单独判断一下边界。
2、怎样判断除尽,只需要我们每次保留余数就可以了
3、怎样记录状态   首先是位置,然后是余数 
为什么是余数呢?
因为当前的余数,_yu_  _,表示的是到当前位所剩下的, 也就是说
当前位取遍所有的0-9之后,后面所有的结果。
它也是一种计数,只是不同的一个标准,但是他们互斥,并且%的
所有的情况就是[0,m) ,这样就可以不重不漏的算完所有的情况
上三条来自 暴力都不会_QAQ 

我的总结:

1、仔细读题!!!是所有偶数位必须为d,所有奇数位必须不为d。

2、偶数位奇数位是从最高位开始算的,所以我们dp的时候从最高位开始。因为要整除m,每次把余数*10+当前的数字。

3、用字符串模拟大数字,同样是solve(y)-solve(x-1),这里我们不需要x-1,只需要单独判断一下x这个数是否符合条件就可以了。

4、第二维我直觉是用余数,但详细证明参加上面第三条。。。

代码:

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 30
#define inf 1<<29
#define MOD 2007
#define LL long long
using namespace std;
typedef long long ll;
const ll mo=1e9+7;
char s[2010],ss[2010];
ll dp[2010][2010],a[2010];
ll m,d,jo;
int len;
ll dfs(int pos,ll sum,bool limit)
{
    if(pos==len) return sum==0;
    if(!limit && dp[pos][sum]!=-1) return dp[pos][sum];
    int up=limit ? a[pos] : 9;
    ll tmp=0;
    for(int i=0;i<=up;i++)
    {
        if(pos&1)
        {
            if(i==d)
            tmp=(tmp+dfs(pos+1,(sum*10+i)%m,limit&&i==a[pos]))%mo;
        }
        else if(i!=d)
        tmp=(tmp+dfs(pos+1,(sum*10+i)%m,limit&&i==a[pos]))%mo;
    }
    if(!limit) dp[pos][sum]=tmp;
    return tmp;
}
int solve(char *s)
{
    int pos=strlen(s)-1;
    len=0;
    while(len<=pos)
    {
        a[len]=s[len]-'0';
        len++;
    }
    //printf("%s\n",s);
    ll as=dfs(0,0,true);
    return as;
}
int main()
{
    int le,ri;
    while(scanf("%lld%lld",&m,&d)!=EOF)
    {
        memset(dp,-1,sizeof(dp));
        scanf("%s%s",s,ss);
        int l=strlen(s);
        ll spc=1,sm=0;
        for(int i=0;i<l;i++)
        {
        if((i%2==0)&&((s[i]&15)==d)) {spc=0;break;}
        else if((i&1)&&((s[i]&15)!=d)) {spc=0;break;}
        sm=(sm*10+(s[i]&15))%m;
        }
        if(spc) spc=(sm==0);
        printf("%lld\n",(solve(ss)-solve(s)+spc+mo)%mo);
    }
    return 0;
}

 

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值