牛客网Binary Vector

链接:https://ac.nowcoder.com/acm/contest/5671/B
来源:牛客网

题目描述:

Roundgod is obsessive about linear algebra. Let A={0,1}A=\{0,1\}A={0,1}, everyday she will generate a binary vector randomly in AnA^nAn. Now she wonders the probability of generating nnn linearly independent vectors in the next nnn days modulo 109+710^9+7109+7. Formally, it can be proved that the answer has the form of PQ\frac{P}{Q}QP, where PPP and QQQ are coprime and QQQ is not a multiple of 109+710^9+7109+7. The answer modulo 109+710^9+7109+7 thus means P⋅Q−1(mod 109+7)P \cdot Q^{-1} (\textrm{mod}\ 10^9+7 )PQ1(mod 109+7), where Q−1Q^{-1}Q1 is the multiplicative inverse of 109+710^9+7109+7.

Wcy thinks the problem too easy. Let the answer of nnn be fnf_nfn, she wants to know f1⊕f2⊕...⊕fNf_1\oplus f_2\oplus ...\oplus f_Nf1f2...fN, where ⊕\oplus⊕ denotes bitwise exclusive or operation.

Note that when adding up two vectors, the components are modulo 222.
翻译:
 

,每天Roundgod从 (即维度为n,每一位由01组成的所有向量的集合)中随机选择一个二进制向量。现在他想知道n天中选取n个线性独立向量的概率。答案的输出格式同上题,模数为

表示n的答案,最后输出 表示异或。

线性独立介绍:https://baike.baidu.com/item/%E7%BA%BF%E6%80%A7%E7%8B%AC%E7%AB%8B

对于问题B,A^n是仅包含0和1的n维向量。

输入描述:

输出描述:

示例1:

输入:

3
1
2
3

  

输出

500000004
194473671
861464136

  

说明:

由于这N个向量线性无关,则这N个向量张成的空间秩为N,考虑将每次随机的向量加入之前向量的空间;

由于这N个向量线性无关,则这N个向量张成的空间秩为N,考虑将每次随机的向量加入之前向量的空间;

那么最后N个向量秩为N当且仅当每次加入的向量都不属于之前的空间。
那么概率为
容易发现
 所以可以线性求出所有fn,时间复杂度为O(n)

了解以上内容,便是实现了:

#include<bits/stdc++.h>
using namespace std;
long long mod=1e9+7;
long long m=2,ff[20000055];
long long f,p=500000004,pp=500000004;
int main()
{
     
    f=ff[1]=p;
    for (int i=2; i<=20000050; i++)
    {
        m=m*2%mod; pp=pp*p%mod;
        f=f%mod*(m-1)%mod*pp%mod;
        ff[i]=ff[i-1]^f;
    }
    int t,n;
    cin>>t;
    while (t--)
    {
        cin>>n;
        cout<<ff[n]<<endl;
    }
}//很短

猜你喜欢

转载自www.cnblogs.com/WWWZZZQQQ/p/13387878.html
今日推荐