hdu 3652 B-number

B-number

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


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   3651  3655  3654  3653  3659 
 

Statistic |  Submit |  Discuss | Note


神奇的数位DP:

dp[i][j][0] 表示位数不超过i,余数为j的含13的个数

dp[i][j][1] 表示位数不超过i,余数为j的不含13且最高位为3的个数

dp[i][j][2] 表示位数不超过i,余数为j的不含13的个数

#include
#include
#include
#include
#include
using namespace std;
int n,m,num[10],md[12];
int dp[20][20][20];
int sqr(int x,int y)
{
	int t=1;
	for (int i=1;i<=y;i++)
	 t=t*10;
	return t;
}
void calc()
{
  md[0]=1;
  for (int i=1;i<=9;i++)
   md[i]=md[i-1]*10;
  memset(dp,0,sizeof(dp));
  dp[0][0][2]=1; 
  for (int i=0;i<=9;i++)
  {
   dp[1][i][0]=0,dp[1][i][2]=1,dp[1][i][1]=0;
  }
  dp[1][3][1]=1;
  for (int i=2;i<=9;i++)
   	{
   		int t=md[i-1];
   		for (int j=0;j<=9;j++)
		  {
		  	int k=(t*j)%13;
		  	for (int l=0;l<=12;l++)
		  	{
		  	 
		  	 dp[i][(k+l)%13][0]+=dp[i-1][l][0]+(j==1?dp[i-1][l][1]:0);
		  	 dp[i][(k+l)%13][1]+=(j==3?dp[i-1][l][2]:0);
		  	 dp[i][(k+l)%13][2]+=dp[i-1][l][2]-(j==1?dp[i-1][l][1]:0);
		    }
		  }  
   	}
}
int solve(int x)
{
    int d[11],len=0;
    for(int i=x;i;i/=10) d[len++]=i%10;
    d[len]=0;
    bool flag=false;
    int ans=0,mod=0;
    for(int i=len-1;i>=0;mod=(mod+d[i]*md[i])%13,i--)
    {
        for(int j=0;j3) ans+=dp[i+1][(13-mod)%13][1];
        if(d[i]>1) ans+=dp[i][(13-(mod+md[i])%13)%13][1];
        if(d[i+1]==1 && d[i]==3) flag=true;
    }
    return ans;
}
int main()
{
	calc();
	while (scanf("%d",&n)!=EOF)
	{
	  int t=solve(n+1);
	  printf("%d\n",t);	
	}
} 


你可能感兴趣的:(动态规划)