zoukankan      html  css  js  c++  java
  • 2018沈阳区域赛现场赛 Gym

    2018沈阳区域赛现场赛  Gym - 101955C C - Insertion Sort 

    https://cn.vjudge.net/problem/Gym-101955C

    Time limit6000 ms

    Memory limit1048576 kB

    OSWindows

    Source2018-2019 ACM-ICPC, Asia Shenyang Regional Contest

    EditorialAnnouncement (en)Statements (en)

    Insertion sort is a simple sorting algorithm that builds the final sorted array one item at an iteration.

    More precisely, insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

    This type of sorting is typically done in-place, by iterating up the array, growing the sorted array behind it. At each array-position, it checks the value there against the largest value in the sorted array (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element in place and moves to the next. If smaller, it finds the correct position within the sorted array, shifts all the larger values up to make a space, and inserts into that correct position.

    The resulting array after kk iterations has the property where the first kkentries are sorted. In each iteration the first remaining entry of the input is removed, and inserted into the result at the correct position, thus extending the result.

    Knuth is an ACM-ICPC master and provides a modified pseudocode implementation about the insertion sort for you. His modified algorithm for an array of sortable items AA (11-based array) can be expressed as:

    He notes that a permutation of 11 to nn is almost sorted if the length of its longest increasing subsequence is at least (n1)(n−1).

    Given the parameter kk, you are asked to count the number of distinct permutations of 11 to nn meeting the condition that, after his modified insertion sort, each permutation would become an almost sorted permutation.

    Input

    The input contains several test cases, and the first line contains a positive integer TT indicating the number of test cases which is up to 50005000.

    For each test case, the only line contains three integers n,kn,k and qq indicating the length of the permutations, the parameter in his implementation and a prime number required for the output respectively, where 1n,k501≤n,k≤50 and 108q109108≤q≤109.

    Output

    For each test case, output a line containing "Case #x: y" (without quotes), where x is the test case number starting from 11, and y is the remainder of the number of permutations which meet the requirement divided by qq.

    Example

    Input
    4
    4 1 998244353
    4 2 998244353
    4 3 998244353
    4 4 998244353
    Output
    Case #1: 10
    Case #2: 14
    Case #3: 24
    Case #4: 24

    Note

    In the first sample case, we can discover 1010 permutations which meet the condition, and they are listed as follows:

    • [1,2,3,4][1,2,3,4];
    • [1,2,4,3][1,2,4,3];
    • [1,3,2,4][1,3,2,4];
    • [1,3,4,2][1,3,4,2];
    • [1,4,2,3][1,4,2,3];
    • [2,1,3,4][2,1,3,4];
    • [2,3,1,4][2,3,1,4];
    • [2,3,4,1][2,3,4,1];
    • [3,1,2,4][3,1,2,4];
    • [4,1,2,3][4,1,2,3].

    分析

      题目的意思是输入n、k、p,p作为取余的数据,把n作为全排列,k作为遍历的次数,每遍历一次就进行一次(不完整)冒泡排序,问有多少种排序完之后情况最长上升子序列的长度大于n-1(假装描述清楚了题意)。

      这场比赛时作为团队赛完成的,A完水题后将这题的暴力情况打了出来,然后觉得没啥想法,就吃饭+看小说+打守望了(逃)打着打着守望觉得我可以通过暴力打表找规律,然后,,,,居然找到规律了,,,,,具体情况已经在队内进行讲解,打表算差即可,在此放上大佬退出的公式:k!*((n-1)*(n-k)+1);

    AC代码:

     1 import java.math.BigInteger;
     2 import java.util.Scanner;
     3 
     4 public class Main {
     5 
     6     public static void main(String[] args) {
     7         Scanner cin = new Scanner(System.in);
     8         BigInteger a[][] = new BigInteger[60][60];
     9         BigInteger jiecheng = BigInteger.ONE;
    10         for(int i = 1; i <= 55; i ++){
    11             jiecheng = jiecheng.multiply(BigInteger.valueOf(i));
    12             a[i][i-1] = jiecheng;
    13             a[i][i] = jiecheng;
    14         }
    15         BigInteger cha = BigInteger.valueOf(2);
    16         for(int i= 1; i <= 50; i ++){//k
    17             //System.out.println(a[i+1][i]);
    18             //System.out.println(a[i][i]);
    19             BigInteger ch0 = a[i+1][i].subtract(a[i][i]);
    20             for(int j = i+2; j <= 50; j ++){
    21                 ch0 = ch0.add(cha);
    22                 a[j][i] = a[j-1][i].add(ch0);
    23             }
    24             cha = cha.multiply(BigInteger.valueOf(i+1));
    25         }
    26         //System.out.println(a[7][4]);
    27         int T = 0;
    28         T = cin.nextInt();
    29         for(int i = 1; i <= T; i ++){
    30             int n,k;
    31             BigInteger p;
    32             n =  cin.nextInt();
    33             k = cin.nextInt();
    34             if(k >= n){
    35                 k = n;
    36             }
    37             p = cin.nextBigInteger();
    38             System.out.print("Case #"+i+": ");
    39             System.out.println(a[n][k].mod(p));
    40         }
    41 
    42         //System.out.println("Hello World!");
    43     }
    44 }
  • 相关阅读:
    当框架限制了我的想法,我选择Hack
    Ubuntu下自制douban.fm播放器
    如何让基于Swing的Java程序在Linux下看起来更和谐
    基于Bootstrap的黑色极简博客园主题
    spring boot引入外部jar包
    Spring缓存注解@CachePut , @CacheEvict,@CacheConfig使用
    【转】c#如何监视文件或者文件夹的变化
    【转】一些重要的计数器
    【原创】使用timer、imagelist与notifyicon控件实现图标闪烁并避免了GDI泄漏(重点)
    【转】全角字符与半角字符的相互转换(C#)
  • 原文地址:https://www.cnblogs.com/Kidgzz/p/10825388.html
Copyright © 2011-2022 走看看