hdu2089

#include <iostream>
#include <string>
using namespace std;

typedef long long LL;
const int MaxBit=22;
LL dp[MaxBit][2][2];
//LL dp[MaxBit][3][2];
/*
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
 */

/*
* 0 不含4,末尾不为6
* 1 不含4,末尾为6

* 0 没到上界,1上界
dp[i][0][0]=dp[i-1][0][0]*8 + dp[i-1][1][0]*7;
dp[i][1][0]=dp[i-1][0][0]   + dp[i-1][1][0];
*/
//int nextState(int now,int j)
//{
// if ( now==2 || (now==0 && j==2)  ){
//  return 2;
// }
// return j==6;
//}

int isTrue(const string& str)
{
 int strLen=str.length();
 if (str[0]== '4'  ){
  return 0;
 }
 for ( int i=1; i<strLen;++i ){
  if (str[i]== '4'  ){
   return 0;
  }
  if ( str[i-1]=='6' && str[i]=='2' ){
   return 0;
  }
 }
 return 1;
}
LL DP_Bit(const string & str)
{
 memset(dp,0,sizeof(dp));
 dp[0][0][1]=1;
 const int strLen=str.length();
 for ( int i=1 ; i<=strLen ; ++i ){
  //没达到上界
  dp[i][0][0]=dp[i-1][0][0]*8 + dp[i-1][1][0]*7;
  dp[i][1][0]=dp[i-1][0][0]   + dp[i-1][1][0];

  //前一位达到上界
  for ( int j=0 ; j<str[i-1]-'0' ; ++j ){
   if ( j!=4 ){
    if ( j==6 ){
     dp[i][1][0]+=dp[i-1][0][1];
     dp[i][1][0]+=dp[i-1][1][1];
    } else {
     dp[i][0][0]+=dp[i-1][0][1];
     if ( j!=2 ){
      dp[i][0][0]+=dp[i-1][1][1];
     }
    }
   }
   
  }
  if ( str[i-1]!='4' ){
   if ( str[i-1]=='6' ){
    dp[i][1][1]+=dp[i-1][0][1];
    dp[i][1][1]+=dp[i-1][1][1];
   } else  {
    dp[i][0][1]+=dp[i-1][0][1];
    if ( str[i-1]!='2' ){
     dp[i][0][1]+=dp[i-1][1][1];
    }
   } 
  }
  
 }
 //cout<<dp[1][0][1]<<" "<<dp[1][1][1]<<endl;
 //cout<<dp[strLen][0][1]<<" "<<dp[strLen][1][1]<<endl;
 
 /*for ( int i=0;i<=strLen ;++i ){
 cout<<" "<<i<<endl;
 cout<<dp[i][0][1]<<" "<<dp[i][1][1]<<endl;
 }*/
 return dp[strLen][0][0]+dp[strLen][1][0]+dp[strLen][0][1]+dp[strLen][1][1];

}


int main()
{
 string strLow,strHigh;
 while (true){
  cin>>strLow;
  cin>>strHigh;
  if ( strLow== "0" && strHigh=="0"){
   break;
  }

  cout<<DP_Bit(strHigh)-DP_Bit(strLow)+isTrue(strLow)<<endl;
  /*cout<<isTrue(strLow)<<endl;
  cout<<DP_Bit(strHigh)<<endl;
  cout<<DP_Bit(strLow)<<endl;*/

  //if ( strLow=="0" ){
  // cout<<DP_Bit(strHigh)<<endl;
  //} else {
  // //strLow[strLow.length()-1]-=1;
  // 
  // cout<<DP_Bit(strHigh)-DP_Bit(strLow)+isTrue(strLow)<<endl;
  //}
  
 }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值