HDU 3830 checkers

Source: Internet
Author: User

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


HDU 3830 checkers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.