hdu5114 Collision (扩展欧几里德+思维)
来源:互联网 发布:轩墨宝宝 知乎 编辑:程序博客网 时间:2024/05/22 10:38
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.
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 ≤ 10 5)
The next line contains four integers x 1, y 1, x 2, y 2. The initial position of the two balls is (x 1, y 1) and (x 2, y 2). (0 ≤ x 1, x 2 ≤ x; 0 ≤ y 1, y 2 ≤ y)
In the second line, output “Collision will not happen.” (without quotes) if the collision will never happen. Otherwise, output two real numbers x c and y c, rounded to one decimal place, which indicate the position where the two balls will first collide.
310 101 1 9 910 100 5 5 1010 101 0 1 10
Case #1:6.0 6.0Case #2:Collision will not happen.Case #3:6.0 5.0
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <math.h>using namespace std;typedef long long LL;LL gcd(LL a,LL b){ return b?gcd(b,a%b):a;}void Ex_gcd(LL a, LL b, LL &x, LL &y){ if(b == 0)//递归出口 { x = 1; y = 0; return; } LL x1, y1; Ex_gcd(b, a%b, x1, y1); x = y1; y = x1-(a/b)*y1;}int main(){ LL x,y,x1,x2,y1,y2; int t; scanf("%d",&t); for(int cas=1;cas<=t;cas++) { scanf("%lld%lld",&x,&y); scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2); double ansx,ansy; printf("Case #%d:\n",cas); if(x1==x2&&y1==y2) { ansx=x1;ansy=y1; } else if(x1==x2&&y1!=y2) { if(y1>y2)swap(y1,y2); double t=2*y-y1-y2; ansx=t/2+x1; ansy=t/2+y1; } else if(x1!=x2&&y1==y2) { if(x1>x2)swap(x1,x2); double t=2*x-x1-x2; ansx=t/2+x1; ansy=t/2+y1; } else { x*=2;y*=2;x1*=2;x2*=2;y1*=2;y2*=2;///用扩欧要保证整数,所有数据乘2 LL z=gcd(x,y); LL quanx,quany; LL c=(2*y-y1-y2-2*x+x1+x2)/2; if(c%z!=0){printf("Collision will not happen.\n");continue;} else { LL xx=x/z,yy=y/z; c/=z; Ex_gcd(xx,yy,quanx,quany);///用扩欧要保证整数,所有数据乘2 quanx*=c; quanx=(quanx%yy+yy)%yy;///最小正整数解 LL shijian=quanx*x+(2*x-x1-x2)/2;///两周期重合时间+从(X1,Y1)到重合点时间 ansx=(x1+shijian)%(2*x);///一去一回2x算一个周期 ansy=(y1+shijian)%(2*y); //printf(" time= %d %.1lf %.1lf\n",shijian,ansx,ansy); if(ansx > x) ansx = 2*x - ansx;///超过边界对称回来 if(ansy > y) ansy= 2*y - ansy; ansx/=2;ansy/=2; } } printf("%.1lf %.1lf\n",ansx,ansy); } return 0;}
阅读全文
0 0
- hdu5114 Collision (扩展欧几里德+思维)
- hdu5114 Collision [模拟]
- hdu5114 Collision(一元同余方程)
- 欧几里德和扩展欧几里德
- 欧几里德, 与 扩展欧几里德
- 欧几里德|扩展欧几里德
- 欧几里德&&扩展欧几里德详解
- 欧几里德&&扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德
- 扩展欧几里德!!!!!
- 【20170630】TensorFlow与python学习(2)-Mnist入门
- c语言之宏定义
- Pluraform Floating Park 嵌入场所
- Springboot整合druid的具体过程
- 子数组最大累加和+子矩阵的最大和+子数组累加和为给定值的最大子数组长度
- hdu5114 Collision (扩展欧几里德+思维)
- 100多个经典常用的PHP功能插件大全实例演示和下载
- Ignite 消息和事件
- lua table简单使用
- 4个数和为0
- 2010 成都Regional H 博弈,概率dp
- 白话讲MyIsam和InnoDB的区别
- 面向过程和面向对象的二级菜单转变
- Golang 如何在切片尾部追加元素,又如何删除切片指定索引的元素,以及如何在切片中间插入元素,还有切片之间的复制