結果
| 問題 |
No.471 直列回転機
|
| コンテスト | |
| ユーザー |
tanzaku
|
| 提出日時 | 2016-12-21 00:39:06 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 664 ms / 3,141 ms |
| コード長 | 8,733 bytes |
| コンパイル時間 | 4,561 ms |
| コンパイル使用メモリ | 91,108 KB |
| 実行使用メモリ | 65,092 KB |
| 平均クエリ数 | 19589.39 |
| 最終ジャッジ日時 | 2024-06-11 10:22:39 |
| 合計ジャッジ時間 | 28,007 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 58 |
ソースコード
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; }
}
}
tanzaku