zoukankan      html  css  js  c++  java
  • 【51nod】1773 A国的贸易

    题解

    FWT板子题

    可以发现
    (dp[i][u] = sum_{i = 0}^{N - 1} dp[i - 1][u xor (2^i)] + dp[i - 1][u])
    然后如果把异或提出来可以变成一个异或卷积
    也就是另一个数组里只有(0),(2^0),(2^1)...(2^{n - 1})有值

    用FWT变换一下,然后快速幂,之后和原数组卷积起来就是答案了

    代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #define enter putchar('
    ')
    #define space putchar(' ')
    #define MAXN 2000005
    //#define ivorysi
    using namespace std;
    typedef long long int64;
    template<class T>
    void read(T &res) {
        res = 0;char c = getchar();T f = 1;
        while(c < '0' || c > '9') {
    	if(c == '-') f = -1;
    	c = getchar();
        }
        while(c >= '0' && c <= '9') {
    	res = res * 10 - '0' + c;
    	c = getchar();
        }
        res = res * f;
    }
    template<class T>
    void out(T x) {
        if(x < 0) {x = -x;putchar('-');}
        if(x >= 10) out(x / 10);
        putchar('0' + x % 10);
    }
    const int MOD = 1000000007;
    int N,T,g[MAXN],f[MAXN],Inv2 = (MOD + 1) / 2;
    
    int inc(int a,int b) {
        return a + b >= MOD ? a + b - MOD : a + b;
    }
    int mul(int a,int b) {
        return 1LL * a * b % MOD;
    }
    void FWT(int *a) {
        for(int i = 1 ; i < (1 << N) ; i <<= 1) {
    	for(int j = 0 ; j < (1 << N) ; j += (i << 1)) {
    	    for(int k = 0 ; k < i ; ++k) {
    		int t0 = a[j + k],t1 = a[j + k + i];
    		a[j + k] = inc(t0,t1);
    		a[j + k + i] = inc(t0,MOD - t1);
    	    }
    	}
        }
    }
    void IFWT(int *a) {
        for(int i = 1 ; i < (1 << N) ; i <<= 1) {
    	for(int j = 0 ; j < (1 << N) ; j += (i << 1)) {
    	    for(int k = 0 ; k < i ; ++k) {
    		int t0 = a[j + k],t1 = a[j + k + i];
    		a[j + k] = mul(inc(t0,t1),Inv2);
    		a[j + k + i] = mul(inc(t0,MOD - t1),Inv2);
    	    }
    	}
        }
    }
    void conv(int *a,int *b) {
        for(int i = 0 ; i < (1 << N) ; ++i) a[i] = mul(a[i],b[i]);
    }
    void fpow(int *a,int *ans,int c) {
        static int t[MAXN];
        for(int i = 0 ; i < (1 << N) ; ++i) t[i] = a[i],ans[i] = a[i];
        --c;
        while(c) {
    	if(c & 1) conv(ans,t);
    	conv(t,t);
    	c >>= 1;
        }
    }
    void Solve() {
        read(N);read(T);
        g[0] = 1;
        for(int i = 0 ; i < N ; ++i) g[1 << i] = 1;
        for(int i = 0 ; i < (1 << N) ; ++i) read(f[i]);
        FWT(g);FWT(f);
        fpow(g,g,T);
        conv(f,g);
        IFWT(f);
        for(int i = 0 ; i < (1 << N) ; ++i) {
    	out(f[i]);if(i != (1 << N) - 1) space;
        }
        enter;
    }
    int main() {
    #ifdef ivorysi
        freopen("f1.in","r",stdin);
    #endif
        Solve();
    }
    
  • 相关阅读:
    Android实现资料收藏
    GPS(Global Positioning System)全球定位系统
    Android开发环境搭建全程演示(jdk+eclipse+android sdk)
    ADT在线安装
    简洁判断一个byte中有多少位为1的bit?
    tomcat部署java项目
    centos7把编译安装的服务通过systemctl管理
    ansible自动化部署
    redi通过哨兵sentinel实现主从切换
    redis实现集群加主从复制
  • 原文地址:https://www.cnblogs.com/ivorysi/p/9178577.html
Copyright © 2011-2022 走看看