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 ...
随机推荐
- JDBC_获取数据库连接
<span style="font-size:24px;">package src.com.jdbc.java; import java.io.IOException; ...
- MySql 初次安装登陆
名称:随便写 服务器:127.0.0.1或者localhost 端口:在安装mysql应该看到是3306 用户:root 密码:(默认的是空,如果你设置过自己应该知道) 其他就可以不用设置
- centos安装epel源
用163的源,但是我发现这个源里面,根本没有libmcrypt libmcrypt-devel这二个包,装php扩展mcrypt时,又要用到这二个包,所以我手动装了libmcrypt包,但是给我的感觉 ...
- ODBC、OLE DB、 ADO、ODAC、ODP.NET
面对各式各样.越来越多的数据来源和访问需求.软件开发框架中一般都提供了统一的访问接口和方法,来屏蔽数据库底层差异. 各式各样的Provider提供者. ODBC(Open Database Conne ...
- Flask中路由模块的实现
在Flask中的路由功能主要通过修饰函数route实现,下面我们就来挖掘下route在源代码中是怎么分配视图函数的. def route(self, rule, **options): def dec ...
- EAN-13 条码(又称GTIN-13 条码)
EAN全名为European Article Number(欧洲商品条码),在1977年时由欧洲几个主要工业国家共同发展出来的,后来变成国际商品条码系统.台湾在1985年加入EAN会员,现在我们买东西 ...
- 生成唯一32位ID编码代码Java(GUID)
源码下载链接:http://pan.baidu.com/s/1jGCEWlC 扫扫关注"茶爸爸"微信公众号 坚持最初的执着,从不曾有半点懈怠,为优秀而努力,为证明自己而活. /* ...
- 转:30分钟掌握STL
三十分钟掌握STL 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把它翻译出来.我没有对翻译出来的内容校验过.如果你没法在三十分钟内觉得有 ...
- 「操作系统」: Conditional Move Instructions(trap)
Not all conditional expressions can be compiled using conditional moves. Most significantly, the abs ...
- Binary Tree(二叉树+思维)
Binary Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...