BZOJ3771 Triple 【NTT + 容斥】
题目链接
题解
做水题放松一下
先构造\(A_i\)为\(x\)指数的生成函数\(A(x)\)
再构造\(2A_i\)为指数的生成函数\(B(x)\)
再构造\(3A_i\)为指数的生成函数\(C(x)\)
那么只需计算
\]
那么\(x^i\)的系数即为损失价值\(i\)的方案数
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 800005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
const LL P = 2281701377ll,G = 3;
inline LL qpow(LL a,LL b){
LL re = 1;
for (; b; b >>= 1,a = a * a % P)
if (b & 1) re = re * a % P;
return re;
}
int R[maxn];
inline void NTT(LL* a,int n,int f){
for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
for (int i = 1; i < n; i <<= 1){
LL gn = qpow(G,(P - 1) / (i << 1));
for (int j = 0; j < n; j += (i << 1)){
LL g = 1,x,y;
for (int k = 0; k < i; k++,g = g * gn % P){
x = a[j + k],y = g * a[j + k + i] % P;
a[j + k] = (x + y) % P,a[j + k + i] = ((x - y) % P + P) % P;
}
}
}
if (f == 1) return;
LL nv = qpow(n,P - 2); reverse(a + 1,a + n);
for (int i = 0; i < n; i++) a[i] = a[i] * nv % P;
}
LL A[maxn],B[maxn],C[maxn],D[maxn],ans[maxn],val[maxn],N,deg;
int main(){
N = read();
REP(i,N){
val[i] = read(),ans[val[i]]++;
A[val[i]] = 1; deg = max(deg,val[i]);
}
//2
int n = 1,m = deg << 1,L = 0;
while (n <= m) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
NTT(A,n,1);
for (int i = 0; i < n; i++) A[i] = A[i] * A[i] % P;
NTT(A,n,-1);
REP(i,N) A[val[i] << 1]--;
for (int i = 0; i < n; i++) ans[i] += A[i] >> 1;
//3
REP(i,N) B[val[i]] = 1;
n = 1,m = deg * 3,L = 0;
while (n <= m) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
NTT(B,n,1);
for (int i = 0; i < n; i++) B[i] = B[i] * B[i] % P * B[i] % P;
NTT(B,n,-1);
//2 + 1
REP(i,N) C[val[i] + val[i]] = 1,D[val[i]] = 1;
int nn = 1; L = 0,m = deg * 3;
while (nn <= m) nn <<= 1,L++;
for (int i = 1; i < nn; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
NTT(C,nn,1); NTT(D,nn,1);
for (int i = 0; i < nn; i++) C[i] = C[i] * D[i] % P;
NTT(C,nn,-1);
REP(i,N) C[val[i] * 3]--;
for (int i = 0; i < nn; i++) C[i] *= 3;
for (int i = 0; i < n; i++) B[i] -= C[i],B[i] /= 6,ans[i] += B[i];
for (int i = 0; i <= deg * 3; i++)
if (ans[i]) printf("%d %lld\n",i,ans[i]);
return 0;
}
BZOJ3771 Triple 【NTT + 容斥】的更多相关文章
- bzoj3771: Triple(容斥+生成函数+FFT)
传送门 咳咳忘了容斥了-- 设\(A(x)\)为斧头的生成函数,其中第\(x^i\)项的系数为价值为\(i\)的斧头个数,那么\(A(x)+A^2(x)+A^3(x)\)就是答案(于是信心满满的打了一 ...
- 【题解】[HAOI2018]染色(NTT+容斥/二项式反演)
[题解][HAOI2018]染色(NTT+容斥/二项式反演) 可以直接写出式子: \[ f(x)={m \choose x}n!{(\dfrac 1 {(Sx)!})}^x(m-x)^{n-Sx}\d ...
- 【BZOJ 3771】 3771: Triple (FFT+容斥)
3771: Triple Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 547 Solved: 307 Description 我们讲一个悲伤的故事. ...
- Codeforces 1553I - Stairs(分治 NTT+容斥)
Codeforces 题面传送门 & 洛谷题面传送门 u1s1 感觉这道题放到 D1+D2 里作为 5250 分的 I 有点偏简单了吧 首先一件非常显然的事情是,如果我们已知了排列对应的阶梯序 ...
- BZOJ 3771: Triple(FFT+容斥)
题面 Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: "这把斧头,是不是你的?" 樵夫一看:&qu ...
- 51nod 1514 美妙的序列 分治NTT + 容斥
Code: #include<bits/stdc++.h> #define ll long long #define mod 998244353 #define maxn 400000 # ...
- 洛谷P5206 [WC2019]数树 [容斥,DP,生成函数,NTT]
传送门 Orz神仙题,让我长了许多见识. 长式子警告 思路 y=1 由于y=1时会导致后面一些式子未定义,先抓出来. printf("%lld",opt==0?1:(opt==1? ...
- HAOI 2018 染色(容斥+NTT)
题意 https://loj.ac/problem/2527 思路 设 \(f(k)\) 为强制选择 \(k\) 个颜色出现 \(s\) 种,其余任取的方案数. 则有 \[ f(k)={m\choos ...
- LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)
题意 LOJ #2541. 「PKUWC 2018」猎人杀 题解 一道及其巧妙的题 , 参考了一下这位大佬的博客 ... 令 \(\displaystyle A = \sum_{i=1}^{n} w_ ...
随机推荐
- 【MySQL函数】日期篇
1.date_format()函数 date_format(createtime,'%Y') 年 date_format(createtime,'%Y-%m') 年月 date_format(crea ...
- Python中print函数中中逗号和加号的区别
strip()方法,去除字符串开头或者结尾的空格 s = " a b c " new_s = s.strip() print("-------->%s<--- ...
- 第一篇 数据库MySql
数据库的简介 数据库:存储数据的仓库 数据库管理系统软件 常见的数据库管理软件:甲骨文的oracle,IBM的db2,sql server, Access,Mysql(开源,免费,跨平台). 关系型数 ...
- v-model 双向数据绑定
通过v-model指令可以实现双向数据绑定 HTML部分: <div id="app"> <input type="text" v-model ...
- linux上open-vswitch安装和卸载
一. ovs 从源码编译安装: 安装依赖项: # apt-get install make # apt-get install gcc # apt-get install build-essentia ...
- LibLas学习笔记
LibLas学习笔记 las 什么是Las格式 LAS文件格式是数据用户之间交换三维点云数据的公共文件格式. 虽然这种格式主要用于交换激光雷达点云数据,但是它支持交换任何三维的x.y.z 数组. 这 ...
- activemq 持久化
转自: http://blog.csdn.net/kobejayandy/article/details/50736479 消息持久性的原理很简单,就是在发送者将消息发送出去后,消息中心首先将消息存储 ...
- [Ubuntu] sogou中文输入法安装
I install sogou 中文输入法 successfully, after following below steps: 1. install sogou pingyin by deb pac ...
- [leetcode-914-X of a Kind in a Deck of Cards]
In a deck of cards, each card has an integer written on it. Return true if and only if you can choos ...
- js 插件 issue
1 iscroll 5 和 lazyload 同时使用 转自 yinjie //lazyload var $scrollEle = $("#wrapper") $("i ...