odd-even number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 220 Accepted Submission(s): 115
Problem Description
For a number,if the length of continuous odd digits is even and the length of continuous even digits is odd,we call it odd-even number.Now we want to know the amount of odd-even number between L,R(1<=L<=R<= 9*10^18).
Input
First line a t,then t cases.every line contains two integers L and R.
Output
Print the output for each case on one line in the format as shown below.
Sample Input
2
1 100
110 220
Sample Output
Case #1: 29
Case #2: 36
Source
2016 ACM/ICPC Asia Regional Shenyang Online
Recommend
wange2014 | We have carefully selected several similar problems for you: 5901 5900 5899 5897 5896
啊数位DP,状态看注释。。
#include "cstring"
#include "cstdio"
#include "string.h"
#include "cmath"
using namespace std;
long long dp[25][5];
int num[25];
/*status:
0 有前导0
1 连续奇数个奇数
2 连续偶数个奇数
3 连续奇数个偶数
4 连续偶数个奇数
*/
long long dfs(int pos,int limit,int status)
{
if(pos<0)
{
if(status==2||status==3)
return 1;
else
return 0;
}
if(!limit&&dp[pos][status]!=-1)
//if(!limit&&~dp[pos][status])
return dp[pos][status];
int end= limit? num[pos]:9;
long long ans=0;
for(int i=0;i<=end;i++)
{
if(!status)
{
if(i==0)
ans+=dfs(pos-1,0,0);
else
{
if(i&1)
ans+=dfs(pos-1,limit&&i==end,1);
else
ans+=dfs(pos-1,limit&&i==end,3);
}
}
else if(status==1)
{
if(i&1)
ans+=dfs(pos-1,limit&&i==end,2);
}
else if(status==2)
{
if(i&1)
ans+=dfs(pos-1,limit&&i==end,1);
else
ans+=dfs(pos-1,limit&&i==end,3);
}
else if(status==3)
{
if(i&1)
ans+=dfs(pos-1,limit&&i==end,1);
else
ans+=dfs(pos-1,limit&&i==end,4);
}
else
{
if(i&1)
{}
else
ans+=dfs(pos-1,limit&&i==end,3);
}
}
if(!limit)
dp[pos][status]=ans;
return ans;
}
long long solve(long long n)
{
memset(dp,-1,sizeof(dp));
int len=0;
while(n)
{
num[len++]=n%10;
n/=10;
}
return dfs(len-1,1,0);
}
int main()
{
int cas;
scanf("%d",&cas);
for(int i=1;i<=cas;i++)
{
long long l,r;
scanf("%lld%lld",&l,&r);
printf("Case #%d: ",i);
printf("%lld\n",solve(r)-solve(l-1));
}
}