Create a table
Question: Input n to generate a n in the full arrangement of a, and generate a n in the full arrangement of B, from 1st to n, corresponding to each bit (ai + bi) % n = ci, and then get the n-bit sequence c to see if c is also a full arrangement of n. If yes, it will succeed and output. If no suitable a and B can be found to construct c, output-1
This is just a bit of water, but I still think about it for a long time.
Process of thinking
1. it is easy to think that the question is that we want to construct a suitable a and B to generate a suitable c. Although the question allows us to output any legal arrangement, but what we are looking for is not an arrangement scheme, but a matching scheme.
Imagine that if there is a proper arrangement on hand, we will bundle the corresponding ai, bi, and ci versions at one time, we can re-arrange this arrangement (when we re-arrange it, the elements of the bundle must be moved together) so that we can re-exclude n! Are all legal because the corresponding bits are together.
So we come to a conclusion that there is no solution. If there is a solution, the number of solutions should be at least n! (N = 1 is also true, n = 1 is special, that is, 0 0), because there may be multiple matching Solutions
2. The above conclusions are far from enough to solve the problem. At this time, I want to use brute force statements to see what the rules will follow when n is small, but I keep writing it without a computer. When we finally pushed it to 8, we found that n was an even number and it was not resolved. Later, we used a computer to create a table and confirmed this rule.
3. however, there is still no good solution for the case where n is an odd number. In the end, we still rely on the computer to create tables and find that this is a problem, as long as both a and B are 0 1 2 3 4 ...... N-1 is a feasible solution.
So we solved this problem.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 100010int main(){ int n; cin >> n; if(!(n&1)) cout << -1 << endl; else { for(int i=0; i<n; i++) cout << i << " "; cout << endl; for(int i=0; i<n; i++) cout << i << " "; cout << endl; for(int i=0; i<n; i++) cout << (2*i)%n << " "; cout << endl; } return 0;}
Tabulation Program
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define N 100010int a [N], B [N], c [N]; bool OK, usedb [N], usedc [N]; void dfs (int n, int cur) {if (cur> = n) {OK = true; cout <"feasible solution" <endl; for (int I = 0; I <n; I ++) cout <a [I] <""; cout <endl; for (int I = 0; I <n; I ++) cout <B [I] <""; cout <endl; for (int I = 0; I <n; I ++) cout <c [I] <""; cout <endl; Return;} for (int I = 0; I <n; I ++) if (! Usedb [I] &! Usedc [(a [cur] + I) % n]) {usedb [I] = usedc [(a [cur] + I) % n] = true; B [cur] = I; c [cur] = (a [cur] + I) % n; dfs (n, cur + 1 ); usedb [I] = usedc [(a [cur] + I) % n] = false ;}} void solve (int n) {memset (usedb, false, sizeof (usedb); memset (usedc, false, sizeof (usedc); for (int I = 0; I <n; I ++) a [I] = I; dfs (n, 0); if (! OK) cout <"unsolvable" <endl ;}int main () {freopen ("BF.txt", "w", stdout); for (int n = 1; n <= 10; n ++) {printf ("% d: ___________ \ n", n); solve (n); cout <endl;} return 0 ;}