HDU 5829 Rikka with Subset
快速数论变换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的更多相关文章
- HDU 5829 Rikka with Subset(NTT)
题意 给定 \(n\) 个数 \(a_1,a_2,\cdots a_n\),对于每个 \(K\in[1,n]\) ,求出 \(n\) 个数的每个子集的前 \(K\) 大数的和,输出每个值,对 \(99 ...
- HDU 6092`Rikka with Subset 01背包变形
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 6092 Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 6092 Rikka with Subset(逆向01背包+思维)
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 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 ...
- hdu 6092 Rikka with Subset (集合计数,01背包)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- HDU 6092 Rikka with Subset(dp)
http://acm.hdu.edu.cn/showproblem.php?pid=6092 题意: 给出两个数组A和B,A数组一共可以有(1<<n)种不同的集合组合,B中则记录了每个数出 ...
- hdu 6092 Rikka with Subset(多重背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6092 #include <cstdio> #include <iostream> ...
- HDU 6092:Rikka with Subset(dp)
分析 很多个较小的数字可以随机组合成较大的数字,所以B数组从小到大开始遍历,除了空集,最小的那个存在的个数对应的数字必然是a数组中的数字. 每求出这一部分之后,更新后续的B序列. 分析完后,主要的难点 ...
随机推荐
- 在code first结构下的生成的数据迁移文件,upadate-database失败
程序控制台出现 already exist Table "xxx",是由于项目中的Migrations(迁移文件)与连接的mysql数据库里迁移记录表里的数量及名称不一致.
- Java重写与重载之间的区别
重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...
- linux:C语言通过ICMP协议判断局域网内部主机是否存活
ICMP协议 ICMP(Internet Control Message,网际控制报文协议)是为网关和目标主机而提供的一种差错控制机制,使它们在遇到差错时能把错误报告给报文源发方. ICMP协议是IP ...
- C++ unordered_map 在key为string类型和char*类型时测试时间性能差异
测试系统liunx centos6.5 代码如下 #include <string.h> #include <sstream> #include <list> #i ...
- C#-实验3
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- RubyMine 2016.1 下载 附注册激活码 破解版方法
注册破解方法: 在要求输入注册的界面选择激活码,然后粘贴以下注册码: 43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIj ...
- Java中IO流
* IO流用来处理设备之间的数据传输 * Java对数据的操作是通过流的方式 * Java用于操作流的类都在IO包中 * 流按流向分为两种:输入流,输出流. * 流按操作类型分为两种: * 字节流 : ...
- 初遇locust
大概有四个月没有用过PYTHON的我. 今天差点都不知道怎么运行了. 说起来真是丢人呐. 幸好还是存留着一点点印象,再加上看了一下以前写的几篇文章, 还是比较快的想起来了.不然真的是要崩溃了. 刚开始 ...
- ubuntu14通过trove/redstack安装openstack环境
---恢复内容开始--- Trove Installation Trove is constantly under development. The easiest way to install Tr ...
- Python学习笔记——基础篇【第六周】——PyYAML & configparser模块
PyYAML模块 Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 常用模块之Co ...