結果

問題 No.620 ぐるぐるぐるりん
ユーザー uwiuwi
提出日時 2017-12-19 12:00:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 446 ms / 1,000 ms
コード長 7,632 bytes
コンパイル時間 4,535 ms
コンパイル使用メモリ 90,444 KB
実行使用メモリ 58,724 KB
最終ジャッジ日時 2024-06-13 02:04:23
合計ジャッジ時間 8,967 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,596 KB
testcase_01 AC 57 ms
50,500 KB
testcase_02 AC 56 ms
50,608 KB
testcase_03 AC 54 ms
50,448 KB
testcase_04 AC 54 ms
50,620 KB
testcase_05 AC 54 ms
50,708 KB
testcase_06 AC 54 ms
50,444 KB
testcase_07 AC 54 ms
50,312 KB
testcase_08 AC 56 ms
50,644 KB
testcase_09 AC 54 ms
50,492 KB
testcase_10 AC 54 ms
50,132 KB
testcase_11 AC 54 ms
50,656 KB
testcase_12 AC 55 ms
50,640 KB
testcase_13 AC 55 ms
50,544 KB
testcase_14 AC 55 ms
50,440 KB
testcase_15 AC 55 ms
50,596 KB
testcase_16 AC 56 ms
50,272 KB
testcase_17 AC 57 ms
50,308 KB
testcase_18 AC 55 ms
50,272 KB
testcase_19 AC 58 ms
50,656 KB
testcase_20 AC 57 ms
50,552 KB
testcase_21 AC 55 ms
50,460 KB
testcase_22 AC 54 ms
50,580 KB
testcase_23 AC 56 ms
50,508 KB
testcase_24 AC 54 ms
50,520 KB
testcase_25 AC 54 ms
50,272 KB
testcase_26 AC 446 ms
58,636 KB
testcase_27 AC 54 ms
50,416 KB
testcase_28 AC 261 ms
58,144 KB
testcase_29 AC 256 ms
57,972 KB
testcase_30 AC 266 ms
58,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package adv2017;
import java.awt.geom.Point2D;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

