51nod1172 Partial Sums V2
推一下式子发现是裸的FFT,$ans[k]=\sum_{i}\sum_{j}[i+j=k]a[i]*C_{m-1+j}^{j}$
比较坑爹的就是这个模数,于是我们上任意模数的FFT
任意模数的FFT目的就是降低卷积中的元素上界,我们设$P=\lfloor \sqrt{mod} \rfloor$,
我们将原函数中的系数变成两个$a1[i]=a[i]/P,a2[i]=a[i]%P,b1[i]=b[i]/P,b2[i]=b[i]%P$
这样我们新卷积出来的值的上限是$n*mod$,
$P^2$的系数是$a1*b1$,$P$的系数是$a1*b2+a2*b1$,$1$的系数是$a2*b2$,然后我们再分别卷积,最后统计答案即可
一开始炸精了。。改成预处理单位复数根就好了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define mod 1000000007
#define MAXN 131072
using namespace std;
const double pi=acos(-1.0);
const int P=;
struct cmx{
double x,y;
cmx(){}
cmx(double a,double b){x=a,y=b;}
cmx operator + (cmx a){return cmx(x+a.x,y+a.y);}
cmx operator - (cmx a){return cmx(x-a.x,y-a.y);}
cmx operator * (cmx a){return cmx(x*a.x-y*a.y,x*a.y+y*a.x);}
cmx operator / (double a){return cmx(x/a,y/a);}
}A[MAXN],B[MAXN],C[MAXN],D[MAXN],E[MAXN],F[MAXN],G[MAXN],wn[MAXN],inv[MAXN];
int n,m,N,rev[MAXN];
long long ans[MAXN],a[MAXN],b[MAXN];
void fft(cmx *a,int o, cmx *wn){
register int i,j,k;
cmx t;
for(i=;i<N;i++)if(i<rev[i])swap(a[i],a[rev[i]]);
for(k=;k<=N;k<<=)
for(j=;j<N;j+=k)
for(i=;i<(k>>);i++){
t=wn[N/k*i]*a[i+j+(k>>)];
a[i+j+(k>>)]=a[i+j]-t;
a[i+j]=a[i+j]+t;
}
if(o==-)for(i=;i<N;i++)a[i]=a[i]/N;
}
void FFT(long long *a,long long *b,long long *ans){
register int i;
for(i=;i<N;i++){
A[i]=cmx(a[i]/P,);
B[i]=cmx(a[i]%P,);
C[i]=cmx(b[i]/P,);
D[i]=cmx(b[i]%P,);
}
fft(A,,wn);fft(B,,wn);fft(C,,wn);fft(D,,wn);
for(i=;i<N;i++){
E[i]=A[i]*C[i];
F[i]=A[i]*D[i]+B[i]*C[i];
G[i]=B[i]*D[i];
}
fft(E,-,inv);fft(F,-,inv);fft(G,-,inv);
register long long tmp;
for(i=;i<n;i++){
tmp=1ll*round(E[i].x);
ans[i]=tmp%mod*P%mod*P%mod;
tmp=1ll*round(F[i].x);
(ans[i]+=tmp%mod*P%mod)%=mod;
(ans[i]+=1ll*round(G[i].x))%=mod;
}
}
long long qp(long long a,int b){
long long c=;
while(b){
if(b&)c=c*a%mod;
a=a*a%mod; b>>=;
}return c;
}
int main(){
scanf("%d%d",&n,&m);
register int i;
for(i=;i<n;i++)scanf("%lld",&a[i]);
b[]=;
for(i=;i<n;i++)
b[i]=1ll*b[i-]*(m-+i)%mod*qp(i,mod-)%mod;
for(N=;N<(n<<);N<<=);
for(i=;i<N;i++){
if(i&)rev[i]=(rev[i>>]>>)|(N>>);
else rev[i]=rev[i>>]>>;
}
for(i=;i<N;i++)
wn[i]=cmx(cos(*pi/N*i),sin(*pi/N*i)),
inv[i]=cmx(cos(*pi/N*i),-sin(*pi/N*i));
FFT(a,b,ans);
for(i=;i<n;i++)printf("%lld\n",ans[i]);
return ;
}
51nod1172 Partial Sums V2的更多相关文章
- 51nod 1172 Partial Sums V2 卡精度的任意模数FFT
卡精度的任意模数fft模板题……这道题随便写个表就能看出规律来(或者说考虑一下实际意义),反正拿到这题之后,很快就会发现他是任意模数fft模板题.然后我就去网上抄了一下板子……我打的是最土的任意模数f ...
- 51nod 1172 Partial Sums V2
题目 给出一个数组A,经过一次处理,生成一个数组S,数组S中的每个值相当于数组A的累加,比如:A = {1 3 5 6} => S = {1 4 9 15}.如果对生成的数组S再进行一次累加操作 ...
- 51nod1161 Partial Sums
开始想的是O(n2logk)的算法但是显然会tle.看了解题报告然后就打表找起规律来.嘛是组合数嘛.时间复杂度是O(nlogn+n2)的 #include<cstdio> #include ...
- Non-negative Partial Sums(单调队列)
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 4193 Non-negative Partial Sums 单调队列。
Non-negative Partial Sums Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- TOJ 1721 Partial Sums
Description Given a series of n numbers a1, a2, ..., an, the partial sum of the numbers is defined a ...
- 【计数】cf223C. Partial Sums
考试时候遇到这种题只会找规律 You've got an array a, consisting of n integers. The array elements are indexed from ...
- CodeForces 223C Partial Sums 多次前缀和
Partial Sums 题解: 一个数列多次前缀和之后, 对于第i个数来说他的答案就是 ; i <= n; ++i){ ; j <= i; ++j){ b[i] = (b[i] + 1l ...
- 51 Nod 1161 Partial sums
1161 Partial Sums 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 取消关注 给出一个数组A,经过一次 ...
随机推荐
- iOS使用第三方管理工具
1.安装cocoaPods 移除当前镜像,因为需要FQ跨域访问 001.gem source --remove https://rubygems.org/ 使用淘宝镜像安装 002.https://r ...
- Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)
根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...
- java死锁小例子
package cn.com.io.threadDemo.ThreadSyn; /** * 通过两个属性值创建死锁 * 本程序通过两个线程各自锁定一个属性值,这样两个线程都无法结束,造成死锁 * @a ...
- 菜鸟级Git GitHub创建仓库
菜鸟标准:知道pwd ,rm 命令是什么. 一.Git 是什么. git 是目前世界上最先进的分布式版本控制系统 二.SVN与Git 1.版本控制系统 SVN 是集中式版本控制系统,版本库是集中放在中 ...
- kindeditor修改允许上传的图片、视频、音频大小
在jsp文件夹下,有个upload_json.jsp文件,打开找到: //最大文件大小 ; 修改数值即可.默认1000000,即为1M.
- Ocelot中文文档-日志
目前,Ocelot使用标准的日志记录接口ILoggerFactory/ILogger . 在IOcelotLogger / IOcelotLoggerFactory中提供了标准的asp.net cor ...
- Scala编程入门---面向对象编程之对象
对象 Object,相当于class单个实例,通常在里面放一些静态的filed或method 第一次调用object方法时候,就会执行object的constructor,也就是Object中不在me ...
- Ext Js v6.2.0.103 Sencha Cmd 命令
Sencha Cmd v6.2.0.103 Sencha Cmd 提供几种全局开关命令. 在大多数案例中, 第一步是在Sencha SDK基础上创建应用 例如 Ext JS 或 Sencha Touc ...
- java基础语法3
逻辑运算符 &:与,和有共同的,必须条件都满足才是true 有false就返回false,必须都是true才返回true |:或者,其中有一个满足条件就返回true ^亦或,相同是false, ...
- React从入门到放弃之前奏(1):webpack4简介
接触webpack是好久之前的事情了,最近看了下webpack没想到都到4了. webpack 是一个现代 JavaScript 应用程序的静态模块打包器(module bundler). 会创建1个 ...