B-number
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
对于一个数来说最后取模等同于对每一位进行取模,举个栗子!
21554%13 == (((2*10+1)%13)*10+5)%13…f [ pos ][ mul ][ pre ][ st ] ;pos 当前的位数,mul 当前数字取模后的余数,pre 上一位的数字,st 是否存在连续的 13
AC代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int N=30;
int n;
ll p[N];
ll f[N][20][15][2];
ll dp(int pos,int mul,int pre,int st,int flag)
{
if(pos==-1) return mul==0&&st;
if(flag&&f[pos][mul][pre][st]!=-1) return f[pos][mul][pre][st];
ll ans=0;
int up=flag?9:p[pos];
for(int i=0;i<=up;i++)
{
ans+=dp(pos-1,(mul*10+i)%13,i,(pre==1&&i==3)||st,flag||i<up);
}
if(flag) f[pos][mul][pre][st]=ans;
return ans;
}
ll slove(ll x)
{
int pos=0;
while(x)
{
p[pos++]=x%10;
x/=10;
}
return dp(pos-1,0,0,0,0);
}
int main()
{
while(cin>>n)
{
memset(f,-1,sizeof f);
cout<<slove(n)<<endl;
}
return 0;
}