【题目描述】

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 k iterations has the property where the first k entries 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 A (1-based array) can be expressed as:He notes that a permutation of 1 to n is almost sorted if the length of its longest increasing subsequence is at least (n−1).Given the parameter k, you are asked to count the number of distinct permutations of 1 to n 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 T indicating the number of test cases which is up to 5000.For each test case, the only line contains three integers n,k and q indicating the length of the permutations, the parameter in his implementation and a prime number required for the output respectively, where 1≤n,k≤50 and 108≤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 1, and y is the remainder of the number of permutations which meet the requirement divided by q.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 10 permutations which meet the condition, and they are listed as follows:[1,2,3,4];
[1,2,4,3];
[1,3,2,4];
[1,3,4,2];
[1,4,2,3];
[2,1,3,4];
[2,3,1,4];
[2,3,4,1];
[3,1,2,4];
[4,1,2,3].

【题目分析】

这是一道打表题。自己以前对这种题很没有经验,因为不太习惯这种思维方式,虽然清楚找出来的规律肯定是有其深层含义的,但是直接找这种规律是很困难的,而且对于竞赛而言也并不需要理解公式的来源,能够解决问题就行,当然探求问题本质的习惯很好,但是对于竞赛我们很多时候要不求甚解,大胆尝试不去求证。不管黑猫白猫,能够抓到老鼠就是好猫,可能在思维层次上两个有优劣,但是从解决问题的角度都是一样的,不能因为觉得这样没水平或者是没有找到问题的本质就对这种有效解决问题的方式抵触。只能怪自己的思维不允许自己一下看出问题的关键而只能通过这种方式帮助解决问题。

而且快速找到规律也是一种能力,不能过分纠结于细节,这是为了找到规律而不是为了探求为什么。

【AC代码】

打表代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<climits>
#include<cctype>
#include<queue>
#include<set>using namespace std;typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=30;int nn,k;
int a[MAXN];
int b[MAXN];
int dp[MAXN];int deal(int n)
{for(int i=1;i<=n;i++){dp[i]=1;}int ret=0;for(int i=1;i<=n;i++){for(int j=1;j<i;j++){if(b[j]<b[i] && dp[j]+1>dp[i]) dp[i]=dp[j]+1; }if(dp[i]>ret) ret=dp[i];}return ret;
}int main()
{nn=10;for(int n=1;n<=nn;n++){printf("[%d]\t",n);for(int i=1;i<=n;i++) a[i]=i;for(int k=1;k<=n;k++){int cnt=0;do{memcpy(b,a,sizeof(a));sort(b+1,b+1+k);if(deal(n)>=n-1) cnt++;}while(next_permutation(a+1,a+n+1));printf("%d\t",cnt);}printf("\n");}return 0;
}

AC代码

#include<stdio.h>
int main()
{long long t,n,k,q,cot=0;scanf("%lld",&t);while(cot!=t){cot++;scanf("%lld%lld%lld",&n,&k,&q);if(k>n)k=n;long long ans=0,temp=1;for(long long i=1;i<=k;i++){temp*=i;temp%=q;}ans=temp;long long tz=k;for(long long i=k+1;i<=n;i++){ans+=temp*k;ans%=q;k+=2;}printf("Case #%lld: %lld\n",cot,ans);}return 0;
}

Insertion Sort——打表找规律相关推荐

  1. CodeForces - Insertion Sort(打表找规律)

    题目链接:http://codeforces.com/gym/101955/problem/C Time limit:6.0 s Memory limit:1024 MB Problem Descri ...

  2. 点分治问题 ----------- P3727 曼哈顿计划E[点分治+博弈SG函数打表找规律]

    题目链接 解题思路: 1.首先对于每个操作我们实际上是一个博弈问题 对于k=1的操作就是很基础的NIM游戏就是找到一条链的异或和为0 对于k=2的操作通过达打表找规律: 如果s是奇数那么偶数的SG函数 ...

  3. Yet Another Meme Problem(打表找规律)

    Try guessing the statement from this picture http://tiny.cc/ogyoiz. You are given two integers AA an ...

  4. hdu_5894_hannnnah_j’s Biological Test(打表找规律)

    题目链接:hdu_5894_hannnnah_j's Biological Test 题意: 有n个不同的位置围成一个圈,现在要安排m个人坐,每个人至少的间隔为k,问有多少种安排 题解: 先打表找规律 ...

  5. D. Pythagorean Triples(1487D)(打表找规律 + 二分)

    D. Pythagorean Triples(1487D)(打表找规律 + 二分) 题目来源:D. Pythagorean Triples 题意: 给定一个 n,求满足以下条件的数对 (a, b, c ...

  6. Ural 2045. Richness of words 打表找规律

    2045. Richness of words 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2045 Description For ...

  7. Ural 2037. Richness of binary words 打表找规律 构造

    2037. Richness of binary words 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2037 Descripti ...

  8. [国家集训队]整数的lqp拆分 数学推导 打表找规律

    题解: 考场上靠打表找规律切的题,不过严谨的数学推导才是本题精妙所在: 求:$\sum\prod_{i=1}^{m}F_{a{i}}$ 设 $f(i)$ 为 $N=i$ 时的答案,$F_{i}$ 为斐 ...

  9. [codeforces 1327E] Count The Blocks 打表找规律+根据规律找公式+优化公式

    Educational Codeforces Round 84 (Rated for Div. 2)   比赛人数13522 [codeforces 1327E]  Count The Blocks  ...

最新文章

  1. Go远超Python,机器学习人才极度稀缺,全球16,655位程序员告诉你这些真相
  2. 学好python工资一般多少钱-学会Python后,月薪40k是什么水平?
  3. 【机器学习基础】机器学习中的特征工程总结!
  4. 解放原画师!Wav2Lip 用 AI 听音同步人物口型
  5. 学生信息管理系统问题集锦(一)
  6. pythonunicode和str_python unicode 和 str 类型的关系
  7. fusion360界面字体模糊处理方法
  8. ue4蓝图碰撞检测的类型_UE4蓝图碰撞检测解析
  9. 《算法和数据结构》学习路线指引
  10. codeforces 831A Unimodal Array
  11. 数字ic设计——SPI
  12. 链改价值节点,构建区块链命运共同体
  13. 2021江苏大学生编程大赛I题(省赛试水)
  14. TI Sitara系列AM64x核心板(双核ARM Cortex-A53)软硬件规格资料
  15. 定位决定地位,眼界决定境界
  16. iOS ProductspecificationsTree 自定义cell 采用MVVM实现:【选择多级商品规格信息(树形,多选)】应用场景: 发布商品-添加多规格信息
  17. 安卓手机端一键抠图,这款软件适合你
  18. Python入门(八):文件处理
  19. 救助:Word文档页面上下方黑线如何去除-非页眉页角
  20. 基于 Django 3.2 VUE nginx 框架开发 机器学习在线 系统

热门文章

  1. js 判断浏览器是否64位
  2. 翻译的一篇关于学习编程语言的小文章
  3. Vue组件间通信:一个例子学会Vue组件-Vue.js学习总结)(转载)
  4. 这是我第一题AC的线段树
  5. 控件中的Events个人理解。
  6. python软件是哪个国家的品牌_有哪些好用的软件被国人误认为是外国研发的?
  7. react usecontext_Vue3原理实战运用,我用40行代码把他装进了React做状态管理
  8. c await和java_blog/java/test/awaitility.zh.md at master · c-rainstorm/blog · GitHub
  9. linux 查看下挂磁盘,linux下磁盘挂载与查看
  10. php签名墙,肺功能检查质量控制网