Dish owner (and query set)
Link: http://www.codechef.com/JULY14/problems/DISHOWN/
Question analysis: the main operation of this question is to give 0 x Y, the cook with the X dish and the cook with the Y dish PK, whoever owns one of the dishes (not necessarily X or Y) has a higher score, wins and wins all the dishes of loser. That is to say, it is the food with the highest score for each person. Therefore, for non-loser users, the food with the highest score remains unchanged. Comprehensive questions are intended for use and easy to understand.
# Include <cstdio> const int maxn = 10004; int to [maxn]; int Val [maxn]; int _ Find (int x) {If (to [X]! = X) to [X] = _ Find (to [X]); // return to [X];} void _ merge (int x, int y) {int FX = _ Find (x); int FY = _ Find (y); To [FY] = FX;} int main () {int T; scanf ("% d", & T); While (t --) {int N; scanf ("% d", & N); For (INT I = 1; I <= N; I ++) {scanf ("% d", & Val [I]); To [I] = I;} int query; scanf ("% d", & query); While (query --) {int op; scanf ("% d", & OP); If (OP = 0) {int X, Y; scanf ("% d", & X, & Y); int FX = _ Find (x); // obtain X, y's parent node int FY = _ Find (y); I F (FX = FY) puts ("invalid query! "); Else {If (Val [FX]> Val [FY]) {_ Merge (x, y);} else if (Val [FX] <Val [FY]) {_ Merge (Y, x) ;}} else {int X; scanf ("% d", & X); printf ("% d \ n ", _ Find (x) ;}} return 0 ;}
Garden game
Link: http://www.codechef.com/JULY14/problems/SGARDEN
Meaning analysis: every time a whistle appears, the person in the position I moves to the position a [I], where a [I] is not repeated. After a limited number of rounds, yi zhi will be able to restore to the initial state. There will be multiple irrelevant cycles, find the number of people in each cycle, and calculate its minimum public multiple. Note that the result will exceed the int range.
Python version:
# Coding: utf8def gcd (A, B): # calculate the maximum common divisor of A and B if (B = 0): return a return int (gcd (B, A % B) def fun (A, B): _ GCD = gcd (A, B) product = A // _ GCD * B # Use the // operator to ensure that the result is an integer return int (product) t = int (input () while (T> 0 ): N = int (input () num = [0] vis = [0] x = input () For I in X. split (): num. append (INT (I) vis. append (0) DIV = [] ST = 1 # calculate the number of people in each loop while (ST <= N): If (vis [st] = 0 ): mark = sT vis [st] = 1 CNT = 1 Nx = num [st] While (M Ark! = Nx): vis [NX] = 1 Nx = num [NX] CNT + = 1 If (CNT not in Div): div. append (CNT) ST + = 1 ans = 1 for each in Div: ANS = fun (ANS, each) print (INT (ANS % 1000000007) T-= 1
C ++ version:
Because the intermediate operation results even exceed the 64-bit integer range, I adopted the factorization method.
# Include <cstdio> # include <cstring> # include <set> using namespace STD; const int maxn = 100005; const int mod = 1000000007; int num [maxn]; bool vis [maxn]; int prime [10000], primecount = 0; int totalcount [10000]; void getprime () {memset (VIS, 0, sizeof (VIS )); int I, j; for (I = 2; I <100000; I ++) {If (! Vis [I]) {Prime [primecount ++] = I; for (j = I + I; j <100000; j + = I) {vis [J] = 1 ;}}} void getdivsor (int param) {int I; int num = Param; int CNT; for (I = 0; prime [I] <= num & I <primecount; I ++) {If (Num % prime [I] = 0) {CNT = 0; while (Num % prime [I] = 0) {CNT ++; num/= prime [I];} If (totalcount [I] <CNT) totalcount [I] = CNT ;}} int main () {getprime (); // calculate all the prime numbers int t in 0.1 million; scanf ("% d ", & T); While (t --) {int N; scanf ("% d", & N); For (INT I = 1; I <= N; I ++) {scanf ("% d", & num [I]);} memset (VIS, 0, sizeof (VIS )); int ST = 1, NX, CNT, mark; set <int> div; while (ST <= N) {If (vis [st] = 0) {mark = sT; vis [st] = 1; CNT = 1; Nx = num [st]; while (mark! = Nx) {vis [NX] = 1; Nx = num [NX]; ++ CNT;} Div. insert (CNT) ;}++ st ;}long long ans = 1; set <int >:: iterator it; for (IT = div. begin (); it! = Div. end (); It ++) {getdivsor (* It) ;}for (INT I = 0; I <primecount; I ++) {long temp = 1; while (totalcount [I]> 0) {temp = (temp * prime [I]) % MOD; totalcount [I] --;} ans = (ANS * temp) % MOD;} printf ("% LLD \ n", ANS);} return 0 ;}