快速数论变换ntt。

早上才刚刚接触了一下FFT,然后就开始撸这题了,所以要详细地记录一下。

看了这篇巨巨的博客才慢慢领会的:http://blog.csdn.net/cqu_hyx/article/details/52194696

FFT的作用是计算卷积。可以简单的理解为计算多项式*多项式最后得到的多项式,暴力计算是O(n*n)的,FFT可以做到O(nlogn)。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar(); x = ;while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int maxn=;
const LL mod=;
const LL G=; LL t[maxn],a[maxn],b[maxn],c[maxn],f[maxn],fac[maxn],NI[maxn];
int T,n,m;
LL rev[maxn],N,len,inv; LL POW[maxn],NiPOW[maxn]; LL power(LL x,LL y)
{
LL res=;
for(;y;y>>=,x=(x*x)%mod)
{
if(y&)res=(res*x)%mod;
}
return res;
} void init()
{
while((n+m)>=(<<len))len++;
N=(<<len);
inv=power(N,mod-);
for(int i=;i<N;i++)
{
LL pos=;
LL temp=i;
for(int j=;j<=len;j++)
{
pos<<=;pos |= temp&;temp>>=;
}
rev[i]=pos;
}
} void ntt(LL *a,LL n,LL re)
{
for(int i=;i<n;i++)
{
if(rev[i]>i)
{
swap(a[i],a[rev[i]]);
}
}
for(int i=;i<=n;i<<=)
{
int mid=i>>; LL wn=power(G,(mod-)/i);
if(re) wn=power(wn,(mod-));
for(int j=;j<n;j+=i)
{
LL w=;
for(int k=;k<mid;k++)
{
int temp1=a[j+k];
int temp2=(LL)a[j+k+mid]*w%mod;
a[j+k]=(temp1+temp2);if(a[j+k]>=mod)a[j+k]-=mod;
a[j+k+mid]=(temp1-temp2);if(a[j+k+mid]<)a[j+k+mid]+=mod;
w=(LL)w*wn%mod;
}
}
}
if(re)
{
for(int i=;i<n;i++)
{
a[i]=(LL)a[i]*inv%mod;
}
}
} bool cmp(LL a,LL b) {return a>b;} LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(a==&&b==) return -;
if(b==){x=;y=;return a;}
LL d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
} LL mod_reverse(LL a,LL n)
{
LL x,y;
LL d=extend_gcd(a,n,x,y);
if(d==) return (x%n+n)%n;
else return -;
} int main()
{
fac[]=; for(int i=;i<=;i++) fac[i]=(LL)i*fac[i-]%mod;
for(int i=;i<=;i++) NI[i]=mod_reverse(fac[i],mod);
POW[]=; for(int i=;i<=;i++) POW[i]=(LL)*POW[i-]%mod;
for(int i=;i<=;i++) NiPOW[i]=mod_reverse(POW[i],mod); scanf("%d",&T); while(T--)
{
len=; memset(c,,sizeof c); memset(a,,sizeof a); memset(b,,sizeof b); scanf("%d",&n); m=n;
for(int i=;i<=n;i++) { int x; scanf("%d",&x); t[i]=(LL)x; } sort(t+,t++n,cmp);
for(int i=;i<n;i++)
{
LL x=fac[n]*NI[i]%mod;
a[i]=x*POW[n-i]%mod;
}
for(int i=;i<=n;i++) b[n-i]=t[i]*fac[i-]%mod; init(); ntt(a,N,); ntt(b,N,);
for(int i=;i<=N;i++) c[i]=a[i]*b[i]%mod;
ntt(c,N,); for(int i=;i<n;i++) f[n-i]=c[i]*NI[n]%mod;
for(int i=;i<=n;i++) f[i]=f[i]*NI[i-]%mod;
for(int i=;i<=n;i++) f[i]=f[i]*NiPOW[i]%mod;
LL ans=; for(int i=;i<=n;i++) { ans=(ans+f[i])%mod; printf("%lld ",ans); }
printf("\n");
}
return ;
}

HDU 5829 Rikka with Subset的更多相关文章

  1. HDU 5829 Rikka with Subset(NTT)

    题意 给定 \(n\) 个数 \(a_1,a_2,\cdots a_n\),对于每个 \(K\in[1,n]\) ,求出 \(n\) 个数的每个子集的前 \(K\) 大数的和,输出每个值,对 \(99 ...

  2. HDU 6092`Rikka with Subset 01背包变形

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. HDU 6092 Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. hdu 6092 Rikka with Subset(逆向01背包+思维)

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. 2017 ACM暑期多校联合训练 - Team 5 1008 HDU 6092 Rikka with Subset (找规律)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  6. hdu 6092 Rikka with Subset (集合计数,01背包)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  7. HDU 6092 Rikka with Subset(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=6092 题意: 给出两个数组A和B,A数组一共可以有(1<<n)种不同的集合组合,B中则记录了每个数出 ...

  8. hdu 6092 Rikka with Subset(多重背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6092 #include <cstdio> #include <iostream> ...

  9. HDU 6092:Rikka with Subset(dp)

    分析 很多个较小的数字可以随机组合成较大的数字,所以B数组从小到大开始遍历,除了空集,最小的那个存在的个数对应的数字必然是a数组中的数字. 每求出这一部分之后,更新后续的B序列. 分析完后,主要的难点 ...

随机推荐

  1. 结构-行为-样式-css&html横纵居中最佳实践

    最近在做手机端的H5项目,有个标题是在一根横线中的,就是水平居中加垂直居中(如图一).这应该是前端开发中经常遇到的一个场景了,做的次数多了就有一些体会,我今天就总结了下这种结构的实现思路:首先,用元素 ...

  2. [ios]iphone 获取UIWebView内Html方法

    原文:http://blog.csdn.net/diyagoanyhacker/article/details/6564897 获取所有html:NSString *lJs = @"docu ...

  3. 关于ABP聚合根类AggregateRoot的思考

    AggregateRoot和Entity的区别 AggregateRoot继承于Entity,并实现了IGeneratesDomainEvents接口 public class AggregateRo ...

  4. 【转】经典!python中使用xlrd、xlwt操作excel表格详解

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  5. js中常用的Math方法总结

    1.min()和max()方法 Math.min()用于确定一组数值中的最小值.Math.max()用于确定一组数值中的最大值. alert(Math.min(2,4,3,6,3,8,0,1,3)); ...

  6. shell -- yes or no

    read -p "Do you want to .... [Y/N]? " yn while true; do case $yn in [Yy]|[Yy][Ee][Ss] ) br ...

  7. iOS strong与weak的使用

    strong修饰的属性是强指针类型的,weak修饰的属性是弱指针类型的 ARC对于内存中的对象管理机制,当某个对象没有被强指针指向的时候,该对象就会被销毁. 所以不适当的使用strong和weak修饰 ...

  8. C语言调用lua

    在lua API中,调用一个函数的步骤很简单: 1.压入你要调用的函数,使用lua_getglobal. 2.压入调用参数. 3.使用lua_pcall 4.从栈中弹出结果.   举例说明,假设你有这 ...

  9. Http中的Get/Post方法

    这篇文章源自http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是G ...

  10. 安卓系统浏览器中select下拉按钮无法弹出选择面板奇怪问题解决

    今天遇到个让人崩溃的问题: 平台: 安卓 4.0 描述: 使用 appcan 开发 hybrid 应用,手机上点击下拉选框按钮无法弹出选择面板. 说明: 发现 webkit 内核 position:f ...