boj435

来源:互联网 发布:游方道仙数据 编辑:程序博客网 时间:2024/05/08 03:48

给一个整数n,求0<=x<=n的范围内,有多少个数满足(x)^(2x)^(3x)==0 (^是异或符号)。答案对1000000009取模

x^(2*x)^(3*x)==0即x^(2*x)==2*x+x; 由二进制数的性质得,二进制的亦或相当于不进位的加法,故x与2*x相加时不能进位,因为2*x相当于x左移一位,所以x相邻位不能都为1。

预处理:dp[i]表示二进制位数为i的符合条件的数的个数,对于位数为i-1的数而言,如果其符合条件,则在其末尾添加一位数0,得到的长度为i的数也必定符合条件。,对于位数为i-2的数,可在其末尾添加01,得到长度为i的数,因为在之前的构造中得到数末尾为0,故不会重复。同时,对于位数为i-1且末尾为0的数而言,可以视作在i-2长度的数末尾添加两个数01。若在位数为i-2的数后面加入00,必定会被包含在第一种情况中。

故dp[i]=dp[i-1]+dp[i-2];

之后对每一位数枚举,若出现连续两位(i和i+1)为1,其必定包括之前的前缀加上长度为l-i+1的所有符合条件的数。

#include<iostream>#include<cstring>#include<string>#include<queue>#include<cstdio>#include<algorithm>#include<vector>#include<cmath>using namespace std;typedef long long LL;const LL INF=1000000009;const LL tw=2;LL dp[100],dp2[100];int A[100];int main(){    int i,j,k;    LL n,x,res;    memset(dp,0,sizeof(dp));    memset(dp2,0,sizeof(dp2));    dp[0]=0;    dp[1]=1;    dp[2]=1;    dp[3]=2;    dp2[0]=0;    for(i=4;i<=63;i++){        dp[i]=(dp[i-1]+dp[i-2])%INF;    }    for(i=1;i<=63;i++){         for(j=i;j>=1;j--){            dp2[i]=(dp2[i]+dp[j])%INF;        }      //  printf("dp[%d]=%lld\n",i,dp2[i]);    }      while(cin>>x){        memset(A,0,sizeof(A));        int t=0;        res=1;        n=x;       // printf("%lld ",x);        while(n>0){            A[++t]=n%2;       // printf("%d",A[t]);            n/=2;        }        if(t==0)res=0;        else if(t==1)res=1;        else if(A[t-1]==1)res=dp2[t];        else{            res=(dp2[t-1]+1)%INF;            for(i=t-1;i>=1;i--){                if(A[i]==1&&A[i-1]==1){                   res=(res+dp2[i])%INF;                   break;                }                else if(A[i]==1){                   res=(res+dp2[i-1]+1)%INF;                }             }        }        printf("%lld\n",(res+1)%INF);    }    return 0;}


0 0
原创粉丝点击