【数位DP】Hdu 3652:B-number

时间:2023-03-09 17:57:40
【数位DP】Hdu 3652:B-number

                    B-number

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

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

  感觉拿到题目后没有什么思路。
  去%了一发题解。
  发现状态可以是多种多样的。
  设f[i][b][j][m]表示i位,第i位为j,b=0代表当前数字没有13,b=1相反,m代表%13等于m的所有数字的个数。
  在预处理里面直接加判一些东西,至于那个count函数指第i位为j的所有数字从之前%13=m的数字转移来的时候要加的mod数。
  说的再清楚一点就是以前mod 13=m,现在=(count(i,j)+m)%13
  其余的非法情况什么的再判一下咯。。
  
 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring> using namespace std; int f[][][][]; int count(int x,int y)
{
for(int i=;i<x;i++)
y=y*%;
return y;
} void DP()
{
for(int i=;i<=;i++)
f[][][i][i]=;
for(int i=;i<=;i++)
for(int j=;j<=;j++)
{
int mod=count(i,j);
for(int kk=;kk<=;kk++)
for(int ll=;ll<;ll++)
{
if(j== && kk==)f[i][][j][ll]+=f[i-][][kk][(ll-mod+)%];
else f[i][][j][ll]+=f[i-][][kk][(ll-mod+)%];
f[i][][j][ll]+=f[i-][][kk][(ll-mod+)%];
}
}
} int get(int x)
{
int num[],len=,res=;
while(x)
{
num[++len]=x%;
x/=;
}
for(int i=;i<num[len];i++)
res+=f[len][][i][];
for(int i=;i<=len-;i++)
for(int j=;j<=;j++)
res+=f[i][][j][];
int mod=count(len,num[len]),flag=;
for(int i=len-;i>=;i--)
{
for(int j=;j<num[i];j++)
{
res+=f[i][][j][(-mod)%];
if(flag || (j== && num[i+]==) )res+=f[i][][j][(-mod)%];
}
if(num[i]==&&num[i+]==)flag=;
mod=(mod+count(i,num[i]))%;
}
return res;
} int main()
{
int r;
DP();
while(~scanf("%d",&r))
printf("%d\n",get(r+));
return ;
}