結果
問題 | No.61 リベリオン |
ユーザー | 37zigen |
提出日時 | 2020-04-16 07:21:32 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,590 bytes |
コンパイル時間 | 2,566 ms |
コンパイル使用メモリ | 78,660 KB |
実行使用メモリ | 43,020 KB |
最終ジャッジ日時 | 2024-10-01 19:09:56 |
合計ジャッジ時間 | 3,757 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
36,404 KB |
testcase_01 | AC | 57 ms
36,900 KB |
testcase_02 | AC | 57 ms
36,532 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 54 ms
36,600 KB |
ソースコード
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; } long garner(long[] a,long[] m) { long ret=0,prd=1; for (int i=0;i<a.length;++i) { if (m[i]==1) continue; ret+=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_) { boolean ans=false; long numerator=gcd(vx_,vy_); vx_/=numerator;vy_/=numerator; 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_; 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[] target={a/gcd(W,H),b/gcd(W,H),a%gcd(W,H)}; long[] mod={W/gcd(W,H),H/gcd(W,H),gcd(W,H)}; 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_*numerator) continue; long sign_x=(vx_*T+sx_); long sign_y=(vy_*T+sy_); 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(); } }