1. 程式人生 > 實用技巧 >牛客網Binary Vector

牛客網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 independentvectors in the next nnn days modulo 109+710^9+7109+7.Formally, it can be proved that the answer has the form ofPQ\frac{P}{Q}QP, wherePPPandQQQ are coprime andQQQ is not a multiple of 109+710^9+7109+7. The answer modulo 109+710^9+7109+7 thus means P⋅Q−1(mod109+7)P \cdot Q^{-1} (\textrm{mod}\ 10^9+7 )PQ1(mod109+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;
    }
}//很短