BZOJ2194: 快速傅立叶之二(NTT,卷积)
Time Limit: 10 Sec Memory Limit: 259 MB
Submit: 1776 Solved: 1055
[Submit][Status][Discuss]
Description
请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 n < = 10 ^ 5。 a,b中的元素均为小于等于100的非负整数。
Input
Output
输出N行,每行一个整数,第i行输出C[i-1]。
Sample Input
3 1
2 4
1 1
2 4
1 4
Sample Output
12
10
6
1
HINT
Source
题目中给的公式不好搞
我们按照套路,将$B$翻转一下
$$C(k) = \sum_0^n a_i * b_{n - 1 - i + k}$$
此时后面的式子就只与$k$有关了
设$$D(n - 1 + k) = \sum_0^n a_i * b_{n - 1 - i + k}$$
直接NTT
#include<cstdio>
#define swap(x,y) x ^= y, y ^= x, x ^= y
#define LL long long
using namespace std;
const int MAXN = * 1e5 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '',c = getchar();
return x * f;
}
const int P = , g = , gi = ;
int N;
int
LL a[MAXN], b[MAXN], r[MAXN];
LL fastpow(LL a, int p, int mod) {
LL base = ;
while(p) {
if(p & ) base = (base * a) % mod;
a = (a * a) % mod; p >>= ;
}
return base % mod;
}
LL NTT(LL *A, int type, int N, int mod) {
for(int i = ; i < N; i++)
if(i < r[i]) swap(A[i], A[r[i]]);
for(int mid = ; mid < N; mid <<= ) {
LL W = fastpow( (type == ) ? g : gi, (P - ) / (mid << ), mod );
for(int j = ; j < N; j += (mid << )) {
int w = ;
for(int k = ; k < mid; k++, w = (w * W) % P) {
LL x = A[j + k] % P, y = w * A[j + k + mid] % P;
A[j + k] = (x + y) % P;
A[j + k + mid] = (x - y + P) % P;
}
}
}
if(type == -) {
LL inv = fastpow(N, mod - , mod);
for(int i = ; i < N; i++)
A[i] = (A[i] * inv) % mod;
}
}
int main() {
#ifdef WIN32
freopen("a.in","r",stdin);
#endif
N = read();
for(int i = ; i < N; i++)
a[i] = read(), b[N - i] = read();
int limit = , L = ;
while(limit <= N + N) limit <<=, L++;
for(int i = ; i < limit; i++) r[i] = (r[i >> ] >> ) | ((i & ) << (L - ));
NTT(a, , limit, P); NTT(b, , limit, P);
for(int i = ; i < limit; i++) a[i] = (a[i] * b[i]) % P;
NTT(a, -, limit, P);
for(int i = ; i < N * ; i++)
printf("%d\n",a[i] % P);
return ;
}
BZOJ2194: 快速傅立叶之二(NTT,卷积)的更多相关文章
- bzoj2194 快速傅立叶之二 ntt
bzoj2194 快速傅立叶之二 链接 bzoj 思路 对我这种和式不强的人,直接转二维看. 发现对\(C_k\)贡献的数对(i,j),都是右斜对角线. 既然贡献是对角线,我们可以利用对角线的性质了. ...
- BZOJ2194: 快速傅立叶之二 FFT_卷积
Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...
- [bzoj2194]快速傅立叶之二_FFT
快速傅立叶之二 bzoj-2194 题目大意:给定两个长度为$n$的序列$a$和$b$.求$c$序列,其中:$c_i=\sum\limits_{j=i}^{n-1} a_j\times b_{j-i} ...
- bzoj2194: 快速傅立叶之二
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- bzoj千题计划256:bzoj2194: 快速傅立叶之二
http://www.lydsy.com/JudgeOnline/problem.php?id=2194 相乘两项的下标 的 差相同 那么把某一个反过来就是卷积形式 fft优化 #include< ...
- BZOJ.2194.快速傅立叶之二(FFT 卷积)
题目链接 \(Descripiton\) 给定\(A[\ ],B[\ ]\),求\[C[k]=\sum_{i=k}^{n-1}A[i]*B[i-k]\ (0\leq k<n)\] \(Solut ...
- BZOJ2194:快速傅立叶之二(FFT)
Description 请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 n < = 10 ^ 5. a,b中的元素均为小于等于100的非 ...
- BZOJ2194 快速傅立叶之二 【fft】
题目 请计算C[k]=sigma(a[i]*b[i-k]) 其中 k < = i < n ,并且有 n < = 10 ^ 5. a,b中的元素均为小于等于100的非负整数. 输入格式 ...
- 2018.11.18 bzoj2194: 快速傅立叶之二(fft)
传送门 模板题. 将bbb序列反过来然后上fftfftfft搞定. 代码: #include<bits/stdc++.h> #define ri register int using na ...
随机推荐
- Android平台接入Facebook登录
官方教程地址: https://developers.facebook.com/docs/android/getting-started 开发环境为Android Studio,官方要求SDK最低版本 ...
- Html 表单标签 Form
Html表单 #转载请留言联系 表单用于搜集不同类型的用户输入,表单由不同类型的标签组成,相关标签及属性用法如下: 1.<form>标签 定义整体的表单区域 action属性 定义表单数据 ...
- Ad-hoc 查询以及动态SQL的罪恶[译]
本文为翻译文章,原文地址:https://www.simple-talk.com/blogs/2009/08/03/stolen-pages-ad-hoc-queries-and-the-sins-o ...
- statsmodels中的summary解读(以linear regression模型为例)
https://datatofish.com/statsmodels-linear-regression/ https://blog.datarobot.com/ordinary-least-squa ...
- 【Kettle】1、简单介绍
本人为转载,感谢作者的辛勤劳作:http://blog.csdn.net/rotkang/article/details/20810921 ------------------------------ ...
- asp.net mvc多级目录结构和多级area实现技巧
今天在工作要实现这个多级area.其原因是这个项目需要多级的功能,大的类别里有小的类别,小的类别里有具体的功能项,每一个功能项还有若干动作Action,所以在菜单和mvc工程的结构上都需要有体现多级的 ...
- inclusion_tag 界面的嵌套 和渲染
后端的html渲染到前端: 如果后端直接定义的是html标签,传到前端的时候因为浏览器的安全机制就会直接渲染成字符串如果想要渲染成需要的标签,就需要在后端用make_save()进行包裹,或者直接在前 ...
- 去除Xcode6创建工程时自带的storyboard
去除Xcode6创建工程时自带的storyboard 1. 删除storyboard文件,并在setting里面清空加载storyboard: 2. 导入ViewController到appDeleg ...
- 利用Linode面板Clone克隆搬家迁移不同VPS数据及利用IP Swap迁移IP地址
在众多海外VPS服务商中,老蒋个人认为Linode提供的VPS方案和性价比还是比较高的,尤其目前基础1GB方案仅需10美元每月且全部是SSD固态硬盘,无论是流量还是硬盘大小,基本上可以满足我们大部分用 ...
- C++ Circle
作业链接 https://github.com/How-Come/object-oriented/tree/master/Circle