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. iOS 设备的屏幕尺寸一览

    最近在做iPad开发项目 iPhone 设备: iPhone 1G --- 320x480iPhone 3G --- 320x480iPhone 3GS --- 320x480iPhone 4 --- ...

  2. DB2 嵌入式应用中定义游标(开放平台上)

    DECLARE CURSOR statement The DECLARE CURSOR statement defines a cursor. Invocation Although an inter ...

  3. 数据结构之DFS与BFS实现

    本文主要包括以下内容 邻接矩阵实现无向图的BFS与DFS 邻接表实现无向图的BFS与DFS 理论介绍 深度优先搜索介绍 图的深度优先搜索(Depth First Search),和树的先序遍历比较类似 ...

  4. ajax实例1

    前台: function getDetail(index){ $.post("<%=request.getContextPath() %>/member/dbcenter!get ...

  5. 同一内网不能网段ping 不通

    [root@NB sysconfig]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use ...

  6. Web框架本质

    Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:utf- ...

  7. 玩玩Excel下的Power View

    作为微软平台下的数据展示工具,Power View是一个不错的选择.而在Excel 2013下,即使你没有SharePoint的实例那么你也可以玩转它.此篇讲对Excel 2013下的Power Vi ...

  8. html5 方框内的小球

    html5 方框内的小球 版本一 <!DOCTYPE html> <html> <head lang="en"> <meta charse ...

  9. STL Map的使用

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力.下面就通过示例记录一下map的使用: 一.向map中 ...

  10. ASP.NET MVC 4 WebAPI Simple Sample

    // Controllers.cs namespace Microshaoft.WebApi.Controllers { using Microshaoft.WebApi.Models; using ...