CodeForce 628D Magic Numbers( 数位DP )

本文介绍了一种使用数位动态规划方法解决特定数学问题的算法——寻找指定范围内,奇数位为特定数字d且能被m整除的所有整数的数量。通过递归的数位DP算法实现,该文详细解释了状态转移方程的设计及其实现过程。

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

Magic Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
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.

求奇数位上为d,偶数为上不为d的同时是m的倍数的数,在[A,B]区间有多少个

思路:

数位dp。dp[pos][md]表示第pos为,模为md有多少中情况。

数位DP全是套路。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
const int mod = 1e9 + 7;
using namespace std;

ll dp[2005][2005];
int m,d,bit[2005],len;
char a[2005],b[2005];

ll dfs( int pos, int md, int over )
{
    if( pos > len )
        return ( md == 0 );
    if( dp[pos][md] != -1 && !over )
        return dp[pos][md];
    int last = over ? bit[pos] : 9;
    ll ans = 0;
    for( int i = 0; i <= last; i++ )
    {
        if( pos%2 && i == d )
            continue;
        if( pos%2 == 0 && i != d )
            continue;
        ans = ( ans + dfs( pos+1, (md*10+i)%m, over && i == last ) )%mod;
    }
    if( !over )
        dp[pos][md] = ans;
    return ans;
}

ll solve( char *s )
{
    len = strlen( s+1 );
    for( int i = 1; i <= len; i++ )
        bit[i] = s[i] - '0';
    return dfs(1,0,1);
}
int main()
{
    memset( dp, -1, sizeof( dp ));
    while( scanf("%d %d",&m,&d) != EOF )
    {
        scanf("%s %s",a+1,b+1);
        int n = strlen( a+1 );
        int tmp,flag;
        flag = tmp = 0;
        for( int i = 1; i <= n; i++ )
        {
            tmp = ( tmp*10 + (a[i] -'0') )%m;
            if( i%2 && a[i]-'0' == d)
                flag = 1;
            if( i%2 == 0&& a[i]-'0'!=d)
                flag = 1;
        }   
        if( tmp != 0 )
            flag = 1;
        printf("%lld\n",(solve(b) - (solve(a) - !flag )+ mod )%mod);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值