题目链接:https://ac.nowcoder.com/acm/contest/329/G
数位dp的模板题,注意需要开ll,先求出来区间内不包含6的个数,然后再用区间长度减一下就是包含6的个数了。
AC代码:
代码语言:javascript复制#include <bits/stdc .h>
#define ll long long
using namespace std;
int a[20];
ll dp[20][20];
ll dfs(int pos, int pre, bool limit){
if(pos == -1) return 1;
if(!limit && dp[pos][pre] != -1) return dp[pos][pre];
int up = limit ? a[pos] : 9;
ll ans = 0;
for(int i=0;i<=up;i ){
if(i == 6)continue;
ans = dfs(pos - 1, i, limit && i == a[pos]);
}
return limit ? ans : dp[pos][pre] = ans;
}
ll solve(ll x){
int pos = 0;
while(x){
a[pos ] = x % 10;
x /= 10;
}
return dfs(pos - 1, 0, true);
}
int main()
{
ll l, r;
scanf("%lld%lld",&l, &r);
memset(dp,-1,sizeof(dp));
ll xx = r - l 1;
ll yy = solve(r) - solve(l - 1);
printf("%lldn",xx - yy);
return 0;
}