题意:题意有点绕嘴,不说了
思路:dfs时我们只走能过成功的状态,即连续的奇数是偶数个并且下一个是偶数,还有就是连续的偶数是奇数个并且下一个是奇数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=5010;
int dig[30];
ll dp[30][10][20];
ll dfs(int pos,int lim,int pre,int num,int flag){
if(pos<0){
if(flag) return 0;
else if(pre%2==0&&num%2==1) return 1;
else if(pre%2==1&&num%2==0) return 1;
else return 0;
}
if(!lim&&dp[pos][pre][num]!=-1) return dp[pos][pre][num];
int las=lim?dig[pos]:9;
ll ret=0;
for(int i=0;i<=las;i++){
if(flag==1){
if(i==0) ret+=dfs(pos-1,lim&&(i==las),0,0,1);
else ret+=dfs(pos-1,lim&&(i==las),i,1,0);
}else{
if(pre%2==0){
if(i%2==0) ret+=dfs(pos-1,lim&&(i==las),i,num+1,0);
else if(num%2!=0) ret+=dfs(pos-1,lim&&(i==las),i,1,0);
}else{
if(i%2==1) ret+=dfs(pos-1,lim&&(i==las),i,num+1,0);
else if(num%2==0) ret+=dfs(pos-1,lim&&(i==las),i,1,0);
}
}
}
if(!lim) dp[pos][pre][num]=ret;
return ret;
}
ll slove(ll n){
if(n==0) return 0;
int len=0;
while(n){
dig[len++]=n%10;
n/=10;
}
return dfs(len-1,1,0,0,1);
}
int main(){
memset(dp,-1,sizeof(dp));
int T,cas=1;
ll n,m;
scanf("%d",&T);
while(T--){
scanf("%I64d%I64d",&n,&m);
printf("Case #%d: %I64d\n",cas++,slove(m)-slove(n-1));
}
return 0;
}