2015 Baidu STAR Program Design Competition, 2015 STAR Program Design

Source: Internet
Author: User

2015 Baidu STAR Program Design Competition, 2015 STAR Program Design

Question Link: Click here ~~

Theme]:

Factory B recently organized a big migration, and everyone had to switch to the specified seat as instructed. The content indicated is sitting in the position. I The person is about to move to the location. J . Factory B now has N Individual, one-to-one N . After the migration, there is a one-to-one correspondence, and only the location is changed.

After the first migration, du duxiong asked everyone to perform a migration according to the original instructions due to negligence. As a result, the wit thought: If you move a house again, you will not be able to restore the first migration. As a result, factory B has made three successive moves in an unprecedented manner.

Although we all know that Dudu Xiong's "wit" is often worrying, it is incredible that this is a true fulfillment. The results after the third migration are exactly the same as those after the first migration.

How many indicators will this happen? If at least one of the two indicators has a different target location, they are considered to be different.

Solutions]: To see the question, the feeling is to push the law, the current I personal instructions must be related to the I-1, Assuming I = 1, 2, 4, 5 .. it is not difficult to roll out the recursive formula: s [I] = (s [I-1] + (I-1) * s [I-2]) % mod; // s [0] = s [1] = 1.

Code:

# Include <stdio. h> # include <string. h >#include <iostream >#include <algorithm> using namespace std; const int N = 1e6 + 5; const int mod = 1e9 + 7; long s [N]; // note long void Init () {s [0] = s [1] = 1; for (int I = 2; I <= N-5; I ++) s [I] = (s [I-1] + (I-1) * s [I-2]) % mod;} int main () {Init (); int t, n, tot = 1; scanf ("% d", & t); while (t --) {scanf ("% d", & n); printf ("Case # % d: \ n % d \ n ", tot ++, s [n]);} return 0 ;}


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.