LightOJ 1140 How Many Zeroes

时间:2023-03-08 22:16:04

题意:写出一个给定区间的每个数,求出一共写了多少个零。

解法:数位DP,定义dp[len][flag][num]:len的定义为数位的长度,flag定义为前导0和没有前导0的两种状态,num定义为写的满足条件的0的个数。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
typedef long long LL;
LL digit[];
LL dp[][][];
LL dfs(LL len,bool flag,LL p,LL p2,bool fp)
{
if (!len)
{
if (!flag)
return p2-p;
else return ;
}
if (!fp && dp[len][flag][p2-p]!=-) return dp[len][flag][p2-p];
LL ret=;
LL fpmax= fp ? digit[len] : ;
for (LL i= ;i<=fpmax ;i++)
{
bool ok= i== ? true : false;
LL temp;
if (i==) temp= ; else temp= ;
ret += dfs(len-,flag&&ok,flag?p+temp:p ,p2+temp,fp&&i==fpmax);
}
if (!fp) return dp[len][flag][p2-p]=ret;
return ret;
}
LL f(LL n)
{
if (n==-) return -;
if (n==) return ;
LL len=;
while (n)
{
digit[++len]=n%;
n /= ;
}
return dfs(len,true,,,true);
}
int main()
{
int t;
int ncase=;
cin>>t;
while (t--)
{
LL a,b;
scanf("%lld%lld",&a,&b);
memset(dp,-,sizeof(dp));
printf("Case %d: %lld\n",ncase++,f(b)-f(a-));
}
return ;
}