Problem G. Cyclic
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 159 Accepted Submission(s): 106
Problem Descriptio
Count the number of cyclic permutations of length n with no continuous subsequence [i, i + 1 mod n].
Output the answer modulo 998244353.
Input
The first line of the input contains an integer T , denoting the number of test cases.
In each test case, there is a single integer n in one line, denoting the length of cyclic permutations.
1 ≤ T ≤ 20, 1 ≤ n ≤ 100000
Output
For each test case, output one line contains a single integer, denoting the answer modulo 998244353.
Sample Input
3 4 5 6
Sample Output
1 8 36
给你一个n
1-n个数排列成环要求后一个不能刚好比前一个大一
问你有多少种排列方式
计算前几项 : 0,0,1,1,8,36 丢到oeis上面去。得到公式 f[i] = (i-1)f[i-2]+(i-1)f[i-2]-(-1)^i;
#include<bits/stdc++.h>
using namespace std;
long long t,n,f[100009],tp = -1;
const long long mod = 998244353;
void init(){
f[1] = 0;
f[2] = 0;
for(int i = 3;i < 100001;i++){
f[i] = ((((i - 2) * f[i - 1] % mod + (i - 1) * f[i - 2] % mod) % mod - tp) % mod + mod) % mod;
tp *= -1;
}
}
int main(){
scanf("%lld",&t);
init();
while(t--){
scanf("%lld",&n);
printf("%lld\n",f[n]);
}
}