SOJ1942 Foto of Problem Solving report

Source: Internet
Author: User

SOJ1942 Foto of Problem solving report

Description

The election campaign just started, so PSOS decided to make some propagation. One of the representatives came with a great idea-he proposes to make an photography of their Parliament Club. Unfortunatelly, even after many briefings, the representatives is still not able to agree upon a ordering of people in t He photography. Moreover, there was a lot of representatives and it was not possible to has all of the them in a single picture. The situation became critical. In the end, the representatives decided they would make one photo of every possible combination of people and their orderin G. Different photographs is used for a large number of billboards PSOS plan to use. To make things more clear, every person gets aUnique Identification number(UIN). Every picture can is then described as the succession of several uinsx1, x2, ... xk, in whichxiis UIN of thei-th person in the picturex. Now we can sort all the possible photographs (combinations) to a single succession. The ordering of combinations of the same length (photographs with the same number of people in it) is defined as follows: The combinationpis greater than the combinationqIf there exists anyisuch as thatpj = qjFor everyj < i, andpi > qi. Your goal is to find the right place for a given picture among all possible photographs.

Input specification

At the first line there are a positive integer stating the number of N assignments to follow. Each assignment consists of exactly and lines. At the first line of each assignment, there is integers and n k , stating the total number of 1 <= k <= n <= 12 Representa Tives () and the number of n them which can fit into a single picture ( k ). At the second line of the assignment, there are exactly k positive numbers x1, x2, ... xk , each of the them 1 <= xi <= n . No number can appear more than once on this line.

Output specification

For each assignment, output the text "variace cislo I ma poradove cislo J ." (combination # I is J -th in sequence). Fill the number of assignment instead I of (starting with one), and the number of the given photograph among all Possi BLE combinations after ordering, instead of J (also starting with one).

Sample Input
41 115 143 31 2 35 35 3 1
Output for Sample Input
Variace Cislo 1 ma poradove cislo 1.Variace cislo 2 ma poradove cislo 4.Variace cislo 3 ma poradove cislo 1.Variace cislo 4 Ma poradove cislo 55.

The main topic: There are kase Group data, each group of data given N, indicating a total of several (1~n). Give the K again, indicating that you want to select several numbers from it. Now give you a number arrangement, asking you to order from the dictionary order from small to large, this arrangement is the first major?

Analysis: Feeling is a very simple problem of permutation number, we try to find out the sequence number before the sequence, plus one. For example, 5 3 5 3 1 This set of data, 5 number of 3, Q 5 3 1 is the number one? First we see, to find than 531 small number, there are two ways, the first is to replace the first number, and then randomly arranged, the second is to maintain 5 unchanged, behind the recursion with a strategy to continue to find. Then the answer is to add up the two answers.
the first way, for one, how can we find a few options? or 5 3 1 For example, the beginning to 5, 5 before all remain unchanged (in fact, 5 is not the front), then 5 in front of his large number 0, 5 front than his small number 0, useless than 5 small number 4, then C (4,1), the second step, 5 after the arbitrary arrangement, That is, choose two arbitrary permutations from the remaining 4 numbers (4,2), and part of the answer is C (4,1) *a (4,2). Then follow this method to add to the last one.
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio>using namespace Std;int Greaterb4[15];int Num[15];long Long A (int a, int b) {Long Long res = 1;for (int i = A; i > A-b; i--) {res *= i;} return res;} int main () {int kase;cin >> kase;int n, k;for (int step = 1; step <= Kase; step++) {memset (greaterb4, 0, sizeof GRE ATERB4); Cin >> n >> k;for (int i = 1; I <= K; i++) {cin >> num[i];for (int j = 1; j < I; J + +) {if (num [J]>num[i]) greaterb4[i]++; Statistics are preceded by several larger numbers}}//input long long ans = 0;for (int i = 1; I <= K; i++) {Long Long tem = num[i]-(I-greaterb4[i]);//c (num,1) where NUM indicates how many tem *= A (n-i, k-i) are smaller than this number and are not used before, and/or the remaining number is fully aligned ans + = tem;} printf ("Variace cislo%d ma poradove cislo%lld.\n", step, ans + 1);} return 0;}

The end of the puzzle. In addition, if you do, you can save a super-Diao number of permutations. The feeling can be O (n) time pre-processing and then in O (1) time to find out the permutation combination number.
The first generation of the operations base, regardless of the number of permutations and combinations are the same. That is, call the FAC () function to produce the operation base. then the number of permutations and the number of combinations only need to separate the operation of the base of the operation can be. Note that both operations are double, and it is important to add exp when calculating the final result.
#include <iostream> #include <cmath> #include <cstdio>using namespace std;const int maxn = 1e6;double f[ Maxn];void FAC () {f[0] = 0;for (int i = 1; i < MAXN; i++) {f[i] = F[i-1] + log (i*1.0);}} Double Geta (int n, int m)//calculates the value of a (n,m), n is the number in the lower right corner, M is the number in the upper right corner {return f[n]-f[n-m];} Double GetC (int n, int m)//calculates the value of C (n,m), n is the lower-right corner number, M is the upper-right corner of the number {return f[n]-f[m]-f[n-m];}  int main () {FAC (); int n, m;while (CIN >> n >> m) {printf ("A (%d,%d) =%lf\n", N, M, exp (Geta (n, m)));p rintf ( "C (%d,%d) =%lf\n", N, M, exp (GetC (n, m)));} return 0;}

Well, save a template, temporarily this kind of bar! ~~

SOJ1942 Foto of Problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.