还是太菜,数位DP看了很久才看懂一部分,勉强能A入门题
模板是看了诸多dalao博客拿出来的,大家似乎都这么写(雾
比如杭电Bomb
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=22;
int dig[maxn];
LL f[maxn][10][2];
LL dfs(int pos,int pre,int istrue,int limit){
if (pos<0) return istrue;
if (!limit && f[pos][pre][istrue]!=-1) return f[pos][pre][istrue];
int last=limit?dig[pos]:9;
LL ret=0;
for (int i=0;i<=last;i++){
int ok=(pre==4)&&(i==9);
ret+=dfs(pos-1,i,istrue||ok,limit&&(i==last));
}
if (!limit) f[pos][pre][istrue]=ret;
return ret;
}
LL solve(LL n)
{
memset(f,-1,sizeof(f));
int len=0;
while(n)
{
dig[len++]=n%10;
n/=10;
}
return dfs(len-1,0,0,1);
}
int main()
{
LL n, T;
cin>>T;
while(T--)
{
cin>>n;
cout<<solve(n)<<endl;
}
return 0;
}
杭电不要62AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=22;
int dig[maxn];
LL f[maxn][10][2];
LL dfs(int pos,int pre,int fg,int limit){
if (pos<0) return fg==0;
if (!limit&&f[pos][pre][fg]!=-1) return f[pos][pre][fg];
LL res=0;
int last=limit?dig[pos]:9;
for (int i=0;i<=last;i++){
res+=dfs(pos-1,i,fg||((pre==6)&&(i==2))||(i==4),limit&&(i==last));
}
if (!limit) f[pos][pre][fg]=res;
return res;
}
LL solve(LL n)
{
memset(f,-1,sizeof(f));
int len=0;
while(n)
{
dig[len++]=n%10;
n/=10;
}
return dfs(len-1,0,0,1);
}
int main()
{
int n, m;
while(cin>>n>>m&&n&&m)
{
LL ans = solve(m)-solve(n);
if(n==1||solve(n)-solve(n-1)) ans++;
cout<<(ans>0?ans:0)<<endl;
}
return 0;
}