Educational Codeforces Round 8 D - Magic Numbers 数位DP

题意很简单,偶数位为d,奇数位不为d,且能被m整除的数,叫d-magic,给定d,m,a,b,求在[a,b]上d-magic的个数。

注意 1 既是 0-magic 又是 2/3/4/5/6/7/8/9-magic 

还有值得注意的是,给定的a,b 数位相同。 一开始没用到这个条件,写得挺麻烦~

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<ctime>
using namespace std;
void fre(){freopen("t.txt","r",stdin);}
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
#define ls o<<1
#define rs o<<1|1
#define MS(x,y) memset(x,y,sizeof(x))
#define debug(x) printf("%d",x);
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
const int INF = 1<<30;
const int MAXN = 105;
const double eps = 1e-8;
const double pi = acos(-1.0);
const LL M = 1e9+7;

LL dp[2005][2005][2];
int num[2005];
char s[2005];
LL m,d,all;
LL dfs(int top, int s, bool e, int flag)//top为第几位数,s为模m的余数,e为是否到上限,flag表示偶数位还是奇数位
{
    if (top==0) return (s==0 ) ;
    if (!e && dp[top][s][flag]!=-1) return dp[top][s][flag];
    int i;
    LL res = 0;
    int u = e?num[top]:9;
    if(flag) {if(u >= d) res = dfs(top-1,(s*10+d)%m, e&&d==u,flag^1);}
    else
    {
        for (i = 0; i <= u; ++i)
        {
            if(i == d) continue;//由于a,b数位相同,所以不用考虑前导0的情况。这里为了代码简单就没改,反正都会减掉的。但是当d = 0 的时候,算出来的并不是
准确的d-magic 个数
            res = (res + dfs(top-1, (s*10+i)%m, e&&i==u, flag^1))%M;
        }
    }
    return e?res:dp[top][s][flag]=res;
}
bool check()
{
    int flag = 1,mo = 0;
    for(int i = all; i >= 1; --i)
    {
        if(!flag && num[i]!=d) return 0;
        else if(flag && num[i] == d) return 0;
        mo = ((mo*10)+num[i])%m;
        flag^=1;
    }
    if(mo) return 0;
    else return 1;
}
int main()
{
   // fre();
    int i;
    MS(dp,-1);
    scanf("%I64d%I64d",&m,&d);

    scanf("%s",s); all = strlen(s);
    for(i = 0; s[i]; ++i) num[all-i] = s[i]-'0';
    LL tem1 = dfs(all,0,1,0);
    if(check()) tem1-=1;

    scanf("%s",s);
    for(i = 0; s[i]; ++i) num[all-i] = s[i]-'0';
    LL tem2 = dfs(all,0,1,0);


    printf("%I64d\n",((tem2 - tem1)%M+M)%M );
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值