这道比较繁琐。。除了处理13之外还得处理整除,这样就无法用排除的方式筛选了。。
方程大概是这样的。。d[i][j][k][v]为扫到i位,末位为j,余数为v(假设后面全为0),k代表前面是否找到13
然后转移按照题目要求转移,这对记忆化搜索来说不难。。
方程维数增多对想出方程和实现过程都带来了一定难度。。所以还是得适应高维的方程才行。。
然后需要注意的是满足题目要求的并不多。。即d里面有很多是空的,所以还是得手动做标记来记忆化比较好。。
其他都是细节问题。。没什么好讲。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1000000007
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 15
#define nm 1000005
#define pi 3.141592653
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,c[NM],d[NM][NM][2][NM],b[NM],_x;
bool V[NM][NM][2][NM];
int dfs(int i,bool f,int t,bool f1,int v){
if(!i)if(f1&&!v)return 1;else return 0;
if(!f&&V[i][t][f1][v])return d[i][t][f1][v];
int m=f?b[i]:9,ans=0;
inc(j,0,m)if((t==1&&j==3)||f1)ans+=dfs(i-1,f&&j==m,j,1,(v+c[i]*j)%13);
else ans+=dfs(i-1,f&&j==m,j,0,(v+c[i]*j)%13);
V[i][t][f1][v]=true;
return d[i][t][f1][v]=ans;
}
int solve(int x){
n=0;mem(b);mem(d);mem(V);
for(int t=x;t;t/=10)b[++n]=t%10;
return dfs(n,1,0,0,0);
}
int main(){
freopen("data.in","r",stdin);
c[1]=1;
inc(i,2,10)c[i]=c[i-1]*10%13;
while(~scanf("%d",&_x))printf("%d\n",solve(_x));
return 0;
}
B-number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7653 Accepted Submission(s): 4491
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
Statistic | Submit | Discuss | Note