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 ...
随机推荐
- CentOS7系列--1.3CentOS7用户管理
CentOS7用户管理 1. 添加用户 [root@centos7 ~]# useradd jack [root@centos7 ~]# passwd jack Changing password f ...
- FineReport中如何用JavaScript解决控件值刷新不及时
我们经常利用按钮进行一些页面值的处理工作,但是默认的逻辑造成,每次新填报的值,需要点击下空白区域或是执行某个其他操作才可以被正确读取,那么我们如何处理呢? 例:当我们用常规取值的时候,虽然B3单元格录 ...
- Base64编码和解码实现
function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...
- Object toString方法
1.System.out.println()里的参数会自动调用toString方法. package com.mydemo.controller; // 1.getClass().getName() ...
- SSH安全
新建用户,设置密码 useradd eason passwd eason 不允许root直接登陆 修改配置文件 vi /etc/ssh/sshd_config 禁止root登录 查找“#PermitR ...
- [翻译] GoogleMaterialDesignIcons
GoogleMaterialDesignIcons Google Material Design Icons Font for iOS GMD图片样式的字体,用于iOS开发. It is based ...
- Zabbix日常监控之lvs监控
参考博文:http://blog.51cto.com/kaibinyuan/1711863 监控环境的搭建请参考:https://www.cnblogs.com/huangyanqi/p/918780 ...
- December 01st 2016 Week 49th Thursday
Life is a maze and love is a riddle. 生活是个迷宫,爱情是个谜语. I am lost in both. Can you provide me some guida ...
- CSS 鼠标样式
设置鼠标指针放在一个元素边界范围内时所用的光标形状,需要对元素的css属性cursor进行设置. cursor属性可能的值: default 默认光标(通常是一个箭头) auto 默认.浏览器设置的( ...
- vue项目用nodejs实现模拟数据方法
1)在项目根目录(如demo)中创建一个文件夹,如base,将项目中所有的前端文件全部放到base文件夹中,此时项目demo下只有一个文件夹base 2)通过cmd进入命令窗口,直接执行npm ins ...