zoukankan      html  css  js  c++  java
  • C

    C - Insertion Sort(数学规律题, 递推)

    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].

    [思路]:根据题意进行模拟,可以打出对应的表,如下
    1 0 0 0 0 0 0 0 0 0 
    2 2 0 0 0 0 0 0 0 0 
    5 6 6 0 0 0 0 0 0 0 
    10 14 24 24 0 0 0 0 0 0 
    17 26 54 120 120 0 0 0 0 0 
    26 42 96 264 720 720 0 0 0 0 
    37 62 150 456 1560 5040 5040 0 0 0 
    50 86 216 696 2640 10800 40320 40320 0 0 
    65 114 294 984 3960 18000 85680 362880 362880 0 
    82 146 384 1320 5520 26640 141120 766080 3628800 3628800
    根据表可以发现对应的规律,先去掉达到n!的不看
    那么容易得到 f[i][j] = (f[i - 1][j - 1] * j) + (j * (j - 1) * te[i - 2][j - 1]) 
    te数组下文解释
    举个例子:我们可以发现
    54 = 14 * 3 + 2 * 3 * 2
    264 = 54 * 4 + 3 * 4 * 4
    1560 = 264 * 5 + 4 * 5 * 12
    可以得知最后一项2 4 12
    2 * 2 = 4 * 3 = 12 * 4 = 48 ......
    以此来推导出te数组
    1
    2 1
    3 2 
    4 3 4
    5 4 6 12 ..........
    那么容易得到上述的递推公式,即可ac该题
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    LL arr[55][55];
    LL te[55][55];
    void Init(LL p){
        arr[1][1] = 1;
        int x = 1;
        for(LL i = 2; i <= 50; i ++){
            arr[i][1] = arr[i - 1][1] + x;
            x += 2;
        }
        for(LL j = 2; j <= 50; j ++){
            for(LL i = j + 1; i <= 50; i ++){
                arr[i][j] = ((arr[i - 1][j - 1] % p * j % p )%p + (j % p * (j - 1) % p * te[i - 2][j - 1] % p) % p) % p;
                //cout << arr[i][j] << " " << te[i - 1][j - 1] << endl;
            }
        }
    }
    void Inits(LL p){
        for(LL i = 1; i <= 55; i ++){
            te[i][1] = i;
            te[i + 1][2] = i;
        }
        for(LL j = 3; j <= 55; j ++){
            for(LL i = 3; i <= 55; i ++){
                te[i][j] = (te[i - 1][j - 1] * (j - 1)) % p;
            }
        }
    }
    LL jiecheng(int x, int p){
        LL sum = 1;
        for(LL i = 1; i <= x; i ++){
            sum = (sum * i) % p;
        }
        return sum;
    }
    int main(){
        int T;
        ios::sync_with_stdio(false);
        cin >> T;
        int cases = 0;
        while(T --){
            LL n, k, p;
            cin >> n >> k >> p;
            Inits(p);
            Init(p);
            if(arr[n][k] == 0){
                cout << "Case #" << ++cases << ": "  << jiecheng(n, p) << endl; 
            }
            else{
                cout << "Case #" << ++cases << ": "  << arr[n][k] << endl; 
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Mongodb 插入数据的方式
    vue插槽(slot)的模板与JSX写法
    vue-resource+iview上传文件取消上传
    封装全局icon组件 svg (仿造element-ui源码)
    vue 修改框架less变量
    vue webpack多页面构建
    vue2 自定义全局组件(Loading加载效果)
    制作npm插件vue-toast-m实例练习
    iview+axios实现文件取消上传
    CSS Variables
  • 原文地址:https://www.cnblogs.com/qq136155330/p/11729991.html
Copyright © 2011-2022 走看看