2018 ACM/ICPC 沈阳站 C Insertion Sort

匿名 (未验证) 提交于 2019-12-02 22:59:29
版权声明:欢迎转载,请注明此博客地址。 https://blog.csdn.net/Ever_glow/article/details/84228482

题目描述

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:

输入


输出

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.

样例输入

 4 4 1 998244353 4 2 998244353 4 3 998244353 4 4 998244353 

样例输出

 Case #1: 10 Case #2: 14 Case #3: 24 Case #4: 24 

提示

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









注意,n < k 的时候输出n!,而不是0.我竟然天真的交了好几发0.

代码实现:

 /* Look at the star Look at the shine for U */ #include<bits/stdc++.h> #define ll unsigned long long #define PII pair<int,int> #define sl(x) scanf("%llu",&x) using namespace std; const int N = 1e6+5; const int mod = 1e9+7; const int INF = 0x3f3f3f3f; const double PI = acos(-1); ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;} ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}   int main() {     ll t,n,i,j,k,cas = 1,q;     for(sl(t);t--;)     {         sl(n);sl(k);sl(q);         if(n < k)         {             ll ans = 1;             for(i = 1;i <= n;i++)                 ans = ans*i%q;             printf("Case #%llu: %llu\n",cas++,ans);             continue;         }         ll ans = 1;         for(i = 1;i <= k;i++)             ans = ans*i%q;         ll now = (n-k-1+q)%q;         ll sum = 1+now*now%q;         sum %= q;         sum = sum+(k*(((n-k)%q+q)%q)%q)%q;         sum = (sum+(n-k-1))%q;         ans = ans*sum%q;         printf("Case #%llu: %llu\n",cas++,ans);     }     return 0; }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!