求範圍內連續奇數個數是偶數,連續偶數個數是奇數的數的個數
阿新 • • 發佈:2018-12-10
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
程式碼:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long using namespace std; ll dp[20][2][2];//dp[len][pre][e] 代表第幾位,之前一位是奇數還是偶數,e表示是否還要新增一個與pre同為奇數或者偶數的數。 int a[20]; ll dfs(int len,int pre,int e,bool flag,bool s)//pre 表示上一個數,0表示偶數,1表示奇數。e=0不需要新增。e=1需要新增。s記錄字首和,s=false前面全是0, { if(len<0) { return e==1?0:1; } if(flag==false&&dp[len][pre][e]!=-1&&s) { return dp[len][pre][e]; } ll ans=0; int cnt=flag?a[len]:9; for(int i=0;i<=cnt;i++) { if(s==false) { if(i==0) { ans+=dfs(len-1,0,0,flag&&i==cnt,false); } else { ans+=dfs(len-1,i%2,i%2==1?1:0,flag&&i==cnt,true); } } else if(i%2==1) { if(pre%2==1) { ans+=dfs(len-1,1,e^1,flag&&i==cnt,true); } else { if(e==0) { ans+= dfs(len-1,1,1,flag&&i==cnt,true); } } } else { if(pre%2==0) ans+=dfs(len-1,0,e^1,flag&&i==cnt,true); else { if(e==0) { ans+=dfs(len-1,0,0,flag&&i==cnt,true); } } } } if(!flag&&s) dp[len][pre][e]=ans; return ans; } ll solve(ll x) { int len=0; memset(a,0,sizeof(0)); while(x>0) { a[len++]=x%10; x/=10; } return dfs(len-1,0,0,true,false); } int main() { int t,ca=1; scanf("%d",&t); memset(dp,-1,sizeof(dp)); while(t--) { long long int l,r; scanf("%I64d%I64d",&l,&r); printf("Case #%d: %I64d\n",ca++,solve(r)-solve(l-1)); } return 0; }