結果

問題 No.471 直列回転機
ユーザー tanzakutanzaku
提出日時 2016-12-21 00:39:06
言語 Java19
(openjdk 21)
結果
AC  
実行時間 654 ms / 3,141 ms
コード長 8,733 bytes
コンパイル時間 6,997 ms
コンパイル使用メモリ 80,660 KB
実行使用メモリ 74,212 KB
平均クエリ数 19589.39
最終ジャッジ日時 2023-09-02 03:41:11
合計ジャッジ時間 30,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
66,940 KB
testcase_01 AC 117 ms
67,064 KB
testcase_02 AC 116 ms
67,420 KB
testcase_03 AC 117 ms
67,752 KB
testcase_04 AC 115 ms
69,400 KB
testcase_05 AC 137 ms
69,936 KB
testcase_06 AC 120 ms
69,380 KB
testcase_07 AC 135 ms
69,896 KB
testcase_08 AC 220 ms
70,632 KB
testcase_09 AC 220 ms
72,464 KB
testcase_10 AC 181 ms
70,624 KB
testcase_11 AC 316 ms
72,960 KB
testcase_12 AC 511 ms
73,000 KB
testcase_13 AC 520 ms
72,564 KB
testcase_14 AC 556 ms
72,832 KB
testcase_15 AC 654 ms
74,212 KB
testcase_16 AC 126 ms
68,056 KB
testcase_17 AC 115 ms
69,816 KB
testcase_18 AC 112 ms
69,432 KB
testcase_19 AC 112 ms
69,404 KB
testcase_20 AC 569 ms
73,288 KB
testcase_21 AC 240 ms
73,100 KB
testcase_22 AC 525 ms
70,844 KB
testcase_23 AC 487 ms
73,132 KB
testcase_24 AC 351 ms
73,004 KB
testcase_25 AC 238 ms
72,336 KB
testcase_26 AC 552 ms
71,304 KB
testcase_27 AC 338 ms
73,368 KB
testcase_28 AC 380 ms
71,204 KB
testcase_29 AC 412 ms
73,288 KB
testcase_30 AC 272 ms
72,628 KB
testcase_31 AC 573 ms
72,588 KB
testcase_32 AC 496 ms
69,192 KB
testcase_33 AC 417 ms
72,884 KB
testcase_34 AC 553 ms
71,872 KB
testcase_35 AC 254 ms
72,400 KB
testcase_36 AC 310 ms
72,912 KB
testcase_37 AC 208 ms
72,592 KB
testcase_38 AC 249 ms
72,312 KB
testcase_39 AC 296 ms
72,908 KB
testcase_40 AC 437 ms
72,636 KB
testcase_41 AC 580 ms
71,312 KB
testcase_42 AC 413 ms
72,948 KB
testcase_43 AC 533 ms
70,760 KB
testcase_44 AC 199 ms
68,368 KB
testcase_45 AC 404 ms
73,152 KB
testcase_46 AC 438 ms
71,036 KB
testcase_47 AC 547 ms
73,292 KB
testcase_48 AC 483 ms
71,268 KB
testcase_49 AC 464 ms
70,940 KB
testcase_50 AC 537 ms
72,920 KB
testcase_51 AC 296 ms
70,996 KB
testcase_52 AC 137 ms
67,540 KB
testcase_53 AC 128 ms
69,632 KB
testcase_54 AC 132 ms
67,720 KB
testcase_55 AC 259 ms
72,432 KB
testcase_56 AC 249 ms
72,536 KB
testcase_57 AC 219 ms
70,588 KB
testcase_58 AC 185 ms
70,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.math.*;
import java.util.*;

import static java.util.Arrays.*;

public class _471 {
	private static final int mod = (int)1e9+7;

	final Random random = new Random(0);
	final IOFast io = new IOFast();

	/// MAIN CODE
	public void run() throws IOException {
//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
		int TEST_CASE = 1;
		while(TEST_CASE-- != 0) {
			int m = io.nextInt();
			int[][] xy = io.nextIntArray2D(m, 2);

			double[][] xs = new double[3][1];
			double[][] ys = new double[3][1];
			double[][] mat = new double[3][3];
			for (int i = 0; i < 3; i++) {
//				mat[i][0] = random.nextInt(10000*2)-10000;
//				mat[i][1] = random.nextInt(10000*2)-10000;
				if (i == 0) { mat[i][0] = 0; mat[i][1] = 0; }
				if (i == 1) { mat[i][0] = 1; mat[i][1] = 0; }
				if (i == 2) { mat[i][0] = 1; mat[i][1] = 1; }
				mat[i][2] = 1;
				io.out.println("? " + (int)mat[i][0] + " " + (int)mat[i][1]);
				io.out.flush();
				xs[i][0] = io.nextInt();
				ys[i][0] = io.nextInt();
			}
			double[][] m1 = mulmat(inverse(mat), xs);
			double[][] m2 = mulmat(inverse(mat), ys);
			double[][] A = new double[][]{
				new double[]{ m1[0][0], m1[1][0], m1[2][0], },
				new double[]{ m2[0][0], m2[1][0], m2[2][0], },
			};

//			dump(m1);
//			dump(m2);
//			dump(A);
//			dump(mat);

			io.out.println("!");
			for (int[] p : xy) {
				double[][] v = new double[][]{
					new double[]{p[0]},
					new double[]{p[1]},
					new double[]{1}};
				double[][] transformed = mulmat(A, v);
//				dump(A, v, transformed);
				long x = (long)(transformed[0][0]);
				long y = (long)(transformed[1][0]);
				io.out.println(x + " " + y);
			}
			io.out.flush();
		}
	}

