【公式题+打表】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
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=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;
}

 

你可能感兴趣的:(【公式题+打表】hdu 2018 多校第十场 1007 Problem G. Cyclic hdu 6432)