B-number HDU - 3652 数字取模 数位DP 寒假集训

统计区间 [1,n] 中含有 ‘13’ 且模 13 为 0 的数字有多少个。

输入格式
多组数据
每行一个正整数n(1 ≤ n ≤ 1e9)
一直输入到文件结束

输出格式
输出一行表示满足条件的答案

样例输入
13
100
200
1000
样例输出
1
1
2
2
多了一维来记录这个当前位置上的数字mod的余数,当板子写把。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define ls (p<<1)
#define rs (p<<1|1)
#define ll long long
using namespace std;
const int maxn = 2e6+5;
const int INF = 0x3f3f3f3f;
ll dp[50][15][50];//dp[pos][pre]记录了遍历第pos为位时,前一位为pre时的状态数
ll a[30],b[30];
// void init(){
//     dp[0][0]=1;
//     dp[0][1]=dp[0][2]=0;
//     for(int i=1;i<25;i++){
//         dp[i][0]=10*dp[i-1][0]-dp[i-1][1];//长度为i不含有49的个数
//         dp[i][1]=dp[i-1][0];//最高位为9但不含49的个数
//         dp[i][2]=10*dp[i-1][2]+dp[i-1][1];//含有49的个数
//     }
// }
int dfs(int lf,int pos,int mod,int pre,bool lim)//lim记录上限
{
    int num,i,ans,modx,prex;
    if(pos<=0)
        return mod == 0 && pre == 2;
    if(!lim && dp[pos][mod][pre] != -1)//没有上限并且已被访问过
        return dp[pos][mod][pre];
    if(lim)
        num=a[pos];
    else num=9;//假设该位是2,下一位是3,如果现在算到该位为1,那么下一位是能取到9的,如果该位为2,下一位只能取到3
    ans = 0;
    for(int i=0;i<=num;i++){
        int tmp=(mod*10+i)%13,n1;
        if(pre==2||pre==1&&i==3) 
            n1=2;
        else if(i==1) n1=1;
        else n1=0;
        ans+=dfs(i,pos-1,tmp,n1,lim&&i==num);
    }
    if(!lim)
        dp[pos][mod][pre] = ans;
    return ans;
}

void solve(){
    ll x,y;
    while(scanf("%lld",&x)!=EOF){
        memset(dp,-1,sizeof(dp));
        memset(a,0,sizeof(a));
        ll len = 0;
        while(x) {
            a[++len] = x % 10;
            x /= 10;
        }
        cout<<dfs(-1,len,0,0,1)<<endl;
    }
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值