CodeForces - 628D

本文介绍了一种使用数位DP方法解决特定整数区间内d-magic数的计数问题,这些数在十进制表示中仅在偶数位置出现指定数字d,并且能够被给定整数m整除。通过分析示例和给出的代码,读者可以了解到如何高效地计算这类数的数量。

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

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.

Example
Input
2 6
10
99
Output
8
Input
2 0
1
9
Output
4
Input
19 7
1000
9999
Output
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.

数位dp不需要什么技巧
不过注意一下范围
这个题的范围比较坑人

#include<iostream>
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
long long  dp[7000][2001];
long long mo=1e9+7;
long long bte[7000];
long long m,d;
long long dfs(int weishu,int biaoji,int yushu,int qz)
{
    if (weishu == 0)return yushu == 0;
    if(biaoji&&dp[weishu][yushu]!=-1)return dp[weishu][yushu];
    int bianjie=biaoji?9:bte[weishu];
    long long jieg=0;
    for(int a=0;a<=bianjie;a++)
    {
        if(qz%2)
        {
            if(a==d)continue;
            jieg+=dfs(weishu-1,!(a==bianjie&&bianjie==bte[weishu]&&biaoji==0),(yushu*10+a)%m,qz+1);
            jieg %= mo;
        }
        else
        {
            if(a!=d)continue;
            jieg+=dfs(weishu-1,!(a==bianjie&&bianjie==bte[weishu]&&biaoji==0),(yushu*10+a)%m,qz+1);
            jieg %= mo;
        }
    }
    if(biaoji)dp[weishu][yushu]= jieg % mo;;
    return jieg %= mo;;
}
int jiejue(string q)
{
    if(q=="0")return 0;
    memset(bte,0,sizeof(bte));
    bte[0] = q.size();
    for (int a = 0; a < bte[0]; a++)
    {
        bte[a + 1] = q[bte[0] - a-1] - '0';
    }
    return dfs(bte[0],0,0,1);
}
int main()
{
    cin>>m>>d;
    string z,y;
    cin>>z>>y;
    memset(dp,-1,sizeof(dp));
    int u=jiejue(y)-jiejue(z);
    memset(bte, 0, sizeof(bte));
    int yy = u;
    bte[0] = z.size();
    for (int a = 0; a < bte[0]; a++)bte[a + 1] = z[bte[0] - a-1] - '0';
    int jc = 1;
    int we = 0;
    for (int a = z.size(); a >=1; a--)
    {
        we *= 10;
        we += bte[a];
        we %= m;
    }
    if (we%m)jc = 0;
    for (int a = bte[0],b=1; a >= 1; a--,b++)
    {
        if (b % 2)
        {
            if (bte[a] == d)jc = 0;
        }
        else
        {
            if (bte[a] != d)jc = 0;
        }
    }
    cout << (u + jc + mo) % mo;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值