统计区间 [1,n] 中含有 ‘13’ 且模 13 为 0 的数字有多少个。
输入格式
多组数据
每行一个正整数n(1 ≤ n ≤ 1e9)
一直输入到文件结束
输出格式
输出一行表示满足条件的答案
样例输入
13
100
200
1000
样例输出
1
1
2
2
多了一维来记录这个当前位置上的数字mod的余数,当板子写把。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define ls (p<<1)
#define rs (p<<1|1)
#define ll long long
using namespace std;
const int maxn = 2e6+5;
const int INF = 0x3f3f3f3f;
ll dp[50][15][50];//dp[pos][pre]记录了遍历第pos为位时,前一位为pre时的状态数
ll a[30],b[30];
// void init(){
// dp[0][0]=1;
// dp[0][1]=dp[0][2]=0;
// for(int i=1;i<25;i++){
// dp[i][0]=10*dp[i-1][0]-dp[i-1][1];//长度为i不含有49的个数
// dp[i][1]=dp[i-1][0];//最高位为9但不含49的个数
// dp[i][2]=10*dp[i-1][2]+dp[i-1][1];//含有49的个数
// }
// }
int dfs(int lf,int pos,int mod,int pre,bool lim)//lim记录上限
{
int num,i,ans,modx,prex;
if(pos<=0)
return mod == 0 && pre == 2;
if(!lim && dp[pos][mod][pre] != -1)//没有上限并且已被访问过
return dp[pos][mod][pre];
if(lim)
num=a[pos];
else num=9;//假设该位是2,下一位是3,如果现在算到该位为1,那么下一位是能取到9的,如果该位为2,下一位只能取到3
ans = 0;
for(int i=0;i<=num;i++){
int tmp=(mod*10+i)%13,n1;
if(pre==2||pre==1&&i==3)
n1=2;
else if(i==1) n1=1;
else n1=0;
ans+=dfs(i,pos-1,tmp,n1,lim&&i==num);
}
if(!lim)
dp[pos][mod][pre] = ans;
return ans;
}
void solve(){
ll x,y;
while(scanf("%lld",&x)!=EOF){
memset(dp,-1,sizeof(dp));
memset(a,0,sizeof(a));
ll len = 0;
while(x) {
a[++len] = x % 10;
x /= 10;
}
cout<<dfs(-1,len,0,0,1)<<endl;
}
}
int main()
{
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
solve();
return 0;
}