CF - 1330D

给定一个数d,求有多少数列,满足:1<=a1<a2…an<=n,且a1⊕a2>a2, a1⊕a2⊕a3>a3…

首先,设a1的二进制最高位为第i位,那么a2最高位>i。
像是分组背包的思想,最高位为i的元素为一组,因为最高位为i,那么该组只能选最多一个。
dp[i]为以最高位元素为i结尾的数量,那么dp[i] = (1 + dp[ 0 ~ i-1])*(2^i)。

那么,设d的最高位元素为t,ans += dp[i] (0<=i<t),然后特殊处理最高位元素为t的情况 (1 + dp[ 0 ~ i-1] ) * ( d-(1<<t)+1)

  • 代码
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<vector>
#define cl (k<<1)
#define cr (k<<1|1)
#define Mid ((a[k].l+a[k].r)>>1)
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
#define bug printf("???\n")

typedef long long LL;

const int Inf=0x3f3f3f3f;
const double eps=1e-8;
const int maxn=50;

LL dp[maxn];
LL num[maxn];

int main()
{

    int T;
    cin>>T;
    while(T--){
        LL d,m;
        cin>>d>>m;

        dp[0] = 1%m;
        num[0] = 1;
        for(LL i=1; i<=32; i++){
            dp[i] = 1;
            num[i] = ((LL)1<<i);
            for(int k=0; k<i; k++){
                dp[i] = (dp[i] + dp[k])%m;
            }
            dp[i] = dp[i]*num[i] %m;
        }
        // for(int i=0; i<3; i++) cout<<"("<<dp[i]<<")"<<endl;

        LL id=upper_bound(num, num+33, d)-num - 1;
        LL ans=0, tmp=1;
        for(int i=0; i<id; i++){
            ans = (ans+dp[i])%m;
            tmp = (tmp+dp[i])%m;
        }
        ans = (ans + tmp*(d-num[id]+1) )%m;
        cout<<ans<<endl;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值