[2016.7 test.5] T1

来源:互联网 发布:mac电脑兼容双系统吗 编辑:程序博客网 时间:2024/05/11 02:59

A
时间 1s
描述
给一个 W*H 的矩形,两个小球分别以 (1 , 1) 的方向在 (x1,y1) (x2,y2) 点开始运动,碰到
墙壁依照镜面反射。问是否能相遇如果相遇输出第一个相遇地点。
输入
六个数字代表 W,H,x1,y1,x2,y2
输出
一行 2 个浮点数(保留一位小数),空格隔开,表示第一次相遇的位置。
如果不相遇输出”No”不包括引号
样例


Input
10 10 1 1 9 9
Output
6.0 6.0
30%数据满足 W,H<=100
50%数据满足 W,H<=1000
100%数据满足 W,H<=10000

#include <iostream>#include <iomanip>#include <fstream>#include <stdlib.h>#include <time.h>#include<cstring>#include<cstdio>#include<vector>#include<string>#include<algorithm>#include <limits.h>#include<cmath>#include<map>#include<queue>#include<set>using namespace std;#define LL long long#define eps 1e-10const long long  mod =1000000007;  //这个必须是质数LL P,Q,a,x,b,y;int dcmp(double d){    return d < -eps ? -1 : d > eps;}LL extend_gcd(LL a,LL b,LL &x,LL &y){    if(b==0)    {        x=1;y=0;return a;    }    LL ans=extend_gcd(b,a%b,x,y);    LL tmp=x;    x=y;    y=tmp-(a/b)*y;    return ans;}int main(){    freopen("A.in","r",stdin);    freopen("A.out","w",stdout);    int T=1;    int tt=T;    while (T--){        scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&P,&Q,&a,&x,&b,&y);        if (a==b && x==y) {            printf("%.1f %.1f\n",(double)a,(double)x);continue;        }        if (a==b){            double t = Q - 1. * (x + y) / 2.;            double ans = a + t;            ans = fmod(ans , 2. * P);            if ((ans - P) >= 0) ans = 2. * P - ans;            printf("%.1f " , ans);            ans = y + t;            ans = fmod(ans , 2. * Q);            if ((ans - Q) >= 0) ans = 2. * Q- ans;            printf("%.1f\n" , ans);            continue;        }        if (x==y){            double t = P - 1. * (a + b) / 2.;            double ans = a + t;            ans = fmod(ans , 2. * P);            if ((ans - P) >= 0) ans = 2. * P - ans;            printf("%.1f " , ans);            ans = y + t;            ans = fmod(ans , 2. * Q);            if ((ans - Q) >= 0) ans = 2. * Q - ans;            printf("%.1f\n" , ans);            continue;        }        if (abs(a+b-x-y)%2==1 ) {            printf("No\n");continue;        }        LL g=(P-Q)-((a+b-x-y)/2);        LL x1,y1;        LL gcd=extend_gcd(P,Q,x1,y1);        if (g%gcd!=0) {            printf("No\n");continue;        }        x1*=g/gcd;        y1*=g/gcd;        if (x1>=0){            LL ha=Q/gcd;            LL hi=P/gcd;            LL f=x1/ha+1;            x1-=f*ha;y1+=f*hi;            if (x1+ha==0) {x1+=ha;y1-=hi;}        }else {            LL ha=Q/gcd;            LL hi=P/gcd;            LL f=-x1/ha;            x1-=f*ha;y1+=f*hi;        }        x1=-x1;        double t=P-(a+b)/2.0+x1*P;        double ansx,ansy;        if (t-(LL)t!=0.5) {            a+=(LL)t;a%=(2*P);if (a>P) a=2*P-a;ansx=a;            x+=(LL)t;x%=(2*Q);if (x>Q) x=2*Q-x;ansy=x;        }else {            a+=(LL)t;a%=(2*P);ansx=a+0.5;if (ansx>P) ansx=2*P-ansx;            x+=(LL)t;x%=(2*Q);ansy=x+0.5;if (ansy>Q) ansy=2*Q-ansy;        }        printf("%.1f %.1f\n",ansx,ansy);    }    return 0;}
1 0
原创粉丝点击