hdu5432Rikka with Array (數位dp+十進制轉化爲二進制)

Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has an array A of length n,and the ith element of A is equal to the sum of all digits of i in binary representation. For example,A[1]=1,A[3]=2,A[10]=2.

Now, Yuta wants to know the number of the pairs (i,j)(1i<jn) which satisfy A[i]>A[j].

It is too difficult for Rikka. Can you help her?
 

Input
The first line contains a number T(T10)——The number of the testcases.

For each testcase, the first line contains a number n(n10300).
 

Output
For each testcase, print a single number. The answer may be very large, so you only need to print the answer modulo 998244353.
 

Sample Input
1 10
 

Sample Output
7 When $n=10$, $A$ is equal to $1,1,2,1,2,2,3,1,2,2$.

So the answer is $7$.

題意:定義A[i]爲i化爲二進制後的1的個數,給你一個數n,你要在1~n之間找到兩個數a,b,使得a<b且A[a]>A[b].問總共有多少這樣的對數.

思路:這題和普通的數位dp不同,普通的數位dp是求單個數內的方案數,但是這道題是求對數,所以不能用之前的方法。這道題中我們要在記憶化搜索中同時枚舉兩個數,一個爲大數,一個爲小數,然後用dp[pos][cha][flag][same][lim]表示前pos位,大小兩數化爲二進制後1的個數差的絕對值爲cha,flag表示是否小數化爲二進制後的1的個數大於大數,same表示pos位前兩個數是不是完全相同,lim表示大數是不是還有限制。據說題解只開了三維,感覺三維的條件太少了,我只會寫五維的。。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 1005
#define MOD 998244353
char s[maxn];
int dp[maxn][maxn][2][2][2];   //dp[pos][cha][flag1][same][lim]表示前pos位,大小數1的個數差的絕對值是cha,小數中1的個數是不是大於大數中的個數
int wei[maxn],num[maxn];

int dfs(int pos,int cha,int flag,int same,int lim)   //same表示大數和小數是不是前幾位都一樣
{
    int i,j;
    if(pos==0){
        if(flag==1)return 1;
        return 0;
    }
    if(dp[pos][cha][flag][same][lim]!=-1 ){
        return dp[pos][cha][flag][same][lim];
    }
    int ans=0;
    if(same==1){
        if(lim==1){
            if(wei[pos]==0){
                 ans=ans+dfs(pos-1,0,0,1,1);  //都取0
                 if(ans>=MOD)ans-=MOD;
            }
            else if(wei[pos]==1){
                ans=ans+dfs(pos-1,0,0,1,1);if(ans>=MOD)ans-=MOD;  //都取1
                ans=ans+dfs(pos-1,0,0,1,0);if(ans>=MOD)ans-=MOD;  //都取0
                ans=ans+dfs(pos-1,1,0,0,1);if(ans>=MOD)ans-=MOD;   //大數取1,小數取0
            }
        }
        else if(lim==0){
            ans=ans+dfs(pos-1,0,0,1,0);if(ans>=MOD)ans-=MOD;
            ans=ans+dfs(pos-1,0,0,1,0);if(ans>=MOD)ans-=MOD;
            ans=ans+dfs(pos-1,1,0,0,0);if(ans>=MOD)ans-=MOD;
        }
    }
    else if(same==0){
        if(lim==1){
            if(wei[pos]==1){
                ans=ans+dfs(pos-1,cha,flag,same,0);if(ans>=MOD)ans-=MOD;  //都加0
                ans=ans+dfs(pos-1,cha,flag,same,1);if(ans>=MOD)ans-=MOD;  //都加1
                if(cha==0){
                    ans=ans+dfs(pos-1,1,0,same,1);if(ans>=MOD)ans-=MOD; //大數加1
                    ans=ans+dfs(pos-1,1,1,same,0);if(ans>=MOD)ans-=MOD;  //小數加1
                }
                else if(cha>0){
                    if(flag==1){
                        int flag1;
                        if(cha==1)flag1=0;
                        else flag1=1;
                        ans=ans+dfs(pos-1,cha-1,flag1,same,1);if(ans>=MOD)ans-=MOD; //大數加1
                        ans=ans+dfs(pos-1,cha+1,1,same,0);if(ans>=MOD)ans-=MOD;  //小數加1
                    }
                    else if(flag==0){
                        ans=ans+dfs(pos-1,cha+1,0,same,1 );if(ans>=MOD)ans-=MOD; //大數加1
                        ans=ans+dfs(pos-1,cha-1,0,same,0);if(ans>=MOD)ans-=MOD;  //小數加1
                    }
                }
            }
            else if(wei[pos]==0){
                ans=ans+dfs(pos-1,cha,flag,same,1);if(ans>=MOD)ans-=MOD;  //都加0
                if(cha==0){
                    ans=ans+dfs(pos-1,cha+1,1,same,1);if(ans>=MOD)ans-=MOD; //大數加0,小數加1
                }
                else if(cha>0){
                    if(flag==1){
                        ans=ans+dfs(pos-1,cha+1,1,same,1);if(ans>=MOD)ans-=MOD;
                    }
                    else if(flag==0){
                        ans=ans+dfs(pos-1,cha-1,0,same,1);if(ans>=MOD)ans-=MOD;
                    }
                }
            }
        }
        else if(lim==0){
            ans=ans+dfs(pos-1,cha,flag,same,0);if(ans>=MOD)ans-=MOD;  //都加0
            ans=ans+dfs(pos-1,cha,flag,same,0);if(ans>=MOD)ans-=MOD;  //都加1
            if(cha==0){
                ans=ans+dfs(pos-1,1,0,same,0);if(ans>=MOD)ans-=MOD;  //大數加1
                ans=ans+dfs(pos-1,1,1,same,0);if(ans>=MOD)ans-=MOD;  //小數加1
            }
            else if(cha>0){
                if(flag==1){
                    int flag1;
                    if(cha==1)flag1=0;
                    else flag1=1;
                    ans=ans+dfs(pos-1,cha-1,flag1,same,0 );if(ans>=MOD)ans-=MOD; //大數加1
                    ans=ans+dfs(pos-1,cha+1,1,same,0);if(ans>=MOD)ans-=MOD;  //小數加1
                }
                else if(flag==0){
                    ans=ans+dfs(pos-1,cha+1,0,same,0 );if(ans>=MOD)ans-=MOD; //大數加1
                    ans=ans+dfs(pos-1,cha-1,0,same,0);if(ans>=MOD)ans-=MOD;  //小數加1
                }
            }
        }
    }
    dp[pos][cha][flag][same][lim]=ans;
    return ans;
}
void solve()
{
    int i,j;
    int len=strlen(s+1);
    for(i=len;i>=1;i--){
        num[i]=s[len+1-i]-'0';
    }
    int tot=0;
    while(len){
        for(i=len-1;i>=1;i--){
            num[i]+=(num[i+1]&1)*10;
            num[i+1]>>=1;
        }
        if(num[1]&1)wei[++tot]=1;
        else wei[++tot]=0;
        num[1]>>=1;
        if(num[len]==0)len--;
    }

    memset(dp,-1,sizeof(dp));     //這裏要注意,必須每一個樣例都要初始化一遍,因爲不同的數,dp[pos][cha][flag1][same][lim]的意義不同
    printf("%d\n",dfs(tot,0,0,1,1)%MOD);
}

int main()
{
    int n,m,i,j,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s+1);
        solve();
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章