odd-even number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 999 Accepted Submission(s): 540
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
Recommend
wange2014
要求是数位的连续奇数的长度要求是偶数,数位上连续偶数的长度要求是奇数
很明显的数位dp,
flog记录前一位是奇数还是偶数,len记录已经有多长了。
注意要记录前导零,因为前导零会影响连续偶数的长度
#include<bits/stdc++.h>
using namespace std;
long long dp[2][100];
int a[100];
long long dfs(int pos,int flog,int len,int state,int limit,int zero)
{
if(pos==-1)
{
return state;
}
if(dp[flog][pos]!=-1&&state&&!limit)
return dp[flog][pos];
int up=limit?a[pos]:9;
long long ans=0;
for(int i=0;i<=up;i++)
{
if(zero&&i==0)
{
ans+=dfs(pos-1,1,0,0,limit&&a[pos]==0,zero);
}
else if(flog&&(len&1))
{
if(i&1)
{
ans+=dfs(pos-1,flog,(len+1)%2,1,limit&&i==a[pos],0);
}
else
continue;
}
else if(!flog&&!(len&1))
{
if(!(i&1))
{
ans+=dfs(pos-1,flog,(len+1)%2,1,limit&&i==a[pos],0);
}
else
continue;
}
else
{
if(flog)
{
if(i&1)
{
ans+=dfs(pos-1,flog,(len+1)%2,0,limit&&i==a[pos],0);
}
else
{
ans+=dfs(pos-1,0,1,1,limit&&i==a[pos],0);
}
}
else
{
if(i&1)
{
ans+=dfs(pos-1,1,1,0,limit&&i==a[pos],0);
}
else
{
ans+=dfs(pos-1,0,(len+1)%2,0,limit&&i==a[pos],0);
}
}
}
}
if(!limit&&state&&dp[flog][pos]==-1)
dp[flog][pos]=ans;
return ans;
}
long long solve(long long x)
{
int pos=0;
while(x)
{
a[pos++]=x%10;
x/=10;
}
return dfs(pos-1,1,0,0,1,1);
}
int main()
{
memset(dp,-1,sizeof(dp));
int t;
long long x,y;
cin>>t;
for(int j=1;j<=t;j++)
{
scanf("%lld %lld",&x,&y);
printf("Case #%d: %lld\n",j,solve(y)-solve(x-1));
}
return 0;
}