2016沈阳网络赛 odd-even number

时间:2024-01-11 20:16:38

odd-even number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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
分析:数位dp;
   dp[i][j][k]三维分别代表位置,当前数奇偶性,个数奇偶性;
   注意前导0的特判;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
const int N=5e4+;
const int M=N**;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,num[],pos,cas;
ll dp[][][],p,q;
ll dfs(int pos,int x,int y,int z,int k)
{
if(pos<)return k&&(x^y)==;
if(z&&k&&dp[pos][x][y]!=-)return dp[pos][x][y];
int now=z?:num[pos],i;
ll ret=;
rep(i,,now)
{
if(k&&(x^(i&))==&&(x^y)==)continue;
if(!i&&!k)ret+=dfs(pos-,x,y,z||i<num[pos],k||i);
else if((i&)==x)ret+=dfs(pos-,x,y^,z||i<num[pos],k||i);
else ret+=dfs(pos-,x^,,z||i<num[pos],k||i);
}
return z&&k?dp[pos][x][y]=ret:ret;
}
ll gao(ll x)
{
pos=;
while(x)num[pos++]=x%,x/=;
return dfs(pos-,,,,);
}
int main()
{
int i,j;
memset(dp,-,sizeof(dp));
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&p,&q);
printf("Case #%d: %lld\n",++cas,gao(q)-gao(p-));
}
return ;
}