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
题意:求n以内所有能被13整除且数字里包含13的数的个数
题解:数位DP,用f[i][j][k]表示有i位,最高位为j,对13取模等于k,且数字里包含13的数的个数,g[i][j][k]表示有i位,最高位为j,对13取模等于k,且数字里包含13的数的个数。
  然后一位一位判断就可以了。
代码

#include <stdio.h>
#include <string.h>
int n,m;
int v[20];
int f[12][10][13],g[12][10][13],t[12];
int main()
{
int i,j,k,l,ans,rem,x;
t[1]=1;
for(i=2;i<=10;i++) t[i]=t[i-1]*10;
for(i=0;i<=9;i++) g[1][i][i]=1;
for(i=2;i<=10;i++)
{
for(j=0;j<=9;j++)
{
for(k=0;k<=9;k++)
{
for(l=0;l<=12;l++)
{
if(j==1&&k==3)
{
f[i][j][l]+=(j*t[i]+(k+1)*t[i-1]-1-l)/13-(j*t[i]+k*t[i-1]-1-l)/13;
}
else
{
f[i][j][l]+=f[i-1][k][((l-j*t[i])%13+13)%13];
g[i][j][l]+=g[i-1][k][((l-j*t[i])%13+13)%13];
}
}
}
}
}
while(scanf("%d",&n)!=EOF)
{
memset(v,0,sizeof(v));
rem=m=ans=0;  //此时答案的后i位对13取模应该等于rem
x=n;
while(x)
{
v[++m]=x%10;
x/=10;
}
for(i=m;i>=1;i--)
{
for(j=0;j<v[i];j++) ans+=f[i][j][rem];
if(v[i]>3&&v[i+1]==1)
{
ans+=g[i][3][rem];
}
if(v[i]==3&&v[i+1]==1)
{
ans+=n/13-(n/t[i]*t[i]-1)/13;
break;
}
rem=((rem-v[i]*t[i])%13+13)%13;
}
printf("%d\n",ans);
}
return 0;
}

【HDU3652】B-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. #import、#include、#import<>和#import””的区别

    一.#import与#include #import不会引起交叉编译的问题.因为在Objective-C中会存在C/C++和Object-C混编的问题,如果用#include引入头文件,会导致交叉编译 ...

  2. Codeforces Round #327 (Div. 2)B(逻辑)

    B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. java 学习笔记——网络(Socket)

    阅读方法:将网页放大到200%. 如果你用过用过word应该知道按住ctrl键使用鼠标滚轮缩放.

  4. Sublime Text + CTags + Cscope (部分替代Source Insight)

    CTags & cscope 下载: CTags+Cscope --- 我的百度云盘下载http://pan.baidu.com/s/1gfyPnuN ctags58.zip --- src ...

  5. Shell编程基础教程1--Shell简介

    1.Shell简介 1.1.查看你系统shell信息 cat /etc/shell 命令可以获取Linux系统里面有多少种shell程序 echo $SHELL 命令可以查看当前你所使用的shell是 ...

  6. 如何知道SQL语句的性能和改进途径

    用EXPLAIN吧... EXPLAIN , , , ) \G;

  7. AngularJS——依赖注入

    依赖注入    依赖注入(DI)是一个经典的设计模式, 主要是用来处理组件如何获得依赖的问题.关于DI,推荐阅读Martin Flower的文章(http://martinfowler.com/art ...

  8. 随机生成字符串-php-js

    js <script language="javascript"> function randomString(len) { len = len || 32; var ...

  9. 微软改名部再次大显神威——ASP.NET 5改名ASP.NET Core 1.0

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:在计算机科学领域只有两件难事:缓存不可用和命名.--Phil Karlton 今天,S ...

  10. UML 类关系及画法

    1 泛化 [泛化关系]:是一种继承关系,表示一般与特殊的关系,它指定了子类如何特化父类的所有特征和行为.例如:老虎是动物的一种,即有老虎的特性也有动物的共性. [箭头指向]:带三角箭头的实线,箭头指向 ...