链接:CF388D

题目大意

给定一个数\(n\),求选择\(0 \sim n\)中任意个数的数字组成的集合\(S\)中,有多少满足若\(a\in S,b\in S\),则\(a \bigoplus b \in S\),输出方案数对\(1e9+7\)取模。

题目分析

设\(f[i][j][k]\)表示从第\(i\)位到最高位,已经选了\(j\)个基,且由基\(\bigoplus\)得到的最大值与\(n\)的差值是否在\(2^i\)以内的方案数。


况一:当\(k=0\)(异或最大值\(+2^i<n\))时,考虑第\(i-1\)位。

如果该位要作为单独一个基,那么有\(f[i-1][j+1][0]+=f[i][j][0]​\),

该位单独作基,则新增情况数为之前有的情况,视为在之前每种情况上\(x​\)新增一个\(2^{i-1} ​\)的基,

由设可保证新构成的集合异或最大值与\(n​\)的差值在\(2^{i-1}​\)之外,所以算在\(f[i-1][j+1][0]​\)中。

如果该位不单独作基,而是放入已经拥有的j个基中,

那么对于每个基,都有放与不放两种选择,共\(2^j​\)种,\(f[i-1][j][0]+=2^j*f[i][j][0]​\)。


况二:当\(k=1\)(异或最大值\(+2^i>=n\))时,考虑第\(i-1\)位。

讨论\(n\)在第\(i-1\)位是否为\(1\):

1、\(n\)在第\(i-1\)位不为\(1\),异或最大值\(+2^{i-1}>n\):

此时最大值无法新增一个\(2^{i-1}\)。

那么,我们只能继承令第\(i-1\)位为偶数个\(1\)的情况,因为只有这样,最大值才不会改变,共\(2^{j-1}\)种。

2、\(n\)在第\(i-1\)位为\(1\),异或最大值\(+2^{i-1}≤n\):

如果在第\(i-1\)位取\(0\),那么新得到的集合异或最大值\(+2^{i-1}≤n\),因此应存入\(f[i-1][j][0]\)中,共\(2^{j-1}\)种。

如果在第\(i-1\)位取\(1\),那么新得到的集合异或最大值\(+2^{i-1}≥n\),因此应存入\(f[i-1][?][1]\)中。

  • 对于第\(i-1\)位单独作基的情况,可以有\(f[i][j][1]\)种,存入\(f[i-1][j+1][1]\)中,\(f[i-1][j+1][1]+=f[i][j][1]\)。

  • 对于第\(i-1\)位不单独作基的情况,可以对每个基选择放与不放,且必须放奇数个,共\(2^{j-1}\)种选择,

因此\(f[i-1][j][1]+=2^{j-1}*f[i][j][1]\)。


注意:

对于所有情况,当\(j=0\),对于选择第\(i-1\)位为\(0\)的情况,\(2^{j-1}\)算作\(1\);

对于选择第\(i-1\)位为\(1\)的情况,\(2^{j-1}\)算作\(0\),因为就算你没有选择一个基,你的异或和仍可以视作\(0\)。

