题目链接
nnn fishermen have just returned from a fishing vacation. The iii-th fisherman has caught a fish of weight aia_iai​.

Fishermen are going to show off the fish they caught to each other. To do so, they firstly choose an order in which they show their fish (each fisherman shows his fish exactly once, so, formally, the order of showing fish is a permutation of integers from 111 to nnn). Then they show the fish they caught according to the chosen order. When a fisherman shows his fish, he might either become happy, become sad, or stay content.

Suppose a fisherman shows a fish of weight xxx, and the maximum weight of a previously shown fish is yyy (y=0y = 0y=0 if that fisherman is the first to show his fish). Then:

  • if x≥2yx \ge 2yx≥2y, the fisherman becomes happy;
  • if 2x≤y2x \le y2x≤y, the fisherman becomes sad;
  • if none of these two conditions is met, the fisherman stays content.

Let’s call an order in which the fishermen show their fish emotional if, after all fishermen show their fish according to this order, each fisherman becomes either happy or sad. Calculate the number of emotional orders modulo 998244353998244353998244353.

Input

The first line contains one integer nnn (2≤n≤50002 \le n \le 50002≤n≤5000).

The second line contains nnn integers a1a_1a1​, a2a_2a2​, …, ana_nan​ (1≤ai≤1091 \le a_i \le 10^91≤ai​≤109).

Output

Print one integer — the number of emotional orders, taken modulo 998244353998244353998244353.

Examples

input

4
1 1 4 9

output

20

input

4
4 3 2 1

output

0

input

3
4 2 1

output

6

input

8
42 1337 13 37 420 666 616 97

output

19200

最后序列一定是如下形式:
b1⋯⋯⋯⏟≤b12b2⋯⋯⋯⏟≤b22b3⋯⋯⋯⏟≤b32\boxed{b_1} \begin{matrix}\underbrace{\cdots\cdots\cdots}\\\leq \frac{b_1}{2}\end{matrix} \boxed{b_2} \begin{matrix}\underbrace{\cdots\cdots\cdots}\\\leq \frac{b_2}{2}\end{matrix} \boxed{b_3} \begin{matrix}\underbrace{\cdots\cdots\cdots}\\\leq \frac{b_3}{2}\end{matrix} b1​​⋯⋯⋯​≤2b1​​​b2​​⋯⋯⋯​≤2b2​​​b3​​⋯⋯⋯​≤2b3​​​
其中 bi≤bi+12b_i\le \frac{b_{i+1}}{2}bi​≤2bi+1​​ 。
首先将序列 aaa 按升序排序。
考虑逐渐将序列 aaa 中的元素填入长度为 nnn 的空序列。
设 f[i]f[i]f[i] 为 aia_iai​ 作为当前序列中最后一个 bbb 时的方案数,lim[i]lim[i]lim[i] 为满足 aj≤ai2a_j\le \frac{a_i}{2}aj​≤2ai​​ 的最大的 jjj 。
则前一个 bbb 是 aj(j≤lim[i])a_j \ (j\le lim[i])aj​ (j≤lim[i]) 。
对于固定的 jjj ,此时序列中已经填好了 aja_jaj​ , aia_iai​ 和 a1∼lim[j]a_{1\sim lim[j]}a1∼lim[j]​ ,其中 aia_iai​ 一定放在序列中第一个空余位置,此时序列中还有 n−lim[j]−2n-lim[j]-2n−lim[j]−2 个空余位置。
在将元素 aia_iai​ 加入序列的同时要将 alim[j]+1∼lim[i]a_{lim[j]+1\sim lim[i]}alim[j]+1∼lim[i]​ 加入序列,这些元素可以再空余位置中任意选择位置安放,因此共有f[j]⋅A(n−lim[j]−2,lim[i]−lim[j]−1)f[j]\cdot A(n-lim[j]-2,lim[i]-lim[j]-1)f[j]⋅A(n−lim[j]−2,lim[i]−lim[j]−1) 种情况。
因此转移方程为:
f[i]=∑j=0lim[i]f[j]⋅A(n−lim[j]−2,lim[i]−lim[j]−1)f[i]=\sum_{j=0}^{lim[i]}f[j]\cdot A(n-lim[j]-2,lim[i]-lim[j]-1) f[i]=j=0∑lim[i]​f[j]⋅A(n−lim[j]−2,lim[i]−lim[j]−1)
其中 f[0]=1,lim[0]=−1f[0]=1,lim[0]=-1f[0]=1,lim[0]=−1 。
因为最后 ana_nan​ 一定是最后一个 bbb ,如果 lim[n]≠n−1lim[n]\not =n-1lim[n]​=n−1 ,那么一定无解,否则答案为 f[n]f[n]f[n] 。
时间复杂度为 O(n2)O(n^2)O(n2) 。

