[JZOJ4203] Tree

Description

这里写图片描述
这里写图片描述

注意:本题空间限制为7168KB

Solution

我们按照每层操作涉及的区间长度来考虑,以包含1的区间为例,[1,1],[1,2],[1,4],[1,8]…..
可以发现这很像FFT中的蝴蝶变换
前半段转移是or,后半段转移是and

于是只需要开一个 2 n 的数组,类似FFT的过程写出来,最后变换到顶层每个位置就是所对应的i
直接累加答案即可
复杂度 O ( n 2 n )

Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define LL unsigned long long
using namespace std;
const LL mo=4294967296;
unsigned int a[1048576],n,x,y,z,M;
int main()
{
    cin>>n>>a[0]>>x>>y>>z;
    M=1<<n;
    fo(i,1,M-1) a[i]=((LL)a[i-1]*(LL)x+(LL)y)%mo;
    int half=1;
    for(int m=2;m<=M;half=m,m<<=1)
    {
        fo(i,0,half-1)
        {
            for(int j=i;j<M;j+=m)
            {
                unsigned int v=a[j+half];
                a[j+half]=a[j]&v;
                a[j]=a[j]|v;
            }
        }
    }
    LL s=0,c1=1;
    fo(i,0,M-1)
    {
        s=(s+(LL)a[i]*c1%mo)%mo;
        c1=(LL)c1*(LL)z%mo;
    }
    printf("%llu\n",s);
}

猜你喜欢

转载自blog.csdn.net/hzj1054689699/article/details/80735455
今日推荐