	static <T> void swapRow(T[] ary, int i, int j) {
		final T x = ary[i];
		ary[i] = ary[j];
		ary[j] = x;
	}
	static boolean inverseProcess(double[][] tmp) {
		final int n = tmp.length;
		for(int i = 0; i < n; i++) {
			int r = i;
			for(int j = i + 1; j < n; j++) {
				if(Math.abs(tmp[r][i]) < Math.abs(tmp[j][i])) {
					r = j;
				}
			}
			
			if(i != r) {
				swapRow(tmp, i, r);
			}
			
			// rankが・・・
			if(Math.abs(tmp[i][i]) <= 1e-10) {
				return false;
			}
			
			for(int j = tmp[i].length - 1; j >= i; j--) {
				tmp[i][j] /= tmp[i][i];
			}
			
			for(int j = 0; j < n; j++) if(i != j) {
				for(int k = tmp[j].length - 1; k >= i; k--) {
					tmp[j][k] -= tmp[j][i] * tmp[i][k];
				}
			}
		}
		return true;
	}
	// 逆行列を求める
	static double[][] inverse(double[][] m) {
		final int n = m.length;
		assert(n != 0 && m[0].length == n);
		final double[][] tmp = new double[n][2*n];
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				tmp[i][j] = m[i][j];
			}
			tmp[i][i + n] = 1;
		}
		
		inverseProcess(tmp);
		
		final double[][] ret = new double[n][n];
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				ret[i][j] = tmp[i][j + n];
			}
		}
		
		return ret;
	}
	// a [n,v] * b [v,m] => c[n,m]
	static double[][] mulmat(double[][] a, double[][] b) {
		assert(a[0].length == b.length);
		
		final int n = a.length;
		final int v = b.length;
		final int m = b[0].length;
		
		double[][] res = new double[n][m];
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				for(int k = 0; k < v; k++)
				res[i][j] += a[i][k] * b[k][j];
		
		return res;
	}
	
	static double[][] powmat(int r, int n, double[][] mat) {
		double[][] x = new double[n][n];
		for(int i = 0; i < n; i++) {
			x[i][i] = 1;
		}
		
		for(;r > 0; r >>= 1) {
			if((r&1) == 1) {
				x = mulmat(x,mat);
			}
			mat = mulmat(mat,mat);
		}

		return x;
	}
	
	
	/// TEMPLATE
	static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
	static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }
	
	static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; }
	static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; }

	void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); }
	
	static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } 
	
	void main() throws IOException {
		//		IOFast.setFileIO("rle-size.in", "rle-size.out");
		try { run(); }
		catch (EndOfFileRuntimeException e) { }
		io.out.flush();
	}
	public static void main(String[] args) throws IOException { new _471().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

	static
	public class IOFast {
		private InputStream in = System.in;
		private PrintWriter out = new PrintWriter(System.out);

		private static int pos, readLen;
		private static final byte[] buffer = new byte[1024 * 8];
		private static char[] str = new char[500*8*2];
		private static boolean[] isDigit = new boolean[256];
		private static boolean[] isSpace = new boolean[256];
		private static boolean[] isLineSep = new boolean[256];

		static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; }
		public byte read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; }
		public int nextInt() throws IOException {
			byte first = (byte)nextChar();
			boolean minus = false;
			int res = 0;
			if (first == '-') {
				minus = true;
			} else {
				res = first - '0';
			}
			while (true) {
				byte b = read();
				if (isSpace[b]) break;
				res = res * 10 + b - '0';
			}
			return minus ? -res : res;
		}
		public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; }
		public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } }
		int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; }
		public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); }
		public String nextString() throws IOException { return new String(next()); }
		public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); }
//		public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; }
//		public double nextDouble() throws IOException { return Double.parseDouble(nextString()); }
		public double nextDouble() throws IOException {
			double res = nextChar() - '0';
			while (true) {
				byte c = read();
				if (isSpace[c]) return res;
				if (c == '.') break;
				res = res * 10 + c - '0';
			}
			double d = 1;
			while (true) {
				byte c = read();
//				dump(c);
				if (!isDigit[c]) break;
				res = res * 10 + c - '0';
				d *= 10;
			}
//			dump(1);
			return res / d;
		}
		public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; }
		public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; }
		public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; }
		public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; }
		public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; }
	}
}
0