HDU3652 B-number

B-number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7024    Accepted Submission(s): 4100


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整除的数有几个

思路:数位dp,4维数组表示到len为止,以pre结尾的,和mod13后余sum的,是否已经有13的是有几个,dfs求解


#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <map>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 10000005
#define Mod 10001
using namespace std;
#define LL long long

LL dp[20][20][20][2];
int a[20],index[3000];
int mod;


int dfs(int len,int pre,int sta,int sum,bool limit)
{
    if(len<0)
        return sum==0&&sta;
    if(dp[len][pre][sum][sta]!=-1&&!limit)
        return dp[len][pre][sum][sta];
    int up=limit?a[len]:9;
    int ans=0;
    for(int i=0; i<=up; i++)
    {
        ans+=dfs(len-1,i,sta||(pre==1&&i==3),(sum*10+i)%13,limit&&i==up);
    }
    return limit?ans:dp[len][pre][sum][sta]=ans;
}

int solve(int x)
{
    int cnt=0;
    while(x>0)
    {
        a[cnt++]=x%10;
        x/=10;
    }
    return dfs(cnt-1,-1,0,0,1);
}

int main()
{
    int n;
    memset(dp,-1,sizeof dp);
    while(~scanf("%d",&n))
    {
        printf("%d\n",solve(n));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值