luogu P5606 小 K 与毕业旅行 - 构造 - 多项式
题目传送门
先考虑 $a_i > 0$ 的情况。考虑构造这样一个顺序:$a_i$ 要么和后面的数的乘积都大于 $w$ 要么都小于等于 $w$。
这个构造可以这样做:
vector<int> b {0};
sort(a.begin(), a.end());
int l = 0, r = (signed) a.size() - 1;
while (l <= r) {
if (1ll * a[l] * a[r] > w) {
b.push_back(b.back() - 1);
r--;
} else {
b.push_back(b.back() + 1);
l++;
}
}
b.pop_back();
这个可以考虑根号分治,反复尝试 4 种枚举顺序可以发现。
那么按顺序枚举每个 $a_i$,我们知道它可以插入的位置的数量。如果它和后面的数的乘积都大于 $w$,那么可行位置减 1,否则加 1.
考虑没有这个限制条件怎么做,考虑正负分开,计算一下段数,最后再合并。
枚举一下初始可行段数量,用分治 NTT 求出方案数关于初始可行段数量多项式,然后多点求值,然后一遍卷积做一下二项式反演。
时间复杂度 $O(n\log^2 n)$。
下面是验题的时候写的代码。
Code
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; #define ll long long template <typename T>
void pfill(T* pst, const T* ped, T val) {
for ( ; pst != ped; *(pst++) = val);
} template <typename T>
void pcopy(T* pst, const T* ped, T* pval) {
for ( ; pst != ped; *(pst++) = *(pval++));
} const int N = 262144;
const int Mod = 998244353;
const int bzmax = 19;
const int g = 3; void exgcd(int a, int b, int& x, int& y) {
if (!b) {
x = 1, y = 0;
} else {
exgcd(b, a % b, y, x);
y -= (a / b) * x;
}
} int inv(int a, int Mod) {
int x, y;
exgcd(a, Mod, x, y);
return (x < 0) ? (x + Mod) : (x);
} template <const int Mod = :: Mod>
class Z {
public:
int v; Z() : v(0) { }
Z(int x) : v(x){ }
Z(ll x) : v(x % Mod) { } friend Z operator + (const Z& a, const Z& b) {
int x;
return Z(((x = a.v + b.v) >= Mod) ? (x - Mod) : (x));
}
friend Z operator - (const Z& a, const Z& b) {
int x;
return Z(((x = a.v - b.v) < 0) ? (x + Mod) : (x));
}
friend Z operator * (const Z& a, const Z& b) {
return Z(a.v * 1ll * b.v);
}
friend Z operator ~ (const Z& a) {
return inv(a.v, Mod);
}
friend Z operator - (const Z& a) {
return Z(0) - a;
}
Z& operator += (Z b) {
return *this = *this + b;
}
Z& operator -= (Z b) {
return *this = *this - b;
}
Z& operator *= (Z b) {
return *this = *this * b;
}
friend boolean operator == (const Z& a, const Z& b) {
return a.v == b.v;
}
}; typedef Z<> Zi; Zi qpow(Zi a, int p) {
if (p < Mod - 1)
p += Mod - 1;
Zi rt = 1, pa = a;
for ( ; p; p >>= 1, pa = pa * pa) {
if (p & 1) {
rt = rt * pa;
}
}
return rt;
} const Zi inv2 ((Mod + 1) >> 1); class NTT {
private:
Zi gn[bzmax + 4], _gn[bzmax + 4];
public: NTT() {
for (int i = 0; i <= bzmax; i++) {
gn[i] = qpow(Zi(g), (Mod - 1) >> i);
_gn[i] = qpow(Zi(g), -((Mod - 1) >> i));
}
} void operator () (Zi* f, int len, int sgn) {
for (int i = 1, j = len >> 1, k; i < len - 1; i++, j += k) {
if (i < j)
swap(f[i], f[j]);
for (k = len >> 1; k <= j; j -= k, k >>= 1);
} Zi *wn = (sgn > 0) ? (gn + 1) : (_gn + 1), w, a, b;
for (int l = 2, hl; l <= len; l <<= 1, wn++) {
hl = l >> 1, w = 1;
for (int i = 0; i < len; i += l, w = 1) {
for (int j = 0; j < hl; j++, w *= *wn) {
a = f[i + j], b = f[i + j + hl] * w;
f[i + j] = a + b;
f[i + j + hl] = a - b;
}
}
} if (sgn < 0) {
Zi invlen = ~Zi(len);
for (int i = 0; i < len; i++) {
f[i] *= invlen;
}
}
} int correct_len(int len) {
int m = 1;
for ( ; m <= len; m <<= 1);
return m;
}
} NTT; void pol_inverse(Zi* f, Zi* g, int n) {
static Zi A[N];
if (n == 1) {
g[0] = ~f[0];
} else {
int hn = (n + 1) >> 1, t = NTT.correct_len(n << 1 | 1);
pol_inverse(f, g, hn); pcopy(A, A + n, f);
pfill(A + n, A + t, Zi(0));
pfill(g + hn, g + t, Zi(0));
NTT(A, t, 1);
NTT(g, t, 1);
for (int i = 0; i < t; i++) {
g[i] = g[i] * (Zi(2) - g[i] * A[i]);
}
NTT(g, t, -1);
pfill(g + n, g + t, Zi(0));
}
} void pol_sqrt(Zi* f, Zi* g, int n) {
static Zi A[N], B[N];
if (n == 1) {
g[0] = f[0];
} else {
int hn = (n + 1) >> 1, t = NTT.correct_len(n + n); pol_sqrt(f, g, hn); pfill(g + hn, g + n, Zi(0));
for (int i = 0; i < hn; i++)
A[i] = g[i] + g[i];
pfill(A + hn, A + t, Zi(0));
pol_inverse(A, B, n);
pcopy(A, A + n, f);
pfill(A + n, A + t, Zi(0));
NTT(A, t, 1);
NTT(B, t, 1);
for (int i = 0; i < t; i++)
A[i] *= B[i];
NTT(A, t, -1);
for (int i = 0; i < n; i++)
g[i] = g[i] * inv2 + A[i];
}
} typedef class Poly : public vector<Zi> {
public:
using vector<Zi>::vector; Poly& fix(int sz) {
resize(sz);
return *this;
}
} Poly; Poly operator + (Poly A, Poly B) {
int n = A.size(), m = B.size();
int t = max(n, m);
A.resize(t), B.resize(t);
for (int i = 0; i < t; i++) {
A[i] += B[i];
}
return A;
} Poly operator - (Poly A, Poly B) {
int n = A.size(), m = B.size();
int t = max(n, m);
A.resize(t), B.resize(t);
for (int i = 0; i < t; i++) {
A[i] -= B[i];
}
return A;
} Poly sqrt(Poly a) {
Poly rt (a.size());
pol_sqrt(a.data(), rt.data(), a.size());
return rt;
} Poly operator * (Poly A, Poly B) {
int n = A.size(), m = B.size();
int k = NTT.correct_len(n + m - 1);
if (n < 20 || m < 20) {
Poly rt (n + m - 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
rt[i + j] += A[i] * B[j];
}
}
return rt;
}
A.resize(k), B.resize(k);
NTT(A.data(), k, 1);
NTT(B.data(), k, 1);
for (int i = 0; i < k; i++) {
A[i] *= B[i];
}
NTT(A.data(), k, -1);
A.resize(n + m - 1);
return A;
} Poly operator ~ (Poly f) {
int n = f.size(), t = NTT.correct_len((n << 1) | 1);
Poly rt (t);
f.resize(t);
pol_inverse(f.data(), rt.data(), n);
rt.resize(n);
return rt;
} Poly operator / (Poly A, Poly B) {
int n = A.size(), m = B.size();
if (n < m) {
return Poly {0};
}
int r = n - m + 1;
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
A.resize(r), B.resize(r);
A = A * ~B;
A.resize(r);
reverse(A.begin(), A.end());
return A;
} Poly operator % (Poly A, Poly B) {
int n = A.size(), m = B.size();
if (n < m) {
return A;
}
if (m == 1) {
return Poly {0};
}
A = A - A / B * B;
A.resize(m - 1);
return A;
} Zi Inv[N];
void init_inv(int n) {
Inv[0] = 0, Inv[1] = 1;
for (int i = 2; i <= n; i++) {
Inv[i] = Inv[Mod % i] * Zi((Mod - (Mod / i)));
}
} void diff(Poly& f) {
if (f.size() == 1) {
f[0] = 0;
return;
}
for (int i = 1; i < (signed) f.size(); i++) {
f[i - 1] = f[i] * Zi(i);
}
f.resize(f.size() - 1);
}
void integ(Poly& f) {
f.resize(f.size() + 1);
for (int i = (signed) f.size() - 1; i; i--) {
f[i] = f[i - 1] * Inv[i];
}
f[0] = 0;
} Poly ln(Poly f) {
int n = f.size();
Poly h = f;
diff(h);
f = h * ~f;
f.resize(n - 1);
integ(f);
return f;
} void pol_exp(Poly& f, Poly& g, int n) {
Poly h;
if (n == 1) {
g.resize(1);
g[0] = 1;
} else {
int hn = (n + 1) >> 1;
pol_exp(f, g, hn); h.resize(n), g.resize(n);
pcopy(h.data(), h.data() + n, f.data()); g = g * (Poly{1} - ln(g) + h);
g.resize(n);
}
} Poly exp(Poly f) {
int n = f.size();
Poly rt;
pol_exp(f, rt, n);
return rt;
} class PolyBuilder {
protected:
int num;
Poly P[N << 1]; void _init(int *x, int l, int r) {
if (l == r) {
P[num++] = Poly{-Zi(x[l]), Zi(1)};
return;
}
int mid = (l + r) >> 1;
int curid = num++;
_init(x, l, mid);
int rid = num;
_init(x, mid + 1, r);
P[curid] = P[curid + 1] * P[rid];
} void _evalute(Poly f, Zi* y, int l, int r) {
f = f % P[num++];
if (l == r) {
y[l] = f[0];
return;
}
int mid = (l + r) >> 1;
_evalute(f, y, l, mid);
_evalute(f, y, mid + 1, r);
}
public:
Poly evalute(Poly f, int* x, int n) {
Poly rt(n);
num = 0;
_init(x, 0, n - 1);
num = 0;
_evalute(f, rt.data(), 0, n - 1);
return rt;
}
} PolyBuilder; ostream& operator << (ostream& os, Poly& f) {
for (auto x : f)
os << x.v << ' ';
os << '\n';
return os;
} Zi fac[N], _fac[N];
void init_fac(int n) {
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i;
}
_fac[n] = ~fac[n];
for (int i = n; i; i--) {
_fac[i - 1] = _fac[i] * i;
}
} int w;
Poly dividing(int* a, int l, int r) {
if (l == r)
return Poly {a[l], 1};
int mid = (l + r) >> 1;
return dividing(a, l, mid) * dividing(a, mid + 1, r);
}
int xs[N];
Poly work(vector<int> a, int maxseg) {
if (!a.size()) {
Poly rt (maxseg, Zi(0));
rt[0] = 1;
return rt;
}
for (auto& x : a)
(x < 0) && (x = -x);
vector<int> b {0};
sort(a.begin(), a.end());
int l = 0, r = (signed) a.size() - 1;
while (l <= r) {
if (1ll * a[l] * a[r] > w) {
b.push_back(b.back() - 1);
r--;
} else {
b.push_back(b.back() + 1);
l++;
}
}
b.pop_back();
for (auto& x : b)
(x < 0) && (x += Mod);
Poly f = dividing(b.data(), 0, (signed) b.size() - 1);
f = PolyBuilder.evalute(f, xs, maxseg);
for (int i = 0; i < (signed) f.size(); i++) {
f[i] *= _fac[i];
}
Poly g (f.size());
for (int i = 0; i < (signed) g.size(); i++) {
g[i] = _fac[i];
(i & 1) && (g[i] = -g[i], 0);
}
f = (f * g).fix(maxseg);
for (int i = 0; i < (signed) f.size(); i++)
f[i] *= fac[i];
return f;
} int n;
int a[N];
int main() {
scanf("%d%d", &n, &w);
vector<int> A, B;
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
if (a[i] < 0) {
A.push_back(a[i]);
} else {
B.push_back(a[i]);
}
}
init_fac(n + 3);
int maxseg = min(A.size(), B.size()) + 3;
for (int i = 1; i < maxseg; i++)
xs[i] = i;
Poly f = work(A, maxseg);
Poly g = work(B, maxseg);
Zi ans = 0;
for (int i = 0; i < maxseg; i++) {
ans += f[i] * g[i] * 2;
if (i)
ans += f[i] * g[i - 1];
if (i < maxseg - 1)
ans += f[i] * g[i + 1];
}
printf("%d\n", ans.v);
return 0;
}
luogu P5606 小 K 与毕业旅行 - 构造 - 多项式的更多相关文章
- [Luogu] P1993 小K的农场
题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b至少多种植了c个单位的作 ...
- Luogu P1993 小 K 的农场
其实很早以前就打好了,但一直忘记写了. 也就是差分约束的模板题. 关于差分约束,也就是用来求关于一些不等式互相约束算出最优解. 推荐一个讲的很好的博客:http://www.cppblog.com/m ...
- 【luogu P1993 小K的农场】 题解
题目链接:https://www.luogu.org/problemnew/show/P1993 1.差分约束: 对于a - b <= c 有一条 b-->a 权值为c 对于a - b & ...
- luogu 1993 小K的农场
差分约束+spfa判负环 dfs判负环 #include<bits/stdc++.h> #define rep(i,x,y) for(register int i=x;i<=y;i+ ...
- 『题解』洛谷P1993 小K的农场
更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...
- Luogu 1081 【NOIP2012】开车旅行 (链表,倍增)
Luogu 1081 [NOIP2012]开车旅行 (链表,倍增) Description 小A 和小B决定利用假期外出旅行,他们将想去的城市从1到N 编号,且编号较小的城市在编号较大的城市的西边,已 ...
- BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)
蛋蛋安利的双倍经验题 1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1901 Solved: 951 ...
- BZOJ2718: [Violet 4]毕业旅行
2718: [Violet 4]毕业旅行 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 229 Solved: 126[Submit][Status ...
- 洛谷P1993 小K的农场 [差分约束系统]
题目传送门 小K的农场 题目描述 小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述: 农场a比农场b ...
随机推荐
- Ubuntu 16.04上源码编译和安装pytorch教程,并编写C++ Demo CMakeLists.txt | tutorial to compile and use pytorch on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/54e7a3d8/,欢迎阅读最新内容! tutorial to compile and use pytorch on ubuntu ...
- Express 框架以及与http-proxy-middleware整合实现代理
1.Express的简单使用 1.简介 Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具. 使用 Exp ...
- [Flutter] 转一个Flutter学习思维导图
本文的思维导图均转自QQ群,感谢原作者(是谁?) 表单 按钮 视图 Sliver 路由 (Routes) 输入控件 对话框 MDC (Material Design Component) 状态管理 R ...
- 如何取消 SqlDataAdapter.Fill() 的执行(转载)
问 Scenario: We have a DataGridView which is attached to DataAdapter (datatable), we load the data in ...
- Java自学-I/O 对象流
Java 对象流 ObjectInputStream,ObjectOutputStream 对象流指的是可以直接把一个对象以流的形式传输给其他的介质,比如硬盘 一个对象以流的形式进行传输,叫做序列化. ...
- 前端开发HTML5——表单标签
表单简介 Form表单主要用于用户与Web应用程序进行数据的交互,它允许用户将数据发给web应用程序,网页也可以拦截数据的发送以便自己使用.form通常由一到多个表单元素组成,这些表单元素是单行/多行 ...
- postgresql 相关函数总结
1.获取当前日期的年份 select to_char(t.detect_date,'YYYY') select extract(year from now())为double precision 格式 ...
- maven 学习---用Eclipse创建一个Maven Web项目
下面是使用 Eclipse 来创建一个Maven Web项目的说明.这是相当简单的. 现在让我们开始吧! 1: 启动 Eclipse, 点击 File->New->Other 2: 在弹出 ...
- Java 打印HelloKitty
Java第一课 如何在控制台打印出"Hello Kitty" 如图所示,在IDE中使用 System.out.println(); 语句来实现打印 最后附上AIDE下载链接: Ja ...
- 详解JavaScript的任务、微任务、队列以及代码执行顺序
摘要: 理解JS的执行顺序. 作者:前端小智 原文:详解JavaScript的任务.微任务.队列以及代码执行顺序 思考下面 JavaScript 代码: console.log("scrip ...