HDU——B-number(数位DP)

题目大意:

要找出1到n之间有多少个数含13,并且能被13整除

记忆化搜索:

dp[pos][pre][mod][statu],pos位数,pre前一位,mod余数,statu状态

有2个状态:含13,不含13

<span style="font-size:18px;"><strong>#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<cctype>
#define inf 0x3f3f3f3f
#define maxn 10000
typedef long long LL;
using namespace std;
int dp[15][15][15][2];
int n,pos,num[11];
int dfs(int pos,int s,int pre,int z,int e)
{
    if(pos==-1) return s==1&&!z; //含13并且能被13整除
    if(!e&&dp[pos][pre][z][s]!=-1) return dp[pos][pre][z][s];
    int end=e?num[pos]:9;
    int sum=0;
    for(int i=0;i<=end;i++){
        int mod=(z*10+i)%13;
        if(pre==1&&i==3) sum+=dfs(pos-1,1,i,mod,e&&i==end);
        else sum+=dfs(pos-1,s,i,mod,e&&i==end);
    }
    return e?sum:dp[pos][pre][z][s]=sum;
}

void Init(int x)
{
    pos=0;
    while(x){
        num[pos++]=x%10;
        x/=10;
    }
    memset(dp,-1,sizeof dp);
}
int cal(int x)
{
    Init(x);
    return dfs(pos-1,0,0,0,1);
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        printf("%d\n",cal(n));
    }
    return 0;
}</strong></span>

HDU——B-number(数位DP)

时间: 2024-12-20 10:38:46

HDU——B-number(数位DP)的相关文章

hdu 3709Balanced Number(数位dp)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2640    Accepted Submission(s): 1196 Problem Description A balanced number is a non-negative integer that can be balanced if a p

[ACM] hdu 3555 Bomb (数位DP,统计1-N中含有“49”的总数)

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 7187 Accepted Submission(s): 2512 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impro

Hdu3079Balanced Number数位dp

枚举支点,然后就搞,记录之前的点的力矩和. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <s

SPOJ MYQ10 Mirror Number 数位dp&#39;

题目链接:点击打开链接 MYQ10 - Mirror Number A number is called a Mirror number if on lateral inversion, it gives the same number i.e it looks the same in a mirror. For example 101 is a mirror number while 100 is not. Given two numbers a and b, find the number

多校5 HDU5787 K-wolf Number 数位DP

1 // 多校5 HDU5787 K-wolf Number 数位DP 2 // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d 3 // f 用作标记,当现在枚举的数小于之前的数时,就不用判断i与dig[pos]的大小 4 // 整体来说就,按位往后移动,每次添加后形成的数都小于之前的数,并且相邻k位不一样,一直深搜到cnt位 5 // http://blog.csdn.net/weizhuwyzc000/article/details/5209769

hdu 5787 K-wolf Number 数位dp

数位DP 神模板 详解 为了方便自己参看,我把代码复制过来吧 // pos = 当前处理的位置(一般从高位到低位) // pre = 上一个位的数字(更高的那一位) // status = 要达到的状态,如果为1则可以认为找到了答案,到时候用来返回, // 给计数器+1. // limit = 是否受限,也即当前处理这位能否随便取值.如567,当前处理6这位, // 如果前面取的是4,则当前这位可以取0-9.如果前面取的5,那么当前 // 这位就不能随便取,不然会超出这个数的范围,所以如果前面取

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

HDU 3709 Balanced Number (数位DP)

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3798    Accepted Submission(s): 1772 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

hdu 5898 odd-even number(数位dp)

Problem Description For a number,if the length of continuous odd digits is even and the length of continuous even digits is odd,we call it odd-even number.Now we want to know the amount of odd-even number between L,R(1<=L<=R<= 9*10^18). Input Fir