hdu 6432

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]);
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值