Ultraviolet A 718-skyscraper floors (number theory)

Source: Internet
Author: User
Ultraviolet A 718-skyscraper floors

Question Link

There is an e-elevator in an F-storey upstairs. Each elevator has X, and y indicates that Y + K * X can be reached, now I want to ask if I can reach B from layer a (there is no restriction on how to change to the elevator in the middle)

Train of Thought: For the transfer between two elevator rooms, As long as Y [I] + xx x [I] = Y [J] + YY y [J]. next, you can use Extended Euclidean solutions to find the general solutions of X and Y, and then use the general solution range x'> = 0, y'> = 0, X [I] x' + Y [I] <= f, x [J] y' + Y [J] <= F, and the upper and lower limits of T in the general solution are obtained, then we can determine that there is no solution, and then we will build a map, and change the two sides of the elevator to build the edge, and then let a, B and the elevator determine whether they can be reached, to build the edge, the last DFS can be used to determine whether it can be reached.

Code:

#include <stdio.h>#include <string.h>#include <vector>#include <math.h>using namespace std;const int INF = 0x3f3f3f3f;int t, f, e, a, b, x[105], y[105], vis[105];vector<int> g[105];int exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1; y = 0; return a;}int d = exgcd(b, a % b, y, x);y -= a / b * x;return d;}void build(int v, int u, int i) {if (v < y[i]) return;if ((v - y[i]) % x[i] == 0) {g[u].push_back(i);g[i].push_back(u); }}void build2(int i, int j) {int xx, yy;int a = x[i], b = -x[j], c = y[j] - y[i];int d = exgcd(a, b, xx ,yy);if (c % d) return;int down = -INF;int up = INF;if (b / d > 0) { down = max(down, (int)ceil(-xx * c * 1.0 / b)); up = min(up, (int)floor(((f - y[i]) * 1.0 * d / x[i] - xx * c * 1.0) / b));}else {down = max(down, (int)ceil(((f - y[i]) * 1.0 * d / x[i] - xx * c * 1.0) / b)); up = min(up, (int)floor(-xx * c * 1.0 / b));}if (a / d > 0) {down = max(down, (int)ceil((yy * c * 1.0 - (f - y[j]) * 1.0 * d / x[j]) / a)); up = min(up, (int)floor(yy * c * 1.0 / a));}else { down = max(down, (int)ceil(yy * c * 1.0 / a)); up = min(up, (int)floor((yy * c * 1.0 - (f - y[j]) * 1.0 * d / x[j]) / a));}if (down > up) return;g[i].push_back(j);g[j].push_back(i);}bool dfs(int u) {if (u == e + 1) return true;vis[u] = 1;for (int i = 0; i < g[u].size(); i++) {int v = g[u][i];if (vis[v]) continue;if (dfs(v)) return true; } return false;}int main() {scanf("%d", &t);while (t--) {memset(g, 0, sizeof(g));scanf("%d%d%d%d", &f, &e, &a, &b);for (int i = 1; i <= e; i++) {scanf("%d%d", &x[i], &y[i]);   build(a, 0, i);   build(b, e + 1, i);}for (int i = 1; i <= e; i++) {for (int j = i + 1; j <= e; j++) {build2(i, j);   }  }  memset(vis, 0, sizeof(vis));  if (dfs(0)) printf("It is possible to move the furniture.\n");  else printf("The furniture cannot be moved.\n"); }return 0;}


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.