Collision

Time Limit: 15000/15000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 846    Accepted Submission(s): 200

Problem Description
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
The first line contains only one integer T which indicates the number of test cases.

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
For each test case, output “Case #x:” in the first line, where x is the case number (starting from 1). 

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
Hint
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,为了防止小数。
 当x1 = x2的时候,那么无论啥时候,x1=x2;那么这个时候,只要求y1=y2的时刻,
  ty =(m-(y2+(m-y1)))/2+m-y1 = (2*n-(y1+y2))/2;那么根据ty 可以求得坐标;
 同理y1=y2;
 那么当x1!=x2&&y1!=y2的时候,
 得到t1 = tx+k1*n;t2  =ty+k2*m;
  那么t2 = t1;所以解两个同余方程即可,取最小的t;
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<math.h>
7 #include<queue>
8 using namespace std;
9 typedef long long LL;
10 pair<LL,LL>exgcd(LL n,LL m);
11 LL solve(LL x, LL y,LL n,LL m);
12 LL gcd(LL n,LL m);
13 int main(void)
14 {
15 LL n,m,k;
16 int N;
17 int __ca = 0;
18 scanf("%d",&N);
19 while(N--)
20 {
21 scanf("%lld %lld",&n,&m);
22 LL x1,y1,x2,y2;
23 n*=2;
24 m*=2;
25 scanf("%lld %lld %lld %lld",&x1,&y1,&x2,&y2);
26 x1*=2;
27 y1*=2,x2*=2;
28 y2*=2;
29 printf("Case #%d:\n",++__ca);
30 if(x1==x2&&y1==y2)
31 {
32 printf("%.1f %.1f\n",x1/2,y1/2);
33 }
34 else if(x1==x2)
35 {
36 LL ty = (2*m-(y1+y2))/2;
37 LL yy = min(y1,y2)+ty;
38 LL xx = min(x1,x2)+ ty;
39 if((xx/n)%2==0)
40 {
41 xx = xx%n;
42 }
43 else
44 {
45 xx = ((n-xx)%n+n)%n;
46 if(xx == 0)xx = n;
47 }
48 if((yy/m)%2==0)
49 {
50 yy = yy%m;
51 }
52 else
53 {
54 yy = ((m-yy)%m+m)%m; if(yy == 0)yy = m;
55 }
56 printf("%.1lf %.1lf\n",xx/2.0,yy/2.0);
57 }
58 else if(y1 == y2)
59 {
60 LL tx = (2*n-(x1+x2))/2;
61 LL yy = min(y1,y2)+tx;
62 LL xx = min(x1,x2)+tx;
63 if((yy/m)%2==0)
64 {
65 yy = yy%m;
66 }
67 else
68 {
69 yy = ((m-yy)%m+m)%m;
70 if(yy == 0)yy = m;
71 }
72 if((xx/n)%2==0)
73 {
74 xx = xx%n;
75 }
76 else
77 {
78 xx = ((n-xx)%n+n)%n;
79 if(xx == 0)xx = n;
80 }
81 printf("%.1lf %.1lf\n",xx/2.0,yy/2.0);
82 }
83 else
84 {
85 LL ty = (2*m-(y1+y2))/2;
86 LL tx = (2*n-(x1+x2))/2;
87 LL ask = solve(tx,ty,n,m);
88 if(ask==1e18)
89 printf("Collision will not happen.\n");
90 else
91 {
92 LL yy = y1+ask;
93 if((yy/m)%2==0)
94 {
95 yy = yy%m;
96 //if(yy == 0)yy = m;
97 }
98 else
99 {
100 yy = ((m-yy)%m+m)%m;
101 if(yy == 0)yy = m;
102 }
103 LL xx = x1+ask;
104 if((xx/n)%2==0)
105 {
106 xx = xx%n;
107 }
108 else
109 {
110 xx = ((n-xx)%n+n)%n;
111 if(xx == 0)xx = n;
112 }
113 printf("%.1lf %.1lf\n",xx/2.0,yy/2.0);
114 }
115 }
116
117 }
118 return 0;
119 }
120 pair<LL,LL>exgcd(LL n,LL m)
121 {
122 if(m==0)
123 return make_pair(1,0);
124 else
125 {
126 pair<LL,LL>ak = exgcd(m,n%m);
127 return make_pair(ak.second,ak.first-(n/m)*ak.second);
128 }
129 }
130 LL solve(LL x, LL y,LL n,LL m)
131 {
132 LL cc = n;
133 LL c = x-y;
134 LL gc = gcd(n,m);
135 if(c%gc)return 1e18;
136 else
137 {
138 c/=gc;
139 n/=gc;
140 m/=gc;
141 pair<LL,LL>ak = exgcd(n,m);
142 LL x0 = (ak.first*c%m+m)%m;
143 LL lcm = (LL)m*cc;
144 x = x-cc*x0;
145 x = x%lcm+lcm;
146 x%=lcm;
147 return x;
148 }
149 }
150 LL gcd(LL n,LL m)
151 {
152 if(m==0)return n;
153 else return gcd(m,n%m);
154 }