// formatしないやつ
public class N620_4 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int U = ni();
		if(!(1 <= U && U <= 1000))throw new AssertionError("U");
		for(;U > 0;U--){
			int T = ni();
			double P = nd();
			double omega = nd();
			double v = nd();
			double gx = nd();
			double gy = nd();
			if(!(1 <= T && T <= 15))throw new AssertionError("T");
			if(!(0 <= P && P <= 1e9))throw new AssertionError("P");
			if(!(-10 <= omega && omega <= 10))throw new AssertionError("omega");
			if(!(-10 <= v && v <= 10))throw new AssertionError("v");
			if(!(-100 <= gx && gx <= 100))throw new AssertionError("gx");
			if(!(-100 <= gy && gy <= 100))throw new AssertionError("gy");
			/*
	(1+v -ω ux)
	(ω 1+v uy)
	(0 0 1)
			 * 
			 */
			double[][] M = {
					{1+v, -omega},
					{omega, 1+v}
			};
			
			double[][] E = {
					{1, 0},
					{0, 1}
			};
			
			double[][][] MS = new double[T+1][][];
			MS[0] = E;
			for(int i = 1;i <= T;i++){
				MS[i] = mul(MS[i-1], M);
			}
			
			double[][] Q = new double[2*T+2][2*T+2];
			for(int i = 0;i < 2*T;i++){
				Q[i][i] = 1;
			}
			// Q = I
			// c = 0
			// E = MS[T-1] MS[T-2] MS[T-3] ...
			for(int i = 0;i < T;i++){
				for(int j = 0;j < 2;j++){
					for(int k = 0;k < 2;k++){
						Q[2*i+k][2*T+j] = Q[2*T+j][2*i+k] = MS[T-1-i][j][k];
					}
				}
			}
			double[] d = new double[2*T+2];
			d[2*T] = gx - MS[T][0][0];
			d[2*T+1] = gy - MS[T][1][0];
			
			double[] x = solve(Q, d);
			
//			check(omega, v, x, T, gx, gy, P);
			
			for(int i = 0;i < 2*T;i+=2){
				out.print(dtos(x[i], 14));
				out.print(" ");
				out.println(dtos(x[i+1], 14));
//				out.printf("%.14f %.14f\n", x[i], x[i+1]);
			}
		}
	}
	
	public static String dtos(double x, int n) {
		StringBuilder sb = new StringBuilder();
		if(x < 0){
			sb.append('-');
			x = -x;
		}
		x += Math.pow(10, -n)/2;
//		if(x < 0){ x = 0; }
		sb.append((long)x);
		sb.append(".");
		x -= (long)x;
		for(int i = 0;i < n;i++){
			x *= 10;
			sb.append((int)x);
			x -= (int)x;
		}
		return sb.toString();
	}

	
	static void check(double omega, double V, double[] x, double T, double gx, double gy, double P)
	{
		double[] v = new double[]{1, 0, 1};
		double used = 0;
		for(int i = 0;i < T;i++){
			used += x[i*2]*x[i*2] + x[i*2+1]*x[i*2+1];
			double[][] M = {
					{1+V, -omega, x[i*2]},
					{omega, 1+V, x[i*2+1]},
					{0, 0, 1}
			};
			v = mul(M, v);
		}
//		tr(used, P);
//		assert used <= P;
		tr(v, gx, gy);
		if(!(Point2D.distance(v[0], v[1], gx, gy) <= 1e-4))throw new RuntimeException();
	}
	
	public static double[] solve(double[][] a, double[] c)
	{
		int n = a.length;
		int[] ps = new int[n];
		for(int i = 0;i < n;i++)ps[i] = i;
		
		// Forward Elimination
		for(int i = 0;i < n;i++){
			int pivot = -1;
			int from = -1;
			double amax = 0;
			for(int j = i;j < n;j++){
				if(Math.abs(a[ps[j]][i]) > amax){
					amax = Math.abs(a[ps[j]][i]);
					pivot = ps[j];
					from = j;
				}
			}
			if(pivot == -1)return null;
			int d = ps[i]; ps[i] = ps[from]; ps[from] = d;
			
			for(int j = i+1;j < n;j++){
				a[ps[i]][j] /= a[ps[i]][i];
			}
			c[ps[i]] /= a[ps[i]][i];
			a[ps[i]][i] = 1.0;
			for(int j = i+1;j < n;j++){
				for(int k = i+1;k < n;k++){
					a[ps[j]][k] -= a[ps[j]][i] * a[ps[i]][k];
				}
				c[ps[j]] -= a[ps[j]][i] * c[ps[i]];
				a[ps[j]][i] = 0.0;
			}
			
		}
		
		// Back Substitution
		for(int i = n-1;i >= 0;i--){
			for(int j = i-1;j >= 0;j--){
				c[ps[j]] -= a[ps[j]][i] * c[ps[i]];
			}
		}
		
		double[] ret = new double[n];
		for(int i = 0;i < n;i++){
			ret[i] = c[ps[i]];
		}
		
		return ret;
	}

	
	public static double[][] mul(double[][] A, double[][] B)
	{
		assert A[0].length == B.length;
		int m = A.length;
		int n = A[0].length;
		int o = B[0].length;
		double[][] C = new double[m][o];
		for(int i = 0;i < m;i++){
			for(int k = 0;k < n;k++){
				for(int j = 0;j < o;j++){
					C[i][j] += A[i][k] * B[k][j];
				}
			}
		}
		return C;
	}
	

	
	public static double[] mul(double[][] A, double[] v)
	{
		int m = A.length;
		int n = v.length;
		double[] w = new double[m];
		for(int i = 0;i < m;i++){
			double sum = 0;
			for(int k = 0;k < n;k++){
				sum += A[i][k] * v[k];
			}
			w[i] = sum;
		}
		return w;
	}
	
	public static double[][] p2(double[][] A)
	{
		int n = A.length;
		double[][] C = new double[n][n];
		for(int i = 0;i < n;i++){
			for(int k = 0;k < n;k++){
				for(int j = 0;j < n;j++){
					C[i][j] += A[i][k] * A[k][j];
				}
			}
		}
		return C;
	}
	
	// A^e*v
	public static double[] pow(double[][] A, double[] v, long m)
	{
		double[][] mu = A;
		double[] r = v;
		for(;m > 0;m>>>=1){
			if((m&1)==1)r = mul(mu, r);
			mu = p2(mu);
		}
		return r;
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new N620_4().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0