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;
}