B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1197 Accepted Submission(s): 627
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整除
然后再合在一起即可
至于被13整除,每一步维护一个mod即可(具体看代码)
测评情况(HDU)
![]()
C++ AC Code
/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>
#include<cstring>
#define max(a,b) ((a)>(b)?(a):(b))
int n;
int L[30],R[30],len;
int f[30][2][2][15][2][20];
int predoing(int a,int *num)
{
int le=0;
while(a)
{
num[++le]=a%10;
a/=10;
}
return le;
}
int calc(int pos,int d,int u,int last,int flag,int mod)
{
if(pos==0) return flag&&mod==0;
int &res=f[pos][d][u][last][flag][mod];
if(res!=-1) return res;
res=0;
int st=d?L[pos]:0;
int ed=u?R[pos]:9;
for(int i=st;i<=ed;i++)
{
bool tmp=(i==3&&last==1);
res+=calc(pos-1,d&&i==L[pos],u&&i==R[pos],i,flag||tmp,(mod*10+i)%13);
}
return res;
}
void work()
{
memset(f,-1,sizeof(f));
memset(L,0,sizeof(L));
memset(R,0,sizeof(R));
len=predoing(1,L);
len=max(len,predoing(n,R));
printf("%d\n",calc(len,1,1,0,0,0));
}
int main()
{
freopen("hdu3652.in","r",stdin);
freopen("hdu3652.out","w",stdout);
while(scanf("%d",&n)==1) work();
return 0;
}
本文介绍了一种求解B-number的算法实现,B-number是指十进制形式中包含子字符串'13'且能被13整除的非负整数。通过递归状态压缩的方法,文章提供了一个C++代码示例,用于计算从1到n范围内所有符合条件的B-number的数量。

376

被折叠的 条评论
为什么被折叠?



