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)的速度走,碰到边界会反射,问你最终 ...
随机推荐
- 深入了解 Session 与 Cookie
Java —— 深入了解 Session 与 Cookie Java web 了解Cookie和Session 定义 1.很通俗的来讲,Cookie就是浏览器的缓存,就是用户访问网站时保存在浏 ...
- js:关闭当前页面
var userAgent = navigator.userAgent; if (userAgent.indexOf("Firefox") != -1 || userAgent.i ...
- js字符串的裁剪
一.JavaScript字符串的处理方法 1.split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=str. ...
- Documentation/filesystems/sysfs.txt 文档翻译--sysfs
sysfs - 用于导出内核对象的文件系统. 1.sysfs是一个基于ram的文件系统,最初基于ramfs. 它提供了一种方法,可以将内核数据结构,它们的属性以及它们之间的链接导出到用户空间.sysf ...
- ubuntu17.10安装LAMP并测试部署php探针系统
ubuntu17.10修改密码以及安装LAMP并部署php探针系统 步骤1:ubuntu17.10配置IP (这个版本配置IP方式改变较大,apt-get upgrade更新至最新以前配置方式也可以用 ...
- jquery on() bind()绑定的点击事件在js动态新添加的元素生效
方法一:$('.class').on("click",function(){……}); 相当于 $('.class').bind("click",functio ...
- 列表查询SQL语句改造
一个经常遇到到的场景,就是查询列表数据,列表数据由多张表构成 最简单的查询方法是先写一个查询单条数据的方法,比如这个方法中要查询十张表: 然后一个循环调用查单条的方法,这种逻辑上理解是比较简单的(因为 ...
- RDLC报表系列二
---------------------ReportServer数据库权限表说明------------------ --用户表 select * from Users --角色表 select * ...
- postman tests实例记录(还没看,一些常用的)
这段时间准备测试api接口,postman这个工具很是方便,特别是里面的tests的javascript脚本. 记录一下测试接口常用的tests验证的实例. 1.设置环境变量 postman.setE ...
- 给iOS开发新手送点福利,简述UIPageControl的属性和用法
UIPageControl 1. numberOfPages // 设置有多少页 默认为0 [pageControl setNumberOfPages:kImageCount]; 2. cur ...