代码实现

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#define MAXN 0x7fffffff
typedef long long LL;
const int mod=1e9+7;
using namespace std;
inline int Getint(){
register int x=0,f=1;
register char ch=getchar();
while(!isdigit(ch)){
if(ch=='-')f=-1;
ch=getchar();
}
while(isdigit(ch)){
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int ans,f[35][35][2];
void Add(int &x,int y){x=((x+y>=mod)?(x+y-mod):(x+y));}
int main(){
int n=Getint();
f[30][0][1]=1;
for(int i=30;i>0;i--)
for(int j=0;j<=30;j++){
Add(f[i-1][j][0],(1LL<<j)*f[i][j][0]%mod);
Add(f[i-1][j+1][0],f[i][j][0]);
int x=j?(1<<(j-1)):1,y=j?(1<<(j-1)):0;
if(n>>(i-1)&1){
Add(f[i-1][j][0],(LL)x*f[i][j][1]%mod);
Add(f[i-1][j][1],(LL)y*f[i][j][1]%mod);
Add(f[i-1][j+1][1],f[i][j][1]);
}else Add(f[i-1][j][1],(LL)x*f[i][j][1]%mod);
}
for(int i=0;i<=30;i++)
Add(ans,f[0][i][0]),Add(ans,f[0][i][1]);
cout<<ans;
return 0;
}

Codeforces 388D Fox and Perfect Sets的更多相关文章

  1. codeforces 388D Fox and Perfect Sets(线性基+数位dp)

    #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define mp mak ...

  2. Codeforces 388 D. Fox and Perfect Sets

    $ >Codeforces \space 388 D.  Fox and Perfect Sets<$ 题目大意 : 定义一个完美的集合 \(S\) ,当且仅当 \(S\) 非负非空,且 ...

  3. BZOJ CF388D. Fox and Perfect Sets [线性基 数位DP]

    CF388D. Fox and Perfect Sets 题意:求最大元素\(le n\)的线性空间的个数 给神题跪了 orz 容易想到 每个线性基对应唯一的线性空间,我们可以统计满足条件的对应空间不 ...

  4. 数位DP CF388D - Fox and Perfect Sets

    题目地址 一个整数perfect集合满足性质:集合中随意两个整数的异或和仍在这个集合中. 求最大数不超过K的perfect集合的个数. 每一个集合都是一个线性的向量空间. .能够通过全然的高斯消元得出 ...

  5. 【做题】CF388D. Fox and Perfect Sets——线性基&数位dp

    原文链接https://www.cnblogs.com/cly-none/p/9711279.html 题意:求有多少个非空集合\(S \subset N\)满足,\(\forall a,b \in ...

  6. CodeForces 388A Fox and Box Accumulation (模拟)

    A. Fox and Box Accumulation time limit per test:1 second memory limit per test:256 megabytes Fox Cie ...

  7. Codeforces 388C Fox and Card Game (贪心博弈)

    Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...

  8. codeforces 510B. Fox And Two Dots 解题报告

    题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...

  9. codeforces 477B B. Dreamoon and Sets(构造)

    题目链接: B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input st ...

随机推荐

  1. px2rem-loader(Vue:将px转化为rem,适配移动端)

    转载:https://www.cnblogs.com/WQLong/p/7798822.html 1.下载lib-flexible 使用的是vue-cli+webpack,通过npm来安装的 npm ...

  2. BZOJ随即跳题-随即到什么题你写什么题

    来挑战一下吧~ 请事先登录你BZOJ的账号!

  3. python 使用abc实现接口类/虚类(2.2)

    python 使用abc实现接口类/虚类 具体类 class BaseA: def run(self): print('base A running') class ChildA(BaseA): de ...

  4. Maven详解()-- 常用命令

    Maven常用命令: Maven库: http://repo2.maven.org/maven2/ Maven依赖查询: http://mvnrepository.com/ 一,Maven常用命令: ...

  5. c++实现写一个函数,求2个整数的和,要求在函数体内不得使用+,-* /

    #include <iostream> using namespace std; int add(int x, int y) { return x+y; } int addmove(int ...

  6. mssql查询表在哪个数据库中

    mssql查询表在哪个数据库中 EXEC sp_MSforeachdb @command1='IF object_id(''?'' + ''..表名'') IS NOT NULL PRINT ''?' ...

  7. 逻辑回归,多分类推广算法softmax回归中

    转自http://ufldl.stanford.edu/wiki/index.php/Softmax%E5%9B%9E%E5%BD%92 简介 在本节中,我们介绍Softmax回归模型,该模型是log ...

  8. 【转载】C# 开源库大全非常好

    原文地址:http://m.blog.csdn.net/woddle/article/details/37311877 C#开源大全 商业协作和项目管理平台-TeamLab 网络视频会议软件-VMuk ...

  9. .net 接受请求过来的流

    //接收POST过来的数据 System.IO.Stream s = Request.InputStream; int count = 0; byte[] buffer = new byte[1024 ...

  10. Android开发 navigation的跳转动画实现

    前言 此篇博客只简短的介绍navigation如何添加跳转页面的动画属性,如果你还为接触了解过navigation.建议你看我另一篇博客Android开发 navigation入门详解 创建动画xml ...