B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3461 Accepted Submission(s): 1942
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
題意:求出現13并且能被13整除的數字有多少個。
思路:dfs注意記錄狀态即可。主要還是看代碼吧。
AC代碼如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int T,t,n,m;
int num[20];
int dp[20][13][2][2][2][2];
int dfs(int pos,int left,bool th,bool one,bool th2,bool f)
{
if(pos==0)
return left==0 && th && th2;
if(dp[pos][left][th][one][th2][f]>=0)
return dp[pos][left][th][one][th2][f];
int i,j,k,r,a,b,c,d,f2;
dp[pos][left][th][one][th2][f]=0;
if(f)
r=num[pos];
else
r=9;
for(i=0;i<=r;i++)
{
if(f && i==r)
f2=1;
else
f2=0;
a=(left*10+i)%13;
if((left!=0 && a==0)||th)
b=1;
else
b=0;
if(i==1)
c=1;
else
c=0;
if((one && i==3)|| th2)
d=1;
else
d=0;
dp[pos][left][th][one][th2][f]+=dfs(pos-1,a,b,c,d,f2);
}
return dp[pos][left][th][one][th2][f];
}
int solve()
{
int i,j,k,len=0;
m=n;
while(m)
{
num[++len]=m%10;
m/=10;
}
memset(dp,-1,sizeof(dp));
return dfs(len,0,0,0,0,1);
}
int main()
{
int i,j,k;
while(~scanf("%d",&n))
printf("%d\n",solve());
}