【Codeforces 1091D】New Year and the Permutation Concatenation

【链接】 我是链接,点我呀:)
【题意】


把1~n的n!种排列依次连接成一个长度为nn!的序列。
让你在这个序列当中找长度为n的连续段,使得连续段中的数字的和为n
(n-1)/2
输出符合要求的连续段的个数

【题解】


也就是让你找到长度为n的连续段,然后里面1~n各出现一次
考虑相邻的两个排列
设他们有长度为i的相同前缀
例如
xxabcxxcba
会发现我们用第二个排列的前i个数字与第一个排列的后n-i个数字能够组成一个符合要求的连续段。
即"abcxx"
为什么?
假设我们放一个长度为n的窗口在"xxabc"的位置
我们往右挪动了一下
少了一个最左边的x,右边会新出来一个数字,它要为什么才行呢?
因为总和不能变,所以肯定和刚才少掉的数字要一样。
那么显然就只有在他们俩有相同的前缀的时候才能办到这样的事情。
所以我们枚举前缀的长度为i
这样的排列有A(n,i)个
对于这A(n,i)种排列,他们的后n-i个位置还有(n-i)!种
我们先假设我们已经固定了前i个数字,也就是说固定了要讨论的前缀的样子x
那么前缀为x的排列一共有(n-i)!种
显然这(n-i)!种肯定是连接在一起的(因为是按照字典序连接的)
假设(n-i)!等于3
那么就对应了
xabcxbacxcab
(只是举例)
那么就能够组成
"abcx","bacx"这两个符合要求的
也即在(n-i)!-1个交界处产生答案
记住我们是如何找答案的:
后一个排列的前i个数字与前一个排列的后n-i个数字组成一个符合要求的连续段。
(前一个排列的n-i个数字,在前缀x相同的情况下,都是不一样的,所以不会重复计数
(而我们又覆盖到了所有不同的前缀i,因此也没有重复计数
而前缀x有A(n,i)种
那么总共就有A(n,i)*((n-1)!-1)种连续段符合要求

【代码】

import java.io.*;
import java.util.*;

public class Main {
    
    
    static InputReader in;
    static PrintWriter out;
        
    public static void main(String[] args) throws IOException{
        //InputStream ins = new FileInputStream("E:\\rush.txt");
        InputStream ins = System.in;
        in = new InputReader(ins);
        out = new PrintWriter(System.out);
        //code start from here
        new Task().solve(in, out);
        out.close();
    }
    
    static int N = (int)1e6;
    static class Task{
        
        /*
         * i = 1~n-1
         * A(n,i)*( (n-i)! - 1);
         * n!/(n-i)!
         */
        int n;
        long fac[] = new long[N+10];
        long rfac[] = new long[N+10];
        long MOD = (int)998244353;
        
        long _pow(long x,long y) {
            long ans = 1;
            while (y>0) {
                if (y%2==1) {
                    ans = (ans * x)%MOD;
                }
                x = x*x%MOD;
                y = y/2;
            }
            return ans;
        }
        
        public void solve(InputReader in,PrintWriter out) {
            n = in.nextInt();
            fac[0] = 1;
            for (int i = 1;i <= N;i++)
                fac[i] = (1l*fac[i-1]*i)%MOD;
            rfac[N] = _pow(fac[N],MOD-2);
            for (int i = N-1;i>=0;i--)
                rfac[i] = 1l*rfac[i+1]*(i+1)%MOD;
            long ans = 0;
            for (int i = 1;i <= n-1;i++) {
                long temp = fac[n]*rfac[n-i]%MOD*(fac[n-i]-1)%MOD;
                if (temp<0) temp+=MOD;
                ans = (ans + temp)%MOD;
            }
            ans = (ans + fac[n])%MOD;
            out.println(ans);
        }
    }

    

    static class InputReader{
        public BufferedReader br;
        public StringTokenizer tokenizer;
        
        public InputReader(InputStream ins) {
            br = new BufferedReader(new InputStreamReader(ins));
            tokenizer = null;
        }
        
        public String next(){
            while (tokenizer==null || !tokenizer.hasMoreTokens()) {
                try {
                tokenizer = new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}

转载于:https://www.cnblogs.com/AWCXV/p/10578988.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值