Collision(hdu5114)的更多相关文章

  1. 1. md5 collision(50)

    md5 collision(50)      ------南京邮电大学ctf: http://chinalover.sinaapp.com/web19/ 发现了一串代码 <?php $md51 ...

  2. Core源码(二) Linq的Distinct扩展

    先贴源码地址 https://github.com/dotnet/corefx/tree/master/src/System.Linq/src .NET CORE很大一个好处就是代码的开源,你可以详细 ...

  3. 【Unity3D基础教程】给初学者看的Unity教程(四):通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider2D

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...

  4. CCNA网络工程师学习进程(2)基本的网络设备

      在组网技术中用到的设备有中继器(Repeater).集线器(Hub).网桥(Bridge).交换机(Switch).路由器(Router).分别工作在OSI参考模型中的物理层.数据链路层和网络层. ...

  5. 为什么带网格(mesh)的模型添加了刚体Rigidbody和MeshCollider,还是会从地板穿过去?

    两个Gameobject 放置在空中, 一个是Cube,一个是茄子模型 Cube的Collider 是Box Collider , 茄汁的Collider 是mesh collider, 他们都添加了 ...

  6. 【Unity3D基础教程】给初学者看的Unity教程(一):GameObject,Compoent,Time,Input,Physics

    作者:王选易,出处:http://www.cnblogs.com/neverdie/  欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点推荐.谢谢! Unity3D重要模块的类图 最近刚刚完成了一 ...

  7. [转载] 散列表(Hash Table)从理论到实用(上)

    转载自:白话算法(6) 散列表(Hash Table)从理论到实用(上) 处理实际问题的一般数学方法是,首先提炼出问题的本质元素,然后把它看作一个比现实无限宽广的可能性系统,这个系统中的实质关系可以通 ...

  8. [转载] 散列表(Hash Table) 从理论到实用(下)

    转载自: 白话算法(6) 散列表(Hash Table) 从理论到实用(下) [澈丹,我想要个钻戒.][小北,等等吧,等我再修行两年,你把我烧了,舍利子比钻戒值钱.] ——自扯自蛋 无论开发一个程序还 ...

  9. PHP学习笔记:对命名空间(namespace)学习资料的翻译

    Name collisions means: you create a function named db_connect, and somebody elses code that you use ...

随机推荐

  1. 生产调优1 HDFS-核心参数

    目录 1 HFDS核心参数 1.1 NameNode 内存生产配置 问题描述 hadoop-env.sh中配置 1.2 NameNode 心跳并发配置 修改hdfs-site.xml配置 1.3 开启 ...

  2. 从Redis分布式缓存实战入手到底层原理分析、面面俱到覆盖大厂面试考点

    概述 官方说明 Redis官网 https://redis.io/ 最新版本6.2.6 Redis中文官网 http://www.redis.cn/ 不过中文官网的同步更新维护相对要滞后不少时间,但对 ...

  3. day07 Linux配置修改

    day07 Linux配置修改 昨日回顾 1.系统目录 /etc :系统配置目录 /bin-> /usr/bin :保存常用命令的目录 /root :超级管理员目录 /home :普通管理员目录 ...

  4. mongDB进阶

    Mongo进阶 聚合 聚合操作将来自多个文档的值组合在一起,并且可以对分组数据执行各种操作以返回单个结果. 文档进入多阶段管道,将文档转换为聚合结果 聚合管道 例子: 第一阶段:过滤,$match 第 ...

  5. Linux学习 - Bash变量

    一.用户自定义变量(本地名) 用户自定义变量只有在当前的shell中生效 1 定义变量 name="zheng huiwei" aa=123 2 变量叠加 aa="$aa ...

  6. 接口测试 python+PyCharm 环境搭建

    1.配置Python环境变量 a:我的电脑->属性->高级系统设置->环境变量->系统变量中的PATH变量. 变量名:PATH      修改变量值为:;C:\Python27 ...

  7. 解决Spring MVC @ResponseBody出现问号乱码问题

    原因是SpringMVC的@ResponseBody使用的默认处理字符串编码为ISO-8859-1,而我们前台或者客户端的编码一般是UTF-8或者GBK.现将解决方法分享如下! 第一种方法: 对于需要 ...

  8. 痞子衡嵌入式:在i.MXRT1170上启动含DQS的Octal Flash可不严格设Dummy Cycle (以MT35XU512为例)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是Octal或Hyper Flash上DQS信号与Dummy Cycle联系. 关于在 i.MXRT 上启动 NOR Flash 时如何设 ...

  9. centos k3s部署

    目录 一.k3s介绍 二.在线安装 三.离线安装 四.高可用安装 五.配置k3s镜像仓库 六.Kubernetes 仪表盘 七.常用命令 八.参考 一.k3s介绍 1.k3s是一个轻量级的 Kuber ...

  10. Jenkins插件维护

    目录 一.简介 二.插件安装 在线安装插件 上传安装插件 从其它jenkins复制插件 配置插件加速器 一.简介 除了在线安装,还可以官网插件下载地址中进行下载安装,如果访问缓慢可以用清华镜像站. 二 ...