結果

問題 No.62 リベリオン(Extra)
ユーザー 37zigen37zigen
提出日時 2020-04-16 21:13:51
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,066 bytes
コンパイル時間 2,513 ms
コンパイル使用メモリ 79,044 KB
実行使用メモリ 57,236 KB
最終ジャッジ日時 2024-10-02 10:24:58
合計ジャッジ時間 3,793 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
48,212 KB
testcase_01 AC 49 ms
36,628 KB
testcase_02 AC 189 ms
43,080 KB
testcase_03 AC 203 ms
43,076 KB
testcase_04 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
	
	long euclid(long x,long y,long a,long b) {
		return a==0?y:euclid(y-b/a*x,x,b%a,a);
	}
	
	long inv(long a,long m) {
		if (a%m==0) return 0;
		long v=euclid(1, 0, (a%m+m)%m, m);
		return v<0?v+m:v;
	}
	
	long gcd(long a,long b) {
		return a==0?Math.abs(b):gcd(b%a,a);
	}

	long lcm(long a,long b) {
		return a/gcd(a,b)*b;
	}
	
	long pow(long a,long n,long p) {
		return n!=0?(pow(a,n/2,p)*(n%2==1?a:1)):1;
	}
	
	void pre(long[] a,long[] m) {
		int n=a.length;
		for (int i=0;i<n;++i) {
			for (int j=i+1;j<n;++j) {
				long g=gcd(m[i],m[j]);
				if (gcd(m[i]/g,g)>1) {
					m[j]/=g;
					a[j]%=m[j];
				} else {
					m[i]/=g;
					a[i]%=m[i];
				}
				
			}
		}
	}
	
	long mul(long a,long b,long m) {
		a=(a%m+m)%m;b=(b%m+m)%m;
		if (a==0) return 0;
		if (a==1) return b%m;
		if (a%2==1) return (mul(a-1,b,m)+b)%m;
		return 2*mul(a/2,b,m)%m;
	}
	
	long garner(long[] a,long[] m) {
		pre(a,m);
		long ret=0,prd=1;
		for (int i=0;i<a.length;++i) {
			if (m[i]==1) continue;
			ret+=mul(inv(prd,m[i]),(a[i]-ret),m[i])*prd;
			prd=prd*m[i];
		}
		return ret<0?ret+prd:ret;
	}
	
	
	boolean solve(long W_,long H_,long D_,long dx_,long dy_,long sx_,long sy_,long vx_,long vy_) {
		long denominator=gcd(vx_,vy_);
		boolean ans=false;
		for (int i=0;i<=1;++i) {
			for (int j=0;j<=1;++j) {
				long W=W_,H=H_,vx=vx_,vy=vy_,sx=sx_,sy=sy_,dx=dx_,dy=dy_;
				vx/=denominator;vy/=denominator;
				long a=(W-sx+(i==0?1:(W-1))*dx)%W;
				long b=(H-sy+(j==0?1:(H-1))*dy)%H;
				if (a%gcd(vx,W)!=0) continue;
				if (b%gcd(vy,H)!=0) continue;
				if (vx==0&&sx!=dx) continue;
				if (vy==0&&sy!=dy) continue;
				long g;
				g=gcd(vx,W);
				a/=g;vx/=g;W/=g;
				g=gcd(vy,H);
				b/=g;vy/=g;H/=g;
				a=a*inv((vx%W+W)%W,W)%W;
				b=b*inv((vy%H+H)%H,H)%H;
				if (a%gcd(W,H)!=b%gcd(W,H)) continue;
				long[] mod={W,H};
				long[] target={a,b};
				long t=garner(target,mod);
				long dt=lcm(W/gcd(vx,W),H/gcd(vy,H));
				for (long T:new long[] {t,t+dt}) {
					if (T>D_*denominator) break;
					long sign_x=(vx_/denominator%(2*W_)*T%(2*W_)+sx_)%(2*W_);
					long sign_y=(vy_/denominator%(2*H_)*T%(2*H_)+sy_)%(2*H_);
					if (sign_x<0) sign_x-=W_;
					if (sign_y<0) sign_y-=H_;
					if (Math.abs(sign_x)/W_%2!=i) continue;
					if (Math.abs(sign_y)/H_%2!=j) continue;
					ans|=true;
				}
			}
		}
		return ans;
	}
	
	void run() {
		FastScanner sc=new FastScanner();
		PrintWriter pw=new PrintWriter(System.out);

		int Q=sc.nextInt();
		for (int q=0;q<Q;++q) {
			long W=sc.nextLong();
			long H=sc.nextLong();
			long D=sc.nextLong();
			long dx=sc.nextLong();
			long dy=sc.nextLong();
			long sx=sc.nextLong();
			long sy=sc.nextLong();
			long vx=sc.nextLong();
			long vy=sc.nextLong();
			boolean ans=solve(W,H,D,dx,dy,sx,sy,vx,vy);
			pw.println(ans?"Hit":"Miss");
		}
		pw.close();
	}
	
	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
	
    public static void main(String[] args) {
    	new Main().run();
    }
}




class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
    	return (int)nextLong();
    }
}
0