2020牛客暑期多校训练营(第六场) B、Binary Vector(数论)

本文探讨了n个n维随机0/1向量组成矩阵满秩的概率问题。通过分析每个向量的限制条件,得出满足线性独立的情况数,并给出递推公式。最终,文章提供了实现这一计算的代码片段。
摘要由CSDN通过智能技术生成

题目链接

题面:
在这里插入图片描述

题意:
给定 n 个 n 维随机 0/1 向量,求这 n 个 n 维随机 0/1 向量组成的矩阵满秩的概率。

题解:

对于每一个长度为 n 的向量,都有 2 n 2^n 2n 种情况,因为要求 n 个向量线性独立,所以我们依次讨论:
对于第一个向量来说,只需要至少有一个位置为 1 即可,即全部为 0 的情况不满足条件,此时满足条件的情况为 2 n − 1 2^n-1 2n1
对于第二个向量来说,需要满足的条件是,不能全部为 0 ,且不能被第一个向量所表示,此时满足条件的情况为 2 n − 2 2^n-2 2n2
对于第三个向量来说,同样不能为 0 ,不能被第一个向量所表示,不能被第二个向量所表示,不能被第一个向量和第二个向量所表示,此时满足条件的情况为 2 n − 4 2^n-4 2n4
对于第四个向量来说,不能被前面向量表示的集合为 { ∅ , { 1 } , { 2 } , { 3 } , { 1 , 2 } , { 1 , 3 } , { 2 , 3 } , { 1 , 2 , 3 } } ,共八种情况,所以此时满足条件的情况为 2 n − 8 2^n-8 2n8

对于第 n 个向量来说,不能被前面 n - 1 个向量单独或混合表示,此时满足条件的情况为 2 n − 2 n − 1 2^n-2^{n-1} 2n2n1

f [ n ] = ∏ i = 0 n − 1 2 n − 2 i 2 n f[n]=\prod_{i=0}^{n-1}\frac{2^n-2^i}{2^n} f[n]=i=0n12n2n2i

可以得到公式 f [ n ] = f [ n − 1 ] ∗ 2 n − 1 2 n f[n]=f[n-1]*\frac{2^n-1}{2^n} f[n]=f[n1]2n2n1

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<queue>
#include<bitset>
#include<map>
#include<unordered_map>
#include<set>
#include<list>
#include<ctime>
#define ui unsigned int
#define ll long long
#define llu unsigned ll
#define ld long double
#define pr make_pair
#define pb push_back
#define lc (cnt<<1)
#define rc (cnt<<1|1)
#define len(x)  (t[(x)].r-t[(x)].l+1)
#define tmid ((l+r)>>1)
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;

const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const double dnf=1e18;
const int mod=1e9+7;
const double eps=1e-1;
const double pi=acos(-1.0);
const int hp=13331;
const int maxn=20000100;
const int maxp=400100;
const int maxm=600100;
const int up=200000;

int f[maxn];

ll mypow(ll a,ll b)
{
    a%=mod,b%=(mod-1);
    ll ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main(void)
{
    ll pow2=2,inv2=mypow(2,mod-2);
    ll inv=inv2;
    f[1]=inv2;
    for(int i=2;i<maxn;i++)
    {
        pow2=pow2*2%mod;
        inv2=inv2*inv%mod;
        f[i]=f[i-1]*(pow2-1+mod)%mod*inv2%mod;
        f[i-1]^=f[i-2];
    }
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        int n;
        scanf("%d",&n);
        printf("%d\n",f[n]);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值