UVA 718 - Skyscraper Floors(数论)
UVA 718 - Skyscraper Floors
题意:在一个f层高的楼上,有e个电梯,每一个电梯有x,y表示y + k * x层都能够到,如今要问从a层是否能到达b层(中间怎么换乘电梯不限制)
思路:对于两个电梯间能不能换乘,仅仅要满足y[i] + xx x[i] == y[j] + yy y[j].然后移项一下,就能够用拓展欧几里得求解,进而求出x,y的通解,然后利用通解范围x' >= 0, y' >= 0, x[i]
x' + y[i] <= f, x[j] y' + y[j] <= f,求出通解中t的上限和下限,就能够推断有无解了,然后就建图,把两个能够换乘电梯建边,然后在让a, b分别和电梯推断能不能到,能到建边,最后dfs一遍就能推断是否能达到了。
代码:
#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;
}
UVA 718 - Skyscraper Floors(数论)的更多相关文章
- UVA 10627 - Infinite Race(数论)
UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...
- uva 10555 - Dead Fraction)(数论)
option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...
- uva 10560 - Minimum Weight(数论)
题目连接:uva 10560 - Minimum Weight 题目大意:给出n,问说至少须要多少个不同重量的砝码才干称量1~n德重量,给出所选的砝码重量,而且给出k,表示有k个重量须要用上述所选的砝 ...
- UVA 11754 - Code Feat(数论)
UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...
- uva 10692 - Huge Mods(数论)
题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...
- UVA 12009 - Avaricious Maryanna(数论)
UVA 12009 - Avaricious Maryanna 题目链接 题意:给定一个n.求出n个数位组成的数字x,x^2的前面|x|位为x 思路:自己先暴力打了前几组数据,发现除了1中有0和1以外 ...
- uva 11728 - Alternate Task(数论)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36409469 option=com_onli ...
- UVa 1393 - Highways(数论)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 11105 - Semi-prime H-numbers(数论)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36644069 option=com_onli ...
随机推荐
- Node.js学习笔记3(快速入门)
一.开始使用Node.js编程 1.hello world 好了,让我们开始实现第一个 Node.js 程序吧.打开你常用的文本编辑器,在其中输入 ...
- Linux学习:curl 与 wget命令
curl和wget命令都是Linux下的工具,可以用来下载文件. 一.wget 例1: wget http://www.minjieren.com/wordpress-3.1-zh_CN.zip 下载 ...
- MAC Eclipse 快捷键
MAC Eclipse 快捷键大全备忘: Command + O:显示大纲 Command + 1:快速修复 Command + D:删除当前行 Command + Option + ↓:复制当前行到 ...
- 自定义UISlider的样式和滑块
又做完一个项目,遂到了写帖时间了,要跟大家分享的是:自定义UISlider的图片和滑块,先看一下效果: 然后是实现了,我们使用的是UISlider的setMinimumTrackImage,和setM ...
- 简单仿京东导航下拉菜单 javascript
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...
- 如何设置gen_server在退出时执行相关操作
如果gen_server在监控树中不需要stop函数,gen_server会由其supervisor根据shutdown策略自动终止掉.如果要在进程终止之前执行清理,shutdown策略必须设定一个t ...
- php5.3升级到5.5
在网站中发布中: / 开启调试模式 建议开发阶段开启 部署阶段注释或者设为falsedefine('APP_DEBUG',true); true没问题,改为:false就报错 报错如下: PHP Fa ...
- USACO Runaround Numbers 模拟
根据题意的 Runaround 规则去找比当前数大的最近的一个 Runaround数字 模拟题~ Source code: /* ID: wushuai2 PROG: runround LANG: C ...
- USACO Ski Course Design 暴力
从Min到Max范围内暴力一下即可. /* ID: wushuai2 PROG: skidesign LANG: C++ */ //#pragma comment(linker, "/STA ...
- (Problem 14)Longest Collatz sequence
The following iterative sequence is defined for the set of positive integers: n n/2 (n is even) n 3n ...