【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('\n')
#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();
}
【51nod】1773 A国的贸易的更多相关文章
- [51Nod 1773] A国的贸易
[51Nod 1773] A国的贸易 题目描述 A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们有着神奇的贸易规则. ...
- 【51Nod1773】A国的贸易 解题报告
[51Nod1773]A国的贸易 Description 给出一个长度为 \(2^n\) 的序列,编号从\(0\)开始.每次操作后,如果 \(i\) 与 \(j\) 的二进制表示只差一位则第 \(i\ ...
- 51NOD 1773:A国的贸易——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1773 参考1:FWT讲解 https://www.cnblogs.com ...
- 51nod1773 A国的贸易
基准时间限制:2 秒 空间限制:524288 KB 分值: 40 A国是一个神奇的国家. 这个国家有 2n 个城市,每个城市都有一个独一无二的编号 ,编号范围为0~2n-1. A国的神奇体现在,他们 ...
- 51Nod1773 A国的贸易 多项式 FWT
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1773.html 题目传送门 - 51Nod1773 题意 给定一个长度为 $2^n$ 的序列,第 $ ...
- 【51Nod1773】A国的贸易 FWT+快速幂
题目描述 给出一个长度为 $2^n$ 的序列,编号从0开始.每次操作后,如果 $i$ 与 $j$ 的二进制表示只差一位则第 $i$ 个数会加上操作前的第 $j$ 个数.求 $t$ 次操作后序列中的每个 ...
- NOIP2009最优贸易[spfa变形|tarjan 缩点 DP]
题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...
- 【NOIP2009 T3】 最佳贸易 (双向SPFA)
C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...
- [NOIP2009] 提高组 洛谷P1073 最优贸易
题目描述 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个 城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分 为双向通行的道路 ...
随机推荐
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...
- ElastAlert规则
elastalert 是一款基于elasticsearch的开源告警产品(官方说明文档).相信许多人都会使用ELK做日志收集系统,但是产生一个基于日志的“优秀”的安全告警确是一个难题.告警规则难编写, ...
- Android 利用 gson 将 json 转成 对象object 集合list
1.build.gradle 中引入gson compile 'com.google.code.gson:gson:2.8.5' 2.将后台返回的json数据转对象.List.时间格式与后台返回的时间 ...
- swiper隐藏再显示出现点击不了情况
//初始化swiper var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', nextBut ...
- MySQL在net中Datatime转换
<add name="adDb" connectionString="Persist Security Info=False;database=ad ...
- javascript 高度相关
//scrollTop; var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop); ...
- ETL testing
https://www.tutorialspoint.com/etl_testing/index.htm querysurge-installer-6.0.5-linux-x64 测试ETL的工具.
- mysql的force index
MSQL中使用order by 有个坑,会默认走order by 后面的索引.而不走where条件里应该走的索引.大家在使用时要绕过此坑. 如下语句因为order by 走了settle_id这个主键 ...
- 【译】第十篇 Replication:故障排除
本篇文章是SQL Server Replication系列的第十篇,详细内容请参考原文. 复制故障排除是一项艰巨的任务.在任何复制设置中,都涉及到很多移动部件,而可用的工具并不总是很容易识别问题.Th ...
- Eureka简介
Eureka是Spring Cloud Netfix 的一个子模块,也是核心模块之一,用于云端服务发现.一个基于RestFul的服务,用于定位服务,以实现云端中间层服务发现和中间层转移. 服务注册与发 ...