結果

問題 No.62 リベリオン(Extra)
ユーザー 37zigen37zigen
提出日時 2020-04-17 01:42:57
言語 Java19
(openjdk 21)
結果
AC  
実行時間 727 ms / 5,000 ms
コード長 5,427 bytes
コンパイル時間 2,315 ms
コンパイル使用メモリ 76,660 KB
実行使用メモリ 58,104 KB
最終ジャッジ日時 2023-07-26 20:18:55
合計ジャッジ時間 4,273 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,448 KB
testcase_01 AC 43 ms
49,408 KB
testcase_02 AC 274 ms
57,860 KB
testcase_03 AC 278 ms
58,104 KB
testcase_04 AC 727 ms
57,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	
	long euclid(long x,long y,long a,long b,long m) {
		return a==0?y:euclid((y%m-mul(b/a,x,m)+m)%m,x,b%a,a,m);
	}
	
	long inv(long a,long m) {
		if (a%m==0) return 0;
		long v=euclid(1, 0, (a%m+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;
	}
	
	boolean pre(long[] a,long[] m) {
		int n=a.length;
		for (int i=0;i<n;++i) a[i]=(a[i]%m[i]+m[i])%m[i];
		for (int i=0;i<n;++i) {
			for (int j=0;j<n;++j) {
				if (i==j) continue;
				long g=gcd(m[i],m[j]);
				if (m[i]%g!=m[j]%g) return false;
				if (gcd(m[i]/g,g)>1) {
					m[j]/=g;
					a[j]%=m[j];
				} else {
					m[i]/=g;
					a[i]%=m[i];
				}
				
			}
		}
		return true;
	}
	
	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) {
		if (!pre(a,m)) return -1;
		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;
			for (int j=0;j<=i;++j) {
				assert ret%m[j]==a[j];
			}
			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);
				if (t==-1) continue;
				assert t%mod[0]==target[0];
				assert t%mod[1]==target[1];
				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=(mul(vx_/denominator%(2*W_),T,(2*W_))+sx_)%(2*W_);
					long sign_y=(mul(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