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
 

题意:给你一个数n,让你求1~n中有多少数x符合x%13==0 且x中出现过“13”这个子串。

思路:用dfs(pre,pos,num,flag,limit)来数出所有符合条件的数,其中pre表示这一位的上一位是什么,pos表示当前正循环到哪个位置,num表示到这一位时的总和(%13后,且这一位还没有算),flag表示之前的字符串中是否已经出现过"13"这个字符串,limit表示当前这位是有限制还是没有限制的,有限制的话,这一位最高遍历到wei[pos],没有限制的话最高遍历到9.另外没有剪枝的话会超时,所以用dp[pre][pos][num][flag]表示在limit==0的情况下,后面可以加上的值,如果(pre,pos,flag)这个状态已经遍历过的话,就不用遍历一遍了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 805
int wei[15];
int dp[11][40][15][2];//到i位置%13为j且是否含有13的flag值为k的方案数 int dfs(int pre,int pos,int num,int flag,int limit){
int i,j;
if(pos==0){
if((num%13==0) && (flag==1))return 1;
return 0;
}
if((limit==0) && (dp[pre][pos][num][flag]!=-1) ){
return dp[pre][pos][num][flag];
} int sum=0;
int num1,flag1; if(limit==0){
for(j=0;j<=9;j++){
num1=(num*10+j)%13;
flag1=flag;
if((pre==1) && (j==3) ){
flag1=1;
}
sum+=dfs(j,pos-1,num1,flag1,0);
}
}
else if(limit==1){
for(j=0;j<=wei[pos];j++){
num1=(num*10+j)%13;
flag1=flag;
if((pre==1) && (j==3) ){
flag1=1;
}
if(j<wei[pos])sum+=dfs(j,pos-1,num1,flag1,0);
else sum+=dfs(j,pos-1,num1,flag1,1);
}
} if(limit==0){
dp[pre][pos][num][flag]=sum;
}
return sum;
}
int solve(int x)
{
int i,j,len=0,t=x;
while(t){
wei[++len]=t%10;
t/=10;
}
wei[len+1]=0;
memset(dp,-1,sizeof(dp));
int sum;
sum=dfs(0,len,0,0,1);
return sum;
} int main()
{
int n,m,i,j;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",solve(n));
}
}

hdu3652B-number (数位dp)的更多相关文章

  1. 多校5 HDU5787 K-wolf Number 数位DP

    // 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...

  2. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  3. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  4. HDU 5787 K-wolf Number 数位DP

    K-wolf Number Problem Description   Alice thinks an integer x is a K-wolf number, if every K adjacen ...

  5. Fzu2109 Mountain Number 数位dp

    Accept: 189    Submit: 461Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description One ...

  6. HDU 3709 Balanced Number (数位DP)

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  7. beautiful number 数位DP codeforces 55D

    题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...

  8. FZU - 2109 Mountain Number 数位dp

    Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...

  9. BNU 13024 . Fi Binary Number 数位dp/fibonacci数列

    B. Fi Binary Number     A Fi-binary number is a number that contains only 0 and 1. It does not conta ...

  10. hdu 5898 odd-even number(数位dp)

    Problem Description For a number,if the length of continuous odd digits is even and the length of co ...

随机推荐

  1. VRay for SketchUp渲染图黑原因及解决方案

    很多人都遇到用Vray for SketchUp云渲染的时候,渲染出来的图片是全黑或者是局部是黑色, 这是什么原因呢? 1.有一种情况是,SketchUp的文件储存机制和其他的软件有些不同,它是把模型 ...

  2. 树莓派-4WD智能小车操作小结

    树莓派-4WD智能小车操作小结 树莓派4B-4WD智能小车,双层结构,第一层结构为:小车扩展板(底层)+树莓派主板,通过铜柱隔离固定,小车扩展板相当于计算机的外设扩展板:上面一层为第二层,是三个舵机承 ...

  3. 深入理解Redis之简单动态字符串

    目录 SDS SDS与C字符串的区别 SDS获取字符串长度复杂度为O(1),C字符串为O(N) SDS杜绝了缓存区溢出 减少修改字符串时带来的内存重分配次数 二进制安全 Redis没有直接使用C语言传 ...

  4. Trollcave-suid提权

    一 扫描端口 扫描开放端口:nmap -sV -sC -p- 192.168.0.149 -oA trollcave-allports 扫描敏感目录:gobuster dir -u http://19 ...

  5. ORACLE查找占用临时表空间多的SESSION

    需要使用SYS用户登录查看 /* Formatted on 2020/12/30 上午 11:17:12 (QP5 v5.163.1008.3004) */ SELECT k.inst_id &quo ...

  6. Databricks 第8篇:把Azure Data Lake Storage Gen2 (ADLS Gen 2)挂载到DBFS

    DBFS使用dbutils实现存储服务的装载(mount.挂载),用户可以把Azure Data Lake Storage Gen2和Azure Blob Storage 账户装载到DBFS中.mou ...

  7. 视频画面中实现人脸遮挡教程 - 基于 TensorFlow 实现

    在进行视频通话时,我们往往需要对画面进行一些实时分析,例如识别画面里的人.车.动物等等.这节里我们将使用时信魔方的人脸监视模块实现人脸被手部遮挡的检测,如下图所示效果: 预备知识 时信魔方的客户端使用 ...

  8. 性能测试工具locust简单应用

    简介 Locust是一种易于使用的分布式用户负载测试工具.可用于对网站(或系统)负载测试,并依据响应数据计算出系统支持的并发用户数. 安装及调试(以下操作在windows环境下进行) Locust基于 ...

  9. Redis 实战 —— 06. 持久化选项

    持久化选项简介 P61 Redis 提供了两种不同的持久化方法来将数据存储到硬盘里面. RDB(redis database):可以将某一时刻的所有数据都写入硬盘里面.(保存的是数据本身) AOF(a ...

  10. linux设备注册

    一.分配cdev cdev表示字符设备,使用cdev_alloc函数,cdev_alloc函数原型如下: /** * cdev_alloc() - allocate a cdev structure ...