HDU 3830 Checkers, hdu3830checkers
Question:
There are three pieces that can jump between one piece and not between two jumps ask the status u to the status v to jump at least a few steps
Ideas:
The position of three pawns in one State can be set to x y z (small to large)
Only y-x = z-y can be skipped by y or y.
When the equation is not in the preceding formula, the short side can jump many times until the size and relationship change.
In this way, the binary tree structure is formed. We use y to jump between the left and right as the original state. The son uses one of the two sides to jump to the middle state as the father of the original state.
Then the accessibility of u and v is changed to whether they are the same root. So we can jump from u and v to the header to determine whether they are the same root.
How many times can I skip ?? In this case, the LCA method is the same as the multiplication method, that is, u and v are first crawled at the same height and then simultaneously crawled.
The crawling method is the same as the previous state moving to the root. Because the table is not multiplied, we need to determine the height of the crawling using the binary method after the same depth.
Code:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;typedef unsigned long long LL;#define M 1100#define N 16struct state {int x[3];void Sort() {sort(x, x + 3);}int Root() {int floor = 0;int a1 = x[1] - x[0], a2 = x[2] - x[1];while (a1 != a2) {if (a1 > a2) {int d = (a1 - 1) / a2;floor += d;x[2] -= d * a2;x[1] -= d * a2;} else {int d = (a2 - 1) / a1;floor += d;x[0] += d * a1;x[1] += d * a1;}Sort();a1 = x[1] - x[0], a2 = x[2] - x[1];}return floor;}bool operator==(const state ff) const {return (x[0] == ff.x[0]) && (x[1] == ff.x[1]) && (x[2] == ff.x[2]);}void GoUp(int floor) {while (floor) {int a1 = x[1] - x[0], a2 = x[2] - x[1];if (a1 > a2) {int d = (a1 - 1) / a2;if (d > floor)d = floor;floor -= d;x[2] -= d * a2;x[1] -= d * a2;} else {int d = (a2 - 1) / a1;if (d > floor)d = floor;floor -= d;x[0] += d * a1;x[1] += d * a1;}Sort();}}} u, v, fau, fav;int main() {int fu, fv, ans;while (~scanf("%d%d%d%d%d%d", &u.x[0], &u.x[1], &u.x[2], &v.x[0], &v.x[1],&v.x[2])) {u.Sort();v.Sort();fau = u;fav = v;fu = fau.Root();fv = fav.Root();if (fau == fav) {puts("YES");if (fu > fv) {ans = fu - fv;u.GoUp(ans);} else if (fv > fu) {ans = fv - fu;v.GoUp(ans);} elseans = 0;int l = 0, r = min(fu, fv), mid, tmp;while (l <= r) {mid = (l + r) >> 1;fau = u;fav = v;fau.GoUp(mid);fav.GoUp(mid);if (fau == fav) {r = mid - 1;tmp = mid;} elsel = mid + 1;}printf("%d\n", ans + (tmp << 1));} elseputs("NO");}return 0;}