【HDU 3652】 B-number (数位DP)

B-number

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

1
1
2
2

Author

wqb0039

Source

2010 Asia Regional Chengdu Site —— Online Contest

【题意】

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

【分析】

  f[i][j][k]表示填i个数,模13余数为j,状态为k的方案数。
  k=0表示没有‘13’且末位不为1,k=1表示没有‘13’但末位为1,k=3为含有‘13’。

  这是数位DP模版题啊。。感觉我真的从难往简单做了,第一题搞的我,这酸爽!!

  我还是too naive啊,一开始弄k表示状态想得超复杂,觉得要记录含不含k以及上一位是什么,其实很多状态是没用的嘛,只要知道上一位是不是1就好了,我是如此搞笑!

代码如下:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<cmath>
 8 using namespace std;
 9 #define LL long long
10
11 int f[20][20][3],a[20];
12 int c;
13
14 LL ffind(int n,int k,int tt,bool flag)
15 {
16     if(n==0) return (tt==2&&k==0);
17     if(!flag&&f[n][k][tt]!=-1) return f[n][k][tt];
18     LL ans=0;
19     int ed=flag?a[n]:9;
20     for(int i=0;i<=ed;i++)
21     {
22         int nt=0;
23         if(i==1) nt=1;
24         if(tt==1&&i==3) nt=2;
25         if(tt==2) nt=2;
26         ans+=ffind(n-1,(k*10+i)%13,nt,(flag&&(i==ed)));
27     }
28     if(!flag) f[n][k][tt]=ans;
29     return ans;
30 }
31
32 LL get_ans(int n)
33 {
34     int x=n;
35     c=0;
36     while(x) a[++c]=x%10,x/=10;
37     return ffind(c,0,0,1);
38 }
39
40 int main()
41 {
42     memset(f,-1,sizeof(f));
43     int n;
44     while(scanf("%d",&n)!=EOF)
45     {
46         LL ans=get_ans(n);
47         printf("%lld\n",ans);
48     }
49     return 0;
50 }

[HDU 3652]

记得100年前,没做过几道数位DP的我比赛时打了个高精数位DP那么勇猛,还AC了,现在真是返老还童了ORZ!!

2016-10-08 21:28:51

  

时间: 2024-08-02 02:41:12

【HDU 3652】 B-number (数位DP)的相关文章

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

hdu 3565 Bi-peak Number 数位dp

题意:各位数字先增后减的数称为峰值数(位数大于等3且第一位非零),然后两个峰值数连在一起是一个Bi-peak数, 求两个数之间Bi-peak数的各位数字之和的最大值. 思路:设dp[pos][i][j]表示当前考虑pos位,之前的数位为i,状态为j,与之后(pos+1)位组合构成Bi-peak number,这(pos+1)位数位和的 最大值.状态总共有7种,st=0,初始状态:st=1,恰好有一个在第一个波峰的上坡上:st=2,前面至少有两个在第一个波峰的上 坡上; st=3,在第一个波峰的下

HDU 3652 B-number(数位DP)

题意:统计区间 [1,n] 中含有 '13' 且模 13 为 0 的数字有多少个. 分析:由 (HDU 2089 不要62)和(CF 55D - Beautiful numbers)想到该题做法,dp[i][j][f][mod],长度为i,前缀是否为1,是否已符合条件,余数为mod的数字个数. #include <map> #include <set> #include <list> #include <cmath> #include <queue&g

HDU 2089 不要62(数位DP,三种姿势)

HDU 2089 不要62(数位DP,三种姿势) ACM 题目地址:HDU 2089 题意: 中文题意,不解释. 分析: 100w的数据,暴力打表能过 先初始化dp数组,表示前i位的三种情况,再进行推算 直接dfs,一遍搜一变记录,可能有不饥渴的全部算和饥渴的部分算情况,记录只能记录全部算(推荐看∑大的详细题解Orz) 代码: 1. 暴力 (以前写的) /* * Author: illuz <iilluzen[at]gmail.com> * File: 2089_bf.cpp * Create

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

[ACM] hdu 2089 不要62(数位Dp)

不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19043    Accepted Submission(s): 6442 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就

Hdu3079Balanced Number数位dp

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