题解

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变换一下,然后快速幂,之后和原数组卷积起来就是答案了

代码

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #define enter putchar('\n')
  6. #define space putchar(' ')
  7. #define MAXN 2000005
  8. //#define ivorysi
  9. using namespace std;
  10. typedef long long int64;
  11. template<class T>
  12. void read(T &res) {
  13. res = 0;char c = getchar();T f = 1;
  14. while(c < '0' || c > '9') {
  15. if(c == '-') f = -1;
  16. c = getchar();
  17. }
  18. while(c >= '0' && c <= '9') {
  19. res = res * 10 - '0' + c;
  20. c = getchar();
  21. }
  22. res = res * f;
  23. }
  24. template<class T>
  25. void out(T x) {
  26. if(x < 0) {x = -x;putchar('-');}
  27. if(x >= 10) out(x / 10);
  28. putchar('0' + x % 10);
  29. }
  30. const int MOD = 1000000007;
  31. int N,T,g[MAXN],f[MAXN],Inv2 = (MOD + 1) / 2;
  32. int inc(int a,int b) {
  33. return a + b >= MOD ? a + b - MOD : a + b;
  34. }
  35. int mul(int a,int b) {
  36. return 1LL * a * b % MOD;
  37. }
  38. void FWT(int *a) {
  39. for(int i = 1 ; i < (1 << N) ; i <<= 1) {
  40. for(int j = 0 ; j < (1 << N) ; j += (i << 1)) {
  41. for(int k = 0 ; k < i ; ++k) {
  42. int t0 = a[j + k],t1 = a[j + k + i];
  43. a[j + k] = inc(t0,t1);
  44. a[j + k + i] = inc(t0,MOD - t1);
  45. }
  46. }
  47. }
  48. }
  49. void IFWT(int *a) {
  50. for(int i = 1 ; i < (1 << N) ; i <<= 1) {
  51. for(int j = 0 ; j < (1 << N) ; j += (i << 1)) {
  52. for(int k = 0 ; k < i ; ++k) {
  53. int t0 = a[j + k],t1 = a[j + k + i];
  54. a[j + k] = mul(inc(t0,t1),Inv2);
  55. a[j + k + i] = mul(inc(t0,MOD - t1),Inv2);
  56. }
  57. }
  58. }
  59. }
  60. void conv(int *a,int *b) {
  61. for(int i = 0 ; i < (1 << N) ; ++i) a[i] = mul(a[i],b[i]);
  62. }
  63. void fpow(int *a,int *ans,int c) {
  64. static int t[MAXN];
  65. for(int i = 0 ; i < (1 << N) ; ++i) t[i] = a[i],ans[i] = a[i];
  66. --c;
  67. while(c) {
  68. if(c & 1) conv(ans,t);
  69. conv(t,t);
  70. c >>= 1;
  71. }
  72. }
  73. void Solve() {
  74. read(N);read(T);
  75. g[0] = 1;
  76. for(int i = 0 ; i < N ; ++i) g[1 << i] = 1;
  77. for(int i = 0 ; i < (1 << N) ; ++i) read(f[i]);
  78. FWT(g);FWT(f);
  79. fpow(g,g,T);
  80. conv(f,g);
  81. IFWT(f);
  82. for(int i = 0 ; i < (1 << N) ; ++i) {
  83. out(f[i]);if(i != (1 << N) - 1) space;
  84. }
  85. enter;
  86. }
  87. int main() {
  88. #ifdef ivorysi
  89. freopen("f1.in","r",stdin);
  90. #endif
  91. Solve();
  92. }

【51nod】1773 A国的贸易的更多相关文章

  1. [51Nod 1773] A国的贸易

    [51Nod 1773] A国的贸易 题目描述 A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们有着神奇的贸易规则. ...

  2. 【51Nod1773】A国的贸易 解题报告

    [51Nod1773]A国的贸易 Description 给出一个长度为 \(2^n\) 的序列,编号从\(0\)开始.每次操作后,如果 \(i\) 与 \(j\) 的二进制表示只差一位则第 \(i\ ...

  3. 51NOD 1773:A国的贸易——题解

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1773 参考1:FWT讲解 https://www.cnblogs.com ...

  4. 51nod1773 A国的贸易

    基准时间限制:2 秒 空间限制:524288 KB 分值: 40  A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们 ...

  5. 51Nod1773 A国的贸易 多项式 FWT

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1773.html 题目传送门 - 51Nod1773 题意 给定一个长度为 $2^n$ 的序列,第 $ ...

  6. 【51Nod1773】A国的贸易 FWT+快速幂

    题目描述 给出一个长度为 $2^n$ 的序列,编号从0开始.每次操作后,如果 $i$ 与 $j$ 的二进制表示只差一位则第 $i$ 个数会加上操作前的第 $j$ 个数.求 $t$ 次操作后序列中的每个 ...

  7. NOIP2009最优贸易[spfa变形|tarjan 缩点 DP]

    题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...

  8. 【NOIP2009 T3】 最佳贸易 (双向SPFA)

    C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...

  9. [NOIP2009] 提高组 洛谷P1073 最优贸易

    题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...

随机推荐

  1. MySQL学习(二)——MySQL多表

    分页操作:使用limit(参数1,参数2) 起始位置(参数1))*每页显示的条数(参数2) .分类表 create table category( cid ) primary key, cname ) ...

  2. okhttp 简单用法

    1.gradle 依赖 github 中查找最新的 2.MyApplication oncreate 中: @Override public void onCreate () { super.onCr ...

  3. List(JDK1.7)(2)

    LinkedList List接口和Deque接口的一种双向链表实现.非同步的. 快速失败机制.ConcurrentModificationException 结点结构 插入结点 删除结点 add() ...

  4. .NET 定时器类及使用方法

    Timer类实现定时任务 //2秒后开启该线程,然后每隔4s调用一次 System.Threading.Timer timer = new System.Threading.Timer((n) =&g ...

  5. anonymous namespace V.S. static variant

    [anonymous namespace V.S. static variant] 在C语言中,如果我们在多个tu(translation unit)中使用了同一个名字做为函数名或者全局变量名,则在链 ...

  6. spring-mvc Mybatis插件打印SQL

    代码: package com.chainup.exchange.service.adapter; import com.chainup.exchange.service.impl.AccountSe ...

  7. Windows上安装QT4后更改MinGW的路径

    在windows上安装使用MinGW的QT4时,并不会一起安装MinGW. 在安装过程中,会让你指定已经安装的MinGW的路径. 当你使用QT4时,将使用你指定的MinGW的路径下的g++来编译构建程 ...

  8. subtle:有趣的伪平铺式窗口管理器

    Author:吴吉庆 email: jiqingwu@gmail.com home:http://hi.baidu.com/jiqing0925 create:2011-02-19 update:20 ...

  9. Codeforces Round #540 (Div. 3)题解

    题目链接: https://codeforces.com/contest/1118 A题: 题意: q次查询,给你一个n,要你用1和2来凑出n,1的花费为a,2的花费为b,求花费的最小值. 思路: 我 ...

  10. POJ 1986 Distance Queries (Tarjan算法求最近公共祖先)

    题目链接 Description Farmer John's cows refused to run in his marathon since he chose a path much too lo ...