A15286 | Almost Perfect
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
A permutation $p$ of length $n$ is called almost perfect if for all integer $1 \leq i \leq n$ , it holds that $\lvert p_i - p^{-1}_i \rvert \le 1$ , where $p^{-1}$ is the inverse permutation of $p$ (i.e. $p^{-1}_{k_1} = k_2$ if and only if $p_{k_2} = k_1$ ).
Count the number of almost perfect permutations of length $n$ modulo $998244353$ .
Count the number of almost perfect permutations of length $n$ modulo $998244353$ .
输入格式
The first line contains a single integer $t$ ( $1 \leq t \leq 1000$ ) — the number of test cases. The description of each test case follows.
The first and only line of each test case contains a single integer $n$ ( $1 \leq n \leq 3 \cdot 10^5$ ) — the length of the permutation.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$ .
The first and only line of each test case contains a single integer $n$ ( $1 \leq n \leq 3 \cdot 10^5$ ) — the length of the permutation.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$ .
输出格式
For each test case, output a single integer — the number of almost perfect permutations of length $n$ modulo $998244353$ .
输入输出样例
输入 #1
3 2 3 50
输出 #1
2 4 830690567
For $n = 2$ , both permutations $[1, 2]$ , and $[2, 1]$ are almost perfect.
For $n = 3$ , there are only $6$ permutations. Having a look at all of them gives us:
- $[1, 2, 3]$ is an almost perfect permutation.
- $[1, 3, 2]$ is an almost perfect permutation.
- $[2, 1, 3]$ is an almost perfect permutation.
- $[2, 3, 1]$ is NOT an almost perfect permutation ( $\lvert p_2 - p^{-1}_2 \rvert = \lvert 3 - 1 \rvert = 2$ ).
- $[3, 1, 2]$ is NOT an almost perfect permutation ( $\lvert p_2 - p^{-1}_2 \rvert = \lvert 1 - 3 \rvert = 2$ ).
- $[3, 2, 1]$ is an almost perfect permutation.
So we get $4$ almost perfect permutations.
For $n = 3$ , there are only $6$ permutations. Having a look at all of them gives us:
- $[1, 2, 3]$ is an almost perfect permutation.
- $[1, 3, 2]$ is an almost perfect permutation.
- $[2, 1, 3]$ is an almost perfect permutation.
- $[2, 3, 1]$ is NOT an almost perfect permutation ( $\lvert p_2 - p^{-1}_2 \rvert = \lvert 3 - 1 \rvert = 2$ ).
- $[3, 1, 2]$ is NOT an almost perfect permutation ( $\lvert p_2 - p^{-1}_2 \rvert = \lvert 1 - 3 \rvert = 2$ ).
- $[3, 2, 1]$ is an almost perfect permutation.
So we get $4$ almost perfect permutations.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted