Cyclic-杭电2018多校第十场(OEIS)

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

Source

2018 Multi-University Training Contest 10


题解:

一看就是推规律题,谁知道大家都OEIS,而我却是苦苦推公式呢。

首先讲一下这个题目的意思吧,它介绍了一个cyclic permutations。这个是圆周排列数。

比如长度为N:那么它的全排列为:(N-1)!

然后它想要你得到不存在  [ i , ( i+1 )%n  ]形式的。

一个都不能有,

比如说n=4,只有一组符合,那就是  4 3 2 1 。

首先,遇事不决先打表。

你可以根据你需要出现多少次[ i , ( i+1 )%n  ]来控制check()函数即可。

后来我认真推出了n=5时等于8,而n=6,ans=36,都能推,n=7,ans=229,也行。

当我推到第8组,我发现有问题了,答案虽然能推导出来,但不能系统归纳出来。


我们推导一下n=5来热身:

我们知道 n=5,就必须在n=4的基础上加一个元素。

然而,这个元素只要不违反我们所定义的前后相差1即可。

那么n=5怎么推出来的呢????

n=5:

n=4:   3  2  1  0

1、在合法的基础上插入即可。

  • 3  2  4  1  0
  • 3  2  1  0
  • 3  2  1  0  4

2、在一组不合法的情况下插入来隔开。

我们列出所有1组不合法:(利用我的代码一打表,只要控制check函数,tot的个数。即可得到)

  • 0  1  3  2
  • 0  2  1  3
  • 2  3  1
  • 0  3  1  2

大家认真观察第2组,虽然他在n=4,的确是 有一组不合法。

但是大家认真想一想,其实它到了n=5时,它却是合法的。

  1.          0  4  1  3  2
     
  2.          0  4  2  1  3
             0
      2  4  3
             0
      2  1  4  3
     
  3.           4  3  1
     
  4.          0  3  4  2

3、有两组不合法(如果有两组不合法,那么可以确定的是,出现[ n-1 ,0 ]):

其实是合法的,只要用N来隔开另一组不合法就行了。

但是n=4时不存在。。。。。


具体它是怎么推出来的。我是明确知道的。

但是要得到递推公式:只能靠OEIS

但是有一点我能保证且能解释清楚的。

 a[i]=(i-2)*a[i-1]+(i-1)*a[i-2+(i&1?1:-1);

(i-2)*a[i-1]:就是上面我的第一部分,

用上一个合法的序列,只有(i - 2 )个位置是能放的,(n-1,#),(#,0)这两个位置不能放。

然而我还没系统推出来,暂时还不知道。。。所以还要等dls讲解一波。

 OEIS:http://oeis.org/A000757

遇事不决就打表:

#include<bits/stdc++.h>
using namespace std;
const int N=20;
int a[N],n;
bool check(){
    int tot=0;
    for(int i=0;i<n;i++){
        if((a[(i)%n]+1)%n==a[(i+1)%n]){
            tot++;
            return false;
        }
    }
    return true;
}
void solve(){
    memset(a,127,sizeof(a));
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        a[i]=i;
    }
    int cnt=0;
    do{
        if(check()){
            for(int j=0;j<n;j++){
                printf("%d%c",a[j],j==n-1?'\n':' ');
            }
            cnt++;
        }
    }while(next_permutation(a,a+n));
    printf("%d\n",cnt/(n));
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        solve();
    }
}

 

 AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=100050;
ll a[N]={0};
void init(){
    a[1]=0;
    a[2]=0;
    a[3]=1;
    a[4]=1;
    a[5]=8;
    a[6]=36;
    for(int i=7;i<=N;i++){
        a[i]=((i-2)*a[i-1]%mod+(i-1)*a[i-2]%mod+(i&1?1:-1))%mod;
    }
}
void solve(){
    int n;
    scanf("%d",&n);
    printf("%lld\n",a[n]);
}
int main()
{
    init();
    int T;
    scanf("%d",&T);
    while(T--){
        solve();
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值