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

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

题目链接

题目描述

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 A n A^n An. Now she wonders the probability of generating nn linearly independent vectors in the next nn days modulo 1 0 9 + 7 10^9+7 109+7. Formally, it can be proved that the answer has the form of P Q \frac{P}{Q} QP, where P and Q are coprime and Q is not a multiple of 1 0 9 + 7 10^9+7 109+7. The answer modulo 1 0 9 + 7 10^9+7 109+7 thus means P ⋅ Q − 1 ( mod  1 0 9 + 7 ) P \cdot Q^{-1} (\textrm{mod}\ 10^9+7 ) PQ1(mod 109+7), where Q − 1 Q^{-1} Q1 is the multiplicative inverse of 1 0 9 + 7 10^9+7 109+7.

Wcy thinks the problem too easy. Let the answer of n be f n f_n fn, she wants to know f 1 ⊕ f 2 ⊕ . . . ⊕ f N f_1\oplus f_2\oplus ...\oplus f_N f1f2...fN, where \oplus⊕ denotes bitwise exclusive or operation.

Note that when adding up two vectors, the components are modulo 2.

输入描述:

The input contains multiple test cases. The first line of input contains one integer T   ( 1 ≤ T ≤ 1000 ) T\ (1\le T\le 1000 ) T (1T1000), denoting the number of test cases.

In the following T lines, each line contains an integer N   ( 1 ≤ N ≤ 2 ∗ 1 0 7 ) N\ (1\le N\le 2*10^7 ) N (1N2107) describing one test case.

输出描述:

For each test case, output one integer indicating the answer.

示例1

输入

3
1
2
3

输出

500000004
194473671
861464136

这种毒瘤题建议大家别读题(如果不是数学大佬就别浪费时间阅读理解,我们队就是一直死磕题目意思浪费了很多时间),直接找规律得了,事实证明这题就是规律题,我们通过 o e i s oeis oeis 可以发现分子就是 ∏ 2 i − 1 , i ∈ [ 1 , n ] \prod{2^i-1},i\in[1,n] 2i1,i[1,n],分母就是 2 i ∗ ( i + 1 ) / 2 2^{i*(i+1)/2} 2i(i+1)/2,如果直接预处理你就会 T 飞,所以我们考虑优化,因为这个 n 非常大,所以我们要保证每次循环里的计算尽可能接近 O ( 1 ) O(1) O(1),下面观察答案,这题其实和杭电多校那个斐波那契求和很像,就是每次循环更新一下答案,避免重复计算,可以发现答案每次就是乘上一个 2 i − 1 2 i \frac{2^i-1}{2^i} 2i2i1,那么我们可以设一个初值 p 1 = 1 p1=1 p1=1,每次乘二,表示分子,设一个初值 p 2 = ( 500000004 ≡ 1 2 % 1 e 9 + 7 ) p2=(500000004\equiv\frac{1}{2}\%1e9+7) p2=(50000000421%1e9+7),每次乘 500000004 500000004 500000004 表示分母,那么更新答案其实就是乘上 ( p 1 − 1 ) ∗ p 2 (p1-1)*p2 (p11)p2 就行了,这样能保证循环内的运算都是 O ( 1 ) O(1) O(1) 的,AC代码如下:

#include
using namespace std;
typedef long long ll;
const int N=2e7+5;
const ll mod=1e9+7;
const ll inv2=500000004;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
ll f[N],ans[N];
void init(){
    f[0]=1;
    ll p1=1,p2=1;
    for(ll i=1;i<=2e7;i++){
        p1=2*p1%mod;
        p2=inv2*p2%mod;
        f[i]=f[i-1]*p2%mod*(p1-1)%mod;
        ans[i]=f[i]^ans[i-1];
    }
}
int main(){
    init();
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        printf("%lld\n",ans[n]);
    }
    return 0;
}

你可能感兴趣的:(数论,牛客)