SPOJ 10606 Balanced Numbers - 數位dp

題目大意:

一個數被稱爲是平衡的數當且僅當對於所有出現過的數位, 偶數出現奇數次,奇數出現偶數次。 給定 A,B,請統計出 [A,B] 內所有平衡的數的個數。
1 ≤A≤B≤ 1018

分析:

  • 是個數位dp。
  • (n)爲[1,n]中所有平衡的數的個數,則原問題<=>求p(B)-p(A-1)。
  • 注意到要填的數字只有0~9(注意不能有前導0,程序中要特判),
    用0表示這個數字沒有出現過,1表示這個數字出現奇數次,2表示這個數字出現偶數次,用一個10位的3進制數就可以表示出狀態,記爲S。由於個人覺得3進制不好直接取位,用的4進製表示,但是4進制的狀態數410 太大了,就只好使用了hash,建立hash表來做。
  • 設dp[i][S][0/1]:
    S是當前的狀態,
    0表示所有狀態代表的數的前i位嚴格小於n的前i位的方案數;1表示所有狀態代表的數的前i位等於n的前i位的方案數
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXBIT 19
#define MAXST 59049 //3^10
typedef long long LL;

struct HashMap{
    int cnth[2],st[MAXST+10][2],id[MAXST+10][2];
    LL dp[MAXST+10][2];
    void Clear(){
        memset(id,-1,sizeof id);
        memset(st,0,sizeof st);
        memset(dp,0,sizeof dp);
        cnth[0]=cnth[1]=0;
    }
    void Insert(LL S,LL dpval,int d){
        if(id[S][d]==-1){
            id[S][d]=++cnth[d];
            st[cnth[d]][d]=S;
        }
        dp[id[S][d]][d]+=dpval;
    }
}var[2],*last=&var[0],*cur=&var[1];

LL a,b,pw[MAXBIT+10];
int col[MAXBIT+10],lmt[MAXBIT+10];

int Getorg(LL x){
    int ret=0,k=0;
    LL y=x;
    memset(lmt,0,sizeof lmt);
    while(x){
        k++;
        x/=10;
    }
    while(y){
        lmt[k-ret]=y%10;
        y/=10;
        ret++;
    }
    return ret;
}
inline void Getbit(LL s){
    memset(col,0,sizeof col);
    for(int i=0;i<=9;i++){
        col[i]=s%3;
        s/=3;
    }
}
LL DP(LL R)
{
    int n=Getorg(R);
    if(!n) return 1;
    last->Clear();
    for(int k=1;k<lmt[1];k++)
        last->Insert(pw[k],1,0);
    last->Insert(pw[lmt[1]],1,1);
    last->Insert(0,1,0);

    for(int i=2;i<=n;i++){
        cur->Clear();
        for(int j=1;j<=last->cnth[0];j++){
            Getbit(last->st[j][0]);
            for(int k=0;k<=9;k++){
                if(last->st[j][0]==0&&k==0)
                    continue;
                if(col[k]!=2)
                    cur->Insert(last->st[j][0]+pw[k],last->dp[j][0],0);
                else
                    cur->Insert(last->st[j][0]-pw[k],last->dp[j][0],0);
            }
        }
        for(int j=1;j<=last->cnth[1];j++){
            Getbit(last->st[j][1]);
            for(int k=0;k<lmt[i];k++){
                if(last->st[j][1]==0&&k==0)
                    continue;
                if(col[k]!=2)
                    cur->Insert(last->st[j][1]+pw[k],last->dp[j][1],0);
                else
                    cur->Insert(last->st[j][1]-pw[k],last->dp[j][1],0);
            }
            if(lmt[i]==0&&last->st[j][1]==0)
                continue;
            if(col[lmt[i]]!=2)
                cur->Insert(last->st[j][1]+pw[lmt[i]],last->dp[j][1],1);
            else
                cur->Insert(last->st[j][1]-pw[lmt[i]],last->dp[j][1],1);
        }
        cur->Insert(0,1,0);
        swap(cur,last);
    }
    LL ret=0;
    for(int d=0;d<2;d++){
        for(int i=1;i<=last->cnth[d];i++){
            Getbit(last->st[i][d]);
            bool flag=true;
            for(int j=0;j<=9;j++){
                if(j&1){
                    if(col[j]&&col[j]&1){
                        flag=false;
                        break;
                    }
                }
                else{
                    if(col[j]&&(!(col[j]&1))){
                        flag=false;
                        break;
                    }
                }
            }
            if(flag)
                ret+=last->dp[i][d];
        }
    }
    return ret;
}
void prepare()
{
    pw[0]=1;
    for(int i=1;i<=9;i++)
        pw[i]=pw[i-1]*3;
}
int main()
{
    int T;
    scanf("%d",&T);
    prepare();
    while(T--){
        scanf("%lld%lld",&a,&b);
        printf("%lld\n",DP(b)-DP(a-1));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章