Bestcoder 1001 Big Move (permutation combination)

Source: Internet
Author: User

Problem Description

i recently organized a big move in B factory, everyone should follow the instructions to change to the designated seat. Indicates that the content is seated in position I The man on the move to the position JJ On Now B Factory has nn Individual, one to nn Position on the other. After the move is also one by one corresponding, change only the rank.

After the first move, the degree of bear due to negligence, but also asked you to follow the original instructions for a move. So, the wit of it thought: again according to the instructions to move home once again can restore the first time the appearance of the move. As a result, plant B has carried out an unprecedented three consecutive moves.

While we all know that the "wit" of a bear is often worrying, it is inconceivable that this is a real thing. The result of the third move is exactly the same as the result of the first time.

So, how many kinds of instructions are going to make this happen? If at least one of the two instructions has a different target location, the two instructions are considered to be not the same.

Input

The first line is an integer tt, which represents the T group data.

Each group of data contains an integerN (1 \leq n \leq 1) n(1≤n≤1000 0 0 0 )。

Output

For each set of data, output one line of case #i: Then output the results, 10000000071 0 0 0 0 0 0007 Take the mold.

Sample Input
213
Sample Output
Case #1:1Case #2:4

Analysis:

Permutation combination problem, want to learn the algorithm, but also to learn maths, to introduce the transfer equation, in the case of example 987654321→912345678

We start with the smallest unit--a number that takes into account all of its circumstances, in total two kinds:
1. Do not change position (4→4)
2. Position swap with other numbers (7→2,2→7)

the probability that we have the number of I in total is f (i)
Ⅰ. For a row number, if a number does not change position (case one), then the possibility of the whole row is not related to him, that is f (i-1);
Ⅱ. If a number is replaced with another number, then they must correspond to each other, and the remainder of the case is F (i-2), and a number can correspond to the remaining number of i-1, so the number of cases is (i-1) *f (i-2)

Combining the above two cases all the possibilities are f (i) = (f (i-1) + (i-1) *f (i-2))% MOD, the table can be obtained results.

#include <stdio.h> int Mid=1e9+7;long long  a[1000010];int main () {    int t,n,i,j;    a[1]=1;a[2]=2;    for (i=3;i<1000010;i++)        a[i]= (a[i-1]+ (i-1) *a[i-2])%mid;//each time the remainder is taken without affecting the final result    scanf ("%d", &n);    for (i=1;i<=n;i++)    {        scanf ("%d", &t);        printf ("Case #%d:\n%i64d\n", i,a[t]);    }    return 0;}



Bestcoder 1001 Big Move (permutation combination)

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.