#include<bits/stdc++.h>using namespace std;
const int N = 5005, MOD = 998244353;
int fac[N], inv[N];inline int qpow(int x, int n) {int res = 1;while (n) {if (n & 1) res = (1ll * res * x) % MOD;x = 1ll * x * x % MOD, n >>= 1;}return res;
}inline void A_init(const int n) {fac[0] = 1;for (int i = 1; i <= n; ++i)fac[i] = 1ll * fac[i - 1] * i % MOD;inv[n] = qpow(fac[n], MOD - 2);for (int i = n - 1; i >= 0; --i)inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
}inline int A(int n, int m) {if (n < 0 || m < 0 || n < m) return 0;return 1ll * fac[n] * inv[n - m] % MOD;
}int lim[N], a[N], f[N], n;int main() {scanf("%d", &n), A_init(n);for (int i = 1; i <= n; i++)scanf("%d", &a[i]);sort(a + 1, a + 1 + n);for (int i = 1, j = 0; i <= n; i++) {while (2 * a[j + 1] <= a[i])j++;lim[i] = j;}if (lim[n] != n - 1)return puts("0"), 0;f[0] = 1, lim[0] = -1;for (int i = 1; i <= n; i++)for (int j = 0; j <= lim[i]; j++)f[i] = (f[i] + 1ll * f[j] * A(n - 2 - lim[j], lim[i] - lim[j] - 1) % MOD) % MOD;printf("%d\n", f[n]);return 0;
}

另外转移方程中的排列式子可作如下转化:
A(n−lim[j]−2,lim[i]−lim[j]−1)=(n−lim[j]−2)!((n−lim[j]−2)−(lim[i]−lim[j]−1))!=(n−lim[j]−2)!(n−lim[i]−1)!\begin{aligned} A(n-lim[j]-2,lim[i]-lim[j]-1) &= \frac{(n-lim[j]-2)!}{((n-lim[j]-2)-(lim[i]-lim[j]-1))!} \\ &= \frac{(n-lim[j]-2)!}{(n-lim[i]-1)!} \end{aligned} A(n−lim[j]−2,lim[i]−lim[j]−1)​=((n−lim[j]−2)−(lim[i]−lim[j]−1))!(n−lim[j]−2)!​=(n−lim[i]−1)!(n−lim[j]−2)!​​
其中式子一部分只和 iii 有关,另一部分只和 jjj 有关,因此只需维护 f[j]⋅(n−lim[j]−2)!f[j]\cdot (n-lim[j]-2)!f[j]⋅(n−lim[j]−2)! 的前缀和即可将 dp 复杂度降为 O(n)O(n)O(n) 。

#include<bits/stdc++.h>using namespace std;
const int N = 5005, MOD = 998244353;
int fac[N], inv[N];inline int qpow(int x, int n) {int res = 1;while (n) {if (n & 1) res = (1ll * res * x) % MOD;x = 1ll * x * x % MOD, n >>= 1;}return res;
}inline void A_init(const int n) {fac[0] = 1;for (int i = 1; i <= n; ++i)fac[i] = 1ll * fac[i - 1] * i % MOD;inv[n] = qpow(fac[n], MOD - 2);for (int i = n - 1; i >= 0; --i)inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;
}int lim[N], sum[N], a[N], f, n;int main() {scanf("%d", &n), A_init(n);for (int i = 1; i <= n; i++)scanf("%d", &a[i]);sort(a + 1, a + 1 + n);for (int i = 1, j = 0; i <= n; i++) {while (2 * a[j + 1] <= a[i])j++;lim[i] = j;}if (lim[n] != n - 1)return puts("0"), 0;sum[0] = fac[n - 1];for (int i = 1; i <= n; i++) {f = 1ll * sum[lim[i]] * inv[n - lim[i] - 1] % MOD;sum[i] = (sum[i - 1] + 1ll * f * fac[n - lim[i] - 2] % MOD) % MOD;}printf("%d\n", f);return 0;
}

