数学(扩展欧几里得算法):HDU 5114 Collision
Matt is playing a naive computer game with his deeply loved pure girl.
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.
Input
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)
Output
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.
Sample Input
3
10 10
1 1 9 9
10 10
0 5 5 10
10 10
1 0 1 10
Sample Output
Case #1:
6.0 6.0
Case #2:
Collision will not happen.
Case #3:
6.0 5.0
这道题很有意思。
为了避免小数,所有数据*2。
这样想,分类讨论:
1.x轴坐标相等,y轴坐标相等:直接输出此点坐标。
2.只有一个轴坐标不等。
3.两轴坐标都不等。
设x1,x2为两点x坐标,x1>x2,第一次相遇时过了tx秒,交会在坐标xp,得到:
xp=n-(x1+tx-n),xp=x2+tx
可得
tx=n-(x1+x2)/2
同理 ty=m-(y1+y2)/2
若x1==x2或y1==y2,直接输出求出的ty或tx处理出的坐标即可。
否则是第三种情况:
由于x轴相遇周期是n秒,y轴是m秒,所以实际时间是
t=n-(x1+x2)/2+n*a,
t=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),b-k*(n/g) 这里k为任意整数,这个方法对原方程成立。
现在要求解满足n*a-m*b=(tb-ta)的非负最小解,可以直接a=a*(tb-ta)/g,a=a%(m/g),此后a为非负数,最小,且符合题意。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
LL ta,tb,x,y,tim;
int T,cas,n,m,x1,y1,x2,y2;
LL Exgcd(LL a,LL b,LL&x,LL&y){
if(b==){x=,y=;return a;}
LL ret=Exgcd(b,a%b,y,x);
y-=a/b*x;return ret;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d%d%d",&n,&m,&x1,&y1,&x2,&y2);
n*=;m*=;x1*=;y1*=;x2*=;y2*=;
ta=n-(x1+x2)/;tb=m-(y1+y2)/;
printf("Case #%d:\n",++cas);tim=-;
if(x1==x2&&y1==y2)tim=;
if(x1!=x2&&y1==y2)tim=ta;
if(x1==x2&&y1!=y2)tim=tb;
if(x1!=x2&&y1!=y2){
LL d=Exgcd(n,m,x,y);
if((tb-ta)%d==){
x=(tb-ta)/d*x;
x=(x%(m/d)+m/d)%(m/d);
tim=ta+n*x;
}
}
if(tim==-)
puts("Collision will not happen.");
else{
x1=(x1+tim)%(*n);y1=(y1+tim)%(*m);
if(x1>n)x1=*n-x1;if(y1>m)y1=*m-y1;
printf("%.1f %.1f\n",x1/2.0,y1/2.0);
}
}
return ;
}
数学(扩展欧几里得算法):HDU 5114 Collision的更多相关文章
- ****ural 1141. RSA Attack(RSA加密,扩展欧几里得算法)
1141. RSA Attack Time limit: 1.0 secondMemory limit: 64 MB The RSA problem is the following: given a ...
- 详解扩展欧几里得算法(扩展GCD)
浅谈扩展欧几里得(扩展GCD)算法 本篇随笔讲解信息学奥林匹克竞赛中数论部分的扩展欧几里得算法.为了更好的阅读本篇随笔,读者最好拥有不低于初中二年级(这是经过慎重考虑所评定的等级)的数学素养.并且已经 ...
- 扩展欧几里得算法(extgcd)
相信大家对欧几里得算法,即辗转相除法不陌生吧. 代码如下: int gcd(int a, int b){ return !b ? gcd(b, a % b) : a; } 而扩展欧几里得算法,顾名思义 ...
- noip知识点总结之--欧几里得算法和扩展欧几里得算法
一.欧几里得算法 名字非常高大上的不一定难,比如欧几里得算法...其实就是求两个正整数a, b的最大公约数(即gcd),亦称辗转相除法 需要先知道一个定理: gcd(a, b) = gcd(b, a ...
- 欧几里得算法与扩展欧几里得算法_C++
先感谢参考文献:http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html 注:以下讨论的数均为整数 一.欧几里得算法(重点是证 ...
- vijos1009:扩展欧几里得算法
1009:数论 扩展欧几里得算法 其实自己对扩展欧几里得算法一直很不熟悉...应该是因为之前不太理解的缘故吧这次再次思考,回看了某位大神的推导以及某位大神的模板应该算是有所领悟了 首先根据题意:L1= ...
- 浅谈扩展欧几里得算法(exgcd)
在讲解扩展欧几里得之前我们先回顾下辗转相除法: \(gcd(a,b)=gcd(b,a\%b)\)当a%b==0的时候b即为所求最大公约数 好了切入正题: 简单地来说exgcd函数求解的是\(ax+by ...
- (light oj 1306) Solutions to an Equation 扩展欧几里得算法
题目链接:http://lightoj.com/volume_showproblem.php?problem=1306 You have to find the number of solutions ...
- 『扩展欧几里得算法 Extended Euclid』
Euclid算法(gcd) 在学习扩展欧几里得算法之前,当然要复习一下欧几里得算法啦. 众所周知,欧几里得算法又称gcd算法,辗转相除法,可以在\(O(log_2b)\)时间内求解\((a,b)\)( ...
- 题解——洛谷P2613 【模板】有理数取余(扩展欧几里得算法+逆元)
题面 题目描述 给出一个有理数 c=\frac{a}{b} ,求 c mod19260817 的值. 输入输出格式 输入格式: 一共两行. 第一行,一个整数 \( a \) .第二行,一个整 ...
随机推荐
- Web 高性能开发汇总
1. Http服务器: 让Windows Server 2008+IIS 7+ASP.NET支持10万个同时请求 大规模网站架构实战之体系结构(一) 大规模网站架构之WEB加速器SQUID(二) ii ...
- 【Cardboard】 体验 - Google Cardboard DIY及完成后简单体验
体验 - Google Cardboard DIY及完成后简单体验 今年的Google I/O最让我感兴趣的除了Material Design以外就是这个Google Cardboard了.据说是Go ...
- C# 使用winForm的TreeView显示中国城镇四级联动
直接上代码吧,这里 MySql.Data.MySqlClient;需要到mysql官网下载mysql-connector-net-6.9.8-noinstall.zip 访问密码 6073 usi ...
- 进程、线程、GDI+、XML、委托
进制 表示某一位置上的数运算时是逢X进一位.二进制就是逢二进一, 十进制是逢十进一,十六进制是逢十六进一,以此类推. so:二进制001010101只有0和1计算机中的数据都是二进制表示,四进制以0. ...
- linux常用命令及安装软件命令
1.查看操作系统是33位还是64最简单的方法 getconf LONG_BIT 或者 uname -a 2.常用命令 2.1基本操作 clear 清屏 2.2安装命令 rpm(redhat packa ...
- 运行windows系统工具命令
appwiz.cpl 卸载/安装程序 wscui.cpl 操作中心 inetcpl.cpl 查看Internet属性 eventvwr 查看监视消息和疑难解答消息 taskmgr 任 ...
- Recommender Systems基于内容的推荐
基于内容的推荐的基本推荐思路是:用户喜欢幻想小说,这本书是幻想小说,则用户有可能喜欢这本小说 两方面要求:(1)知道用户的喜好:(2)知道物品的属性 基于内容的推荐相比协同过滤方法(个人观点):协同过 ...
- 在RedHat5.4 LINUX 安装mySQL数据库
linux下mysql 最新版安装图解教程 1. 查看当前安装的linux版本 通过上图中的数据可以看出安装的版本为RedHat5.4,所以我们需要下载RedHat5.4对应的mysql安装包
- Android中如何取消调转界面后EditText默认获取聚焦问题
最近在做一个项目,当我点击跳转至一个带有EditText的界面后,模拟器中的软键盘会自动弹出,严重影响了用户体验.在网上找了资料,现总结如下. 我们知道,EditText有一个 android:foc ...
- 转--利用函数模板技术,写一个简单高效的 JSON 查询器
http://www.cnblogs.com/index-html/archive/2012/07/18/js_select.html http://www.ibm.com/developerwork ...