hdu 3652 13倍数

传送门

转自:这里

Problem Description 
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string “13” and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.

Input 
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).

Output 
Print each answer in a single line.

Sample Input

13 
100 
200 
1000

Sample Output




2

题意:找出1~n范围内含有13并且能被13整除的数字的个数

思路:使用记忆化深搜来记录状态,配合数位DP来解决、

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define me(x,y) memset(x,y,sizeof(x));
typedef long long ll;
int a[20];
int dp[20][20][4];

int dfs(int pos,int mod,int sta,int limit)
{
    int mod_x,sta_x;
    if(pos==-1) return mod == 0 && sta == 2;
    if(!limit && dp[pos][mod][sta]!=-1) return dp[pos][mod][sta];
    int up=limit ? a[pos] : 9;
    int ans=0;
    for(int i=0;i<=up;i++)
    {
        mod_x=(mod*10+i)%13;
        sta_x=sta;
       // cout<<mod_x<<" "<<sta_x<<endl;
        if(sta==0&&i==1)  sta_x=1;
        if(sta==1&&i!=1)  sta_x=0;
        if(sta==1&&i==3)  sta_x=2;
        ans+=dfs(pos-1,mod_x,sta_x,limit && i==up);
        //if(i==4) continue;//都是保证枚举合法性
    }
    if(!limit) dp[pos][mod][sta]=ans;
    return ans;
}
int solve(int x)
{
    int pos=0;//
    while(x)
    {
        a[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,0,true);
}
int main()
{
    int b;
    //while(~scanf("%d%d",&a,&b))
    while(~scanf("%d",&b))
    {
        me(dp,-1);
        printf("%d\n",solve(b));
    }
    return 0;
}

附:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int bit[15];
int dp[15][15][3];
//dp[i][j][k]
//i:数位
//j:余数
//k:3种操作状况,0:末尾不是1,1:末尾是1,2:含有13

int dfs(int pos,int mod,int have,int lim)//lim记录上限
{
    int num,i,ans,mod_x,have_x;
    if(pos<=0)
        return mod == 0 && have == 2;
    if(!lim && dp[pos][mod][have] != -1)//没有上限并且已被访问过
        return dp[pos][mod][have];
    num = lim?bit[pos]:9;//假设该位是2,下一位是3,如果现在算到该位为1,那么下一位是能取到9的,如果该位为2,下一位只能取到3
    ans = 0;
    for(i = 0; i<=num; i++)
    {
        mod_x = (mod*10+i)%13;//看是否能整除13,而且由于是从原来数字最高位开始算,细心的同学可以发现,事实上这个过程就是一个除法过程
        have_x = have;
        if(have == 0 && i == 1)//末尾不是1,现在加入的是1
            have_x = 1;//标记为末尾是1
        if(have == 1 && i != 1)//末尾是1,现在加入的不是1
            have_x = 0;//标记为末尾不是1
        if(have == 1 && i == 3)//末尾是1,现在加入的是3
            have_x = 2;//标记为含有13
        ans+=dfs(pos-1,mod_x,have_x,lim&&i==num);//lim&&i==num,在最开始,取出的num是最高位,所以如果i比num小,那么i的下一位都可以到达9,而i==num了,最大能到达的就只有,bit[pos-1]
    }
    if(!lim)
        dp[pos][mod][have] = ans;
    return ans;
}

int main()
{
    int n,len;
    while(~scanf("%d",&n))
    {
        memset(bit,0,sizeof(bit));
        memset(dp,-1,sizeof(dp));
        len = 0;
        while(n)
        {
            bit[++len] = n%10;
            n/=10;
        }
        printf("%d\n",dfs(len,0,0,1));
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值