Educational Codeforces Round 97 (Rated for Div. 2) F. Emotional Fishermen相关推荐

  1. Educational Codeforces Round 73 (Rated for Div. 2) F. Choose a Square 线段树 + 二维转一维

    传送门 文章目录 题意: 思路: 题意: 给你nnn个点(xi,yi)(x_i,y_i)(xi​,yi​),每个点有个价值cic_ici​,现在你可以框一个正方形,要求左下角和右上角的坐标(x,y)( ...

  2. Educational Codeforces Round 76 (Rated for Div. 2) F. Make Them Similar 折半搜索

    传送门 文章目录 题意: 思路: 题意: 思路: 一个显然的思路就是2302^{30}230枚举所有的xxx,让后再检查,这个复杂度显然不能接受. 又发现对于每个位置它取多少不受其他位置限制,满足可拼 ...

  3. Educational Codeforces Round 81 (Rated for Div. 2) F.Good Contest \ 洛谷 划艇 组合 计数dp

    cf传送门 P3643 [APIO2016]划艇 文章目录 题意: 思路: 题意: aia_iai​在[li,ri][l_i,r_i][li​,ri​]等概率随机选一个数,求aaa数组不增的概率. 思 ...

  4. Educational Codeforces Round 101 (Rated for Div. 2) F. Power Sockets 哈希 + 乱搞

    传送门 题意: 给一个二进制串aaa,让后定义两个串相似为有至少一个相同位置相等.现在让你找一个字典序最小的长度为kkk的串使其与aaa中每个长度为kkk的字串相似. 思路: 首先我们知道所有可能的串 ...

  5. Educational Codeforces Round 87 (Rated for Div. 2) F. Summoning Minions

    题目链接 Polycarp plays a computer game. In this game, the players summon armies of magical minions, whi ...

  6. Educational Codeforces Round 41 (Rated for Div. 2) F. k-substrings

    F. k-substrings time limit per test 4 seconds memory limit per test 256 megabytes input standard inp ...

  7. Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)

    Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...

  8. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  9. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  10. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

最新文章

  1. android 键盘搜索按钮事件,Android EditText 软键盘搜索事件
  2. 特约专栏丨陈纯院士:大规模动态时序图实时处理技术、平台及应用
  3. python 调用linux命令-Python Linux系统管理之Python中执行外部命令
  4. linux最常用的目录表示,Linux磁盘分区的命名方式和常用目录?【每日一个知识点第84期-Linux】...
  5. sv队列和动态数组的区别_systemverilog学习(4)动态数组
  6. 电子计算机系统可以分为几类,电子计算机分为两大类.doc
  7. ELK 之二:ElasticSearch 和Logstash高级使用
  8. 【计算机组成原理】Chapter1-复习题
  9. openwrt 运行程序时提示缺少libstdc++.so.6
  10. 大数据计算服务MaxCompute行业开发应用案例
  11. 字符编码ASCII ,Unicode ,UTF-8
  12. linux6禁用屏幕保护程序,禁用屏幕保护程序(ScreenSaver Disabled)
  13. 能源行业的作业成本法管理
  14. 服务器安装字体库解决出现xx问题
  15. 运行vue项目遇到的bug
  16. 阿正入门深度学习---从EM算法开始
  17. 最新最简便解决 teamviewre检测为商业用途 的方法
  18. 百度地图路线规划(途经点)
  19. 程序验证(二):SAT问题
  20. Python快速编程入门 第2版 实训案例及课后编程题

热门文章

  1. python读txt文件-python读取文本文件数据
  2. Wireshark实验
  3. 权变理论计算机管理理论,权变理论是什么意思?权变管理理论的介绍与应用
  4. 2012年最受欢迎低价优质美国虚拟主机TOP5
  5. 申请企业邮箱有哪些好处?企业邮箱优势盘点
  6. Laravel执行seeder报错
  7. 程序语言翻译: 2.1在以阶段划分的编译器中,贯穿于编译器工作始终的是( )。2.2 对高级语言程序进行翻译时,源程序中的变量不可能映射到( )
  8. 《可复制的领导力》——樊登书摘
  9. Cocos2dx3.2学习准备(一):C++11新特性
  10. 股票历史数据-股票历史数据在线查询系统