【公式题+打表】hdu 2018 多校第十场 1007 Problem G. Cyclic hdu 6432

Problem G. Cyclic 

Problem Description

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个数排列成环要求后一个不能刚好比前一个大一

问你有多少种排列方式

公式题,打表后在oeis找到a(n)=(-1)^n+((-1)^k*C(n,k)*(n-k-1)!)(k属于0-n-1的求和)

 

#include<bits/stdc++.h>
using namespace std;
#define cherry main
#define ll long long
const ll mod=998244353;
const int maxn=1e5+10;
ll fac[maxn],inv[maxn];
ll pow1(ll a,ll b)
{
    ll r=1;
    while(b)
    {
        if(b&1) r=r*a%mod;
        a=a*a%mod;
        b/=2;
    }
    return r;
}
void init()
{
    fac[0]=1;
    for(int i=1;i<maxn;i++) fac[i]=fac[i-1]*i%mod;
    inv[maxn-1]=pow1(fac[maxn-1],mod-2);
    for(int i=maxn-2;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
}
ll C(int n,int m)
{
    return fac[n]*inv[m]%mod*inv[n-m]%mod;
}

int main()
{
    int T;
    scanf("%d",&T);
    init();
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ll ans=0;
        if(n%2) ans=-1;
        else ans=1;
        for(int i=0;i<=n-1;i++)
        {
            ll p=(C(n,i)*fac[n-i-1])%mod;
            if(i%2) ans=(ans-p+mod)%mod;
            else ans=(ans+p+mod)%mod;
        }
        printf("%lld\n",(ans+mod)%mod);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值