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 ;}