1. Title Description
The topic is an interesting game problem, the title of the background of the description is very verbose, omitted to talk about.
The specific game rules can be described as follows:
Two persons A and b,a a positive integer A and a positive integer b. The larger of the person's game character is Leizhu, and the smaller one's game character is the challenger. Like what:
A = 342, B = 49, obviously a > B, then A is Leizhu, and B is the challenger.
So what are the rules that challenge winning/failing?
We know that for any positive integer n, we can decompose it, and there may be many forms of decomposition. Like what:
n = 20 o'clock, it is possible to decompose its factors into the following forms:
n = 20 * 1 = 10 * 2 = 5 * 4 = 5 * 2 * 2
The Challenge rule here is:
Challenge success: If all of the decomposition forms of Leizhu, the Challenger has a decomposition, the two decomposition has the same factor.
Challenge failure: If there is a decomposition of leizhu for all the challengers, all the factors in the decomposition do not exist in the challenger's decomposition.
2. Algorithm design
Enumerates all the factorization of a challenger, and enumerates the factorization of Leizhu for each decomposition.
We use a tag array F to record whether a factor I already exists in the Challenger's factorization, if present, f[i] = 1, otherwise f[i] = 0.
For factor I of f[i] = 1, we can not use it to decompose Leizhu.
This goes on until a case is found: Leizhu's factor decomposition is completed successfully, then we can determine Leizhu wins and challenges fail.
If we don't find such a case until the enumeration is complete, we say Leizhu defeated and challenged successfully.
The enumeration process takes a depth-first search traversal .
(Note: Each factor can only occur once, that is, factor decomposition can not be decomposed into the form of: 5 * 2 * 2 )
3. AC Code
1#include <stdio.h>2#include <string.h>3 #defineN 1054 #defineMIN (A, B) ((a>b)? b:a)5 #defineMAX (A, B) ((a>b)? a:b)6 7 inth, L;8 intF[n];9 intis_h_can_be_divided, is_h_divided;Ten One intDfsintn); A - intMain () - { the intA, B; - intDfs_ret; - intwinner; - + //freopen ("In.txt", "R", stdin); - while(EOF! = scanf ("%d%d", &a, &b)) + { A atL =MIN (A, b); -h =MAX (A, b); - - //F[i] = 0, initially, all 1~n are not occupied -Memset (F,0,sizeof(f)); -is_h_divided =0; inis_h_can_be_divided =0; - toDfs_ret =DFS (l); + - //if H cannot be broken down by rules, H wins the if(0==is_h_can_be_divided) *Winner =h; $ //if H can be broken down by rules, butPanax Notoginseng Else if(1= = is_h_can_be_divided &1==Dfs_ret) -Winner =h; the Else +Winner =l; Aprintf"%d\n", winner); the + } - return 0; $ } $ - intDfsintN) - { the intn_size; - inti;Wuyi the //n = = 1 o'clock, which identifies the end of the decomposition of the number - if(1==N) Wu { - //is_h_divided as the decomposition end tag of H About if(0==is_h_divided) $ { -is_h_can_be_divided =1; -is_h_divided =1; - if(Dfs (h)) A return 1; + Else //h is not decomposed, and cannot continue decomposition the { -is_h_divided =0; $ return 0; the } the } the return 1; the } - in //the process of decomposing n theN_size = -< n? -: N; the for(i =2; I <= n_size; ++i) About { the //n% i = = 0 <=> i is n factor the //F[i] = = 0 <=> I have not been occupied, that is guaranteed not to appear 2 * 2 * 5 (i=2) the if(0= = N I &&0==F[i]) + { -F[i] =1; the if(Dfs (n/i))return 1;BayiF[i] =0; the } the } - return 0; -}View Code
USTC OJ-1013 Gizilch (search, medium title)