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
 

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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
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;j<d[i];j++) ans+=dp[i][(13-(mod+j*md[i])%13)%13][0];
        if(flag)//如果原数列中有13,那么之后的数就都是合法的
        {
            for(int j=0;j<d[i];j++) ans+=dp[i][(13-(mod+j*md[i])%13)%13][2];
            continue;
        }
        if(d[i+1]==1 && d[i]>3) 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);	
	}
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值