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

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 }
全部评论

相关推荐

面试官全程关摄像头1.自我介绍一下2.React和Vue哪个更熟悉一点3.你在之前那段实习经历中有没有什么技术性的突破(我只是实习了44天工作28天,我把我能说的都说了)4.你封装的哪个表单组件支不支持动态传值5.自己在实习阶段Vue3项目封装过hook吗6.hook有什么作用7.Vue2和Vue3的响应式区别(我说一个是proxy是拦截所有的底层操作,Object.defineProperty本身就是一个底层操作,有些东西拦截不了,比如数组的一些操作还有等等,面试官就说实在要拦截能不能拦截????我心想肯定不行呀,他的底层机制就不允许吧)8.pinia和vuex的区别(这个回答不出来是我太久没用了)9.pinia和zustand的区别,怎么选(直接给我干懵了)(我说react能用pinia吗&nbsp;&nbsp;他说要用的话也可以)10.渲染一万条数据,怎么解决页面卡顿问题(我说分页、监听滚轮动态加载,纯数据展示好像还可以用canvas画)(估计是没说虚拟表单,感觉不满意)11.type和interface的区别12.ts的泛型有哪些作用(我就说了一个结构相同但是类型不同的时候可以用,比如请求响应的接口,每次的data不同,这里能用一个泛型,他问我还有什么)13.你项目用的是React,如果让你再写一遍你会选择什么14.pnpm、npm、yarn的区别15.dependencies和devdependencies的区别总而言之太久没面试了,上一段实习的面试js问了很多。结果这次js一点没问,网络方面也没考,表现得很一般,但是知道自己的问题了&nbsp;&nbsp;好好准备,等待明天的影石360和周四的腾讯了&nbsp;&nbsp;加油!!!
解zj:大三的第一段面试居然是这样的结局
查看15道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务