6-Collision-hdu5114(小球碰撞)
Collision
Time Limit: 15000/15000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 1623 Accepted Submission(s): 435
The playground is a rectangle with walls around. Two balls are put in different positions inside the rectangle. The balls are so tiny that their volume can be ignored. Initially, two balls will move with velocity (1, 1). When a ball collides with any side of the rectangle, it will rebound without loss of energy. The rebound follows the law of refiection (i.e. the angle at which the ball is incident on the wall equals the angle at which it is reflected).
After they choose the initial position, Matt wants you to tell him where will the two balls collide for the first time.
For each test case, the first line contains two integers x and y. The four vertices of the rectangle are (0, 0), (x, 0), (0, y) and (x, y). (1 ≤ x, y ≤ 105)
The next line contains four integers x1, y1, x2, y2. The initial position of the two balls is (x1, y1) and (x2, y2). (0 ≤ x1, x2 ≤ x; 0 ≤ y1, y2 ≤ y)
In the second line, output “Collision will not happen.” (without quotes) if the collision will never happen. Otherwise, output two real numbers xc and yc, rounded to one decimal place, which indicate the position where the two balls will first collide.
10 10
1 1 9 9
10 10
0 5 5 10
10 10
1 0 1 10
6.0 6.0
Case #2:
Collision will not happen.
Case #3:
6.0 5.0
In first example, two balls move from (1, 1) and (9, 9) both with velocity (1, 1), the ball starts from (9, 9) will rebound at point (10, 10) then move with velocity (−1, −1). The two balls will meet each other at (6, 6).
对我来说这不是水题!!!
为了避免小数,所有数据*2(后面会体会到的)。
这样想,分类讨论:
1.x轴坐标相等,y轴坐标相等:直接输出此点坐标。
2.只有一个轴坐标不等。
3.两轴坐标都不等。
设x1,x2为两点x坐标,x1>x2,第一次相遇时过了tx秒,交会在坐标xp,得到:
xp = n - (x1 + ta - n), xp = x2 + ta
可得
ta = n - (x1 + x2) / 2
同理 tb = m - (y1 + y2) / 2
若x1==x2或y1==y2,直接输出求出的tb或ta处理出的坐标即可。
否则是第三种情况:
由于x轴相遇周期是n秒,y轴是m秒,所以实际时间是
time = n - (x1 + x2) / 2 + n * a,
time = m - (y1 + y2) / 2 + m * b,
用Exgcd解出来,但是要保证a>=0,b>=0,并且a最小。
首先:设ta=n-(x1+x2)/2 , tb=m-(y1+y2)/2 , 问题即变为求解n*a-m*b=(tb-ta), Exgcd形式是n*a+m*b=(n,m)
设g=(n,m),如果(tb-ta)%g!=0说明无解,现在求出的a,b,可以演化出一堆解,形式如:a+k*(m/g), nb-k*(n/g) 这里k为任意整数,这个方法对原方程成立。
现在要求解满足n*a-m*b=(tb-ta)的非负最小解,可以直接a=a*(tb-ta)/g,a=a%(m/g),此后a为非负数,最小,且符合题意。
#include <iostream>
#include <cstdio>
using namespace std; long long gcd(long long a, long long b){
return b == 0 ? a : gcd(b, a % b);
}
void ex_gcd(long long a, long long b, long long &u, long long &v){
if(b == 0){
u = 1, v = 0;
return ;
}
ex_gcd(b, a%b, v, u);
v = v - a/b *u;
return ;
} int main(){
long long n, m, x1, y1, x2, y2;
long long t, ca = 0;
cin >> t; while(t--){
ca++;
scanf("%lld%lld%lld%lld%lld%lld", &n, &m, &x1, &y1, &x2, &y2);
printf("Case #%lld:\n", ca);
n *= 2; //都乘以2避免后面ta,tb出现小数
m *= 2;
x1 *= 2;
y1 *= 2;
x2 *= 2;
y2 *= 2;
long long time = 0;
long long ta = n - (x1 + x2)/2;
long long tb = m - (y1 + y2)/2;
if(x1 == x2 && y1 == y2){
time = 0;
}
else if(x1 == x2 && y1 != y2){
time = tb;
}
else if(y1 == y2 && x1 != x2){
time = ta;
}
else{
long long g = gcd(n, m);
if((ta - tb) % g != 0){
time = -1; //不会相撞
}
else{
long long a, b;
// n /= g;
// m /= g;
// (ta - tb) /= g;
ex_gcd(n / g, m / g, a, b);
a = a * (tb - ta) / g;
a = (a % (m / g) + (m / g)) % (m / g); //必须对(m/g)而不是对m,???
time = ta + n * a; //x和y运动时间是一样的
}
}
if(time == -1){
printf("Collision will not happen.\n");
}
else{
x1 = (x1 + time) % (2 * n); //利用周期化为合理范围
y1 = (y1 + time) % (2 * m);
if(x1 > n) x1 = 2 * n - x1; //保证在0~n范围内,可以画图示意一下
if(y1 > m) y1 = 2 * m - y1;
printf("%.1lf %.1lf\n", x1 / 2.0, y1 / 2.0);
}
} return 0;
}
6-Collision-hdu5114(小球碰撞)的更多相关文章
- HTML5 Canvas彩色小球碰撞运动特效
脚本简介 HTML5 Canvas彩色小球碰撞运动特效是一款基于canvas加面向对象制作的运动小球动画特效. 效果展示 http://hovertree.com/texiao/html5/39/ ...
- js实现多个小球碰撞
实现思路:小球的移动,是通过改变小球的left和top值来改变,坐标分别为(x,y)当x/y值加到最大,即加到父级的宽度或者高度时,使x值或者y值减小,同理当x值或者y值减到最小时,同样的使x值或者y ...
- Canvas+Js制作动量守恒的小球碰撞
目的:通过js实现小球碰撞并实现动量守恒 canvas我们就不多说了,有用着呢. 我们可以通过canvas画2D图形(圆.方块.三角形等等)3D图形(球体.正方体等待). 当然这只是基础的皮毛而已,c ...
- js实现小球碰撞游戏
效果图: 效果图消失只是截的gif图的问题 源码: <!DOCTYPE html> <html lang="en"> <head> <m ...
- 【h5游戏开发】egret引擎p2物理引擎 - 小球碰撞地面搞笑的物理现象
重力的方向和地面的问题 p2中默认的方向是从上到下,如果重力默认是正数的话,物体放到世界中是会从上面往下面飘的 p2中plane地面默认的方向是y轴的方向,而在p2中y轴的方向默认是从上往下 首先来看 ...
- uniapp中用canvas实现小球碰撞的小动画
uniapp 我就不想喷了,踩了很多坑,把代码贡献出来让大家少踩些坑. 实现的功能: 生成n个球在canvas中运动,相互碰撞后会反弹,反弹后的速度计算我研究过了,可以参考代码直接用 防止球出边框 防 ...
- 【鸡年大吉】,不知道写点啥,放个demo(小球碰撞)吧,有兴趣的看看
最初的想法是仿写win7的泡泡屏保效果,但是对于小球的斜碰问题一直没搞明白(如果你会这个,欢迎留言或者做个demo),所以只是简单处理了碰撞后的速度,有时候会看起来很搞笑~~~funny guy 话不 ...
- md5 collision(md5碰撞)之记录一些MD5值
md5 collision之记录一些MD5值 “Magic Hash”的PHP漏洞可以使得攻击者非法获取用户的账号信息. 漏洞原因: PHP以一种特定的方式处理被哈希的字符串,攻击者可以利用其 ...
- Collision (hdu-5114
题意:你有一个以(0, 0), (x, 0), (0, y), (x, y)为边界点的四边形,给你两个点从(x1, y1), (x2, y2)的点发射,以(1, 1)的速度走,碰到边界会反射,问你最终 ...
随机推荐
- 常量池之字符串常量池String.intern()
运行时常量池是方法区(PermGen)的一部分. 需要提前了解: 1. JVM内存模型. 2. JAVA对象在JVM中内存分配 常量池的好处 常量池是为了避免频繁的创建和销毁对象而影响系统性能,其实现 ...
- 6款实用的硬盘、SSD固态硬盘、U盘、储存卡磁盘性能测试工具
一.检测工具名称汇总 HDTune ATTO Disk Benchmark CrystalDiskMark AS SSD Benchmark Parkdale CrystalDiskInfo 二.各项 ...
- flask第二十九篇——一个例子+【更新内容通知】
请关注公众号:自动化测试实战 大家先自己写一下,船长写这个花了半个小时,因为我和大家一样,也是新手: 写一个页面如下,点击书名以后跳转到书的详情页 书的信息如下: books = [ { 'id': ...
- CH1807 Necklace
题意 背景 有一天,袁☆同学绵了一条价值连城宝石项链,但是,一个严重的问题是,他竟然忘记了项链的主人是谁!在得知此事后,很多人向☆同学发来了很多邮件,都说项链是自己的,要求他归还(显然其中最多只有一个 ...
- python print 控制台输出中文
在pycharm里面的控制台用print输出信息, 本意想输出中文, 但是实际上是u\xxxx. 可以用这种方式: print("%s " % cn_string)
- 发发牢骚,觉得走c#这条路,不该太浮躁。
发发牢骚,觉得走c#这条路,不该太浮躁.校园招聘结束了,腾讯,华为,百度,完美时空,网易,阿里,让我觉得.NET很受歧视.清一色的C/C++,JAVA,只有网易有一点.Net的,但是都是非核心的运维工 ...
- 随笔教程:FastAdmin 如何打开新的标签页
随笔教程:FastAdmin 如何打开新的标签页 FastAdmin 有弹窗功能有时候不能胜任所有情况,有一定局限性. 那这时候就需要在新的标签页打开页面. 在 FastAdmin 中打新的标签页很简 ...
- 让APK 成功在 Windows 运行并可以设置本地文件
让APK 成功在 Windows 运行并可以设置本地文件 安装 ARC Welder. 启动 ARC Welder 后选反apk 文件,下载 zip. 将 zip 解压修改 manifest.json ...
- 转HTTP协议 --- Cookie
转自:http://www.cnblogs.com/TankXiao/archive/2013/04/15/2848906.html Cookie是HTTP协议中非常重要的东西, 之前拜读了Fish ...
- redhat 防火墙禁止允许的IP链接指定的端口
编辑:iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp -s 192.168.4.3 --dport 80 -j A ...