牛客多校6 - Binary Vector(组合数学+推公式)

题目链接:点击查看

题目大意:给出一个 n * n 的 01 矩阵,求满秩的概率

题目分析:首先公式是:f[n]=\prod_{i=0}^{n-1}\frac{2^n-2^i}{2^n}=\frac{\prod_{i=0}^{n-1}(2^n-2^i)}{2^{n^2}}

稍微解释一下吧,将 n * n 的矩阵视为 n 个长度为 n 的向量,对于每一个长度为 n 的向量,都有 2^n 种情况,因为要求 n 个向量线性独立,所以我们依次讨论:

  1. 对于第一个向量来说,只需要至少有一个位置为 1 即可,即全部为 0 的情况不满足条件,此时满足条件的情况为2^n-1
  2. 对于第二个向量来说,需要满足的条件是,不能全部为 0 ,且不能被第一个向量所表示,此时满足条件的情况为2^n-2
  3. 对于第三个向量来说,同样不能为 0 ,不能被第一个向量所表示,不能被第二个向量所表示,不能被第一个向量和第二个向量所表示,此时满足条件的情况为2^n-4
  4. 对于第四个向量来说,不能被前面向量表示的集合为 { ∅ , { 1 } , { 2 } , { 3 } , { 1 , 2 } , { 1 , 3 } , { 2 , 3 } , { 1 , 2 , 3 } } ,共八种情况,所以此时满足条件的情况为2^n-8
  5. ......
  6. 对于第 n 个向量来说,不能被前面 n - 1 个向量单独或混合表示,此时满足条件的情况为2^n-2^{n-1}

然后下一步就是将其转换为递推式,根据

f[n]=\frac{\prod_{i=0}^{n-1}(2^n-2^i)}{2^{n^2}},f[n+1]=\frac{\prod_{i=0}^{n}(2^{n+1}-2^i)}{2^{(n+1)^2}}=\frac{\prod_{i=1}^{n}(2^{n+1}-2^i)*(2^{n+1}-1)}{2^{n^2}*2^{2n}*2}=\frac{2^n*\prod_{i=1}^{n}(2^n-2^{i-1})*(2^{n+1}-1)}{2^{n^2}*2^n*2^n*2}=\frac{\prod_{i=0}^{n-1}(2^n-2^i)*(2^{n+1}-1)}{2^{n^2}*2^n*2}=\frac{f[n]*(2^{n+1}-1)}{2^{n+1}}

实现的话递推 2 的逆元就好了,在 mod 为 1e9+7 时,2 的逆元为 500000004

代码:
 

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;
  
typedef long long LL;
  
typedef unsigned long long ull;
  
const int inf=0x3f3f3f3f;
 
const int N=2e7+100;

const int mod=1e9+7;

LL f[N],inv=500000004,ans[N];

void init()
{
	ans[1]=f[1]=500000004;
	LL _2=4,inv_2=inv*inv%mod;
	for(int i=2;i<N;i++)
	{
		f[i]=f[i-1]*(_2-1)%mod*inv_2%mod;
		_2=_2*2%mod;
		inv_2=inv_2*inv%mod;
		ans[i]=ans[i-1]^f[i];
	}
}

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	init();
	int w;
	cin>>w;
	while(w--)
	{
		int n;
		scanf("%d",&n);
		printf("%lld\n",ans[n]);
	}








    return 0;
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Frozen_Guardian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值