結果

問題 No.675 ドットちゃんたち
ユーザー uwiuwi
提出日時 2018-04-13 22:49:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 5,972 bytes
コンパイル時間 4,308 ms
コンパイル使用メモリ 82,740 KB
実行使用メモリ 65,440 KB
最終ジャッジ日時 2023-09-09 04:43:41
合計ジャッジ時間 8,831 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
50,952 KB
testcase_01 AC 70 ms
51,076 KB
testcase_02 AC 70 ms
49,020 KB
testcase_03 AC 74 ms
50,816 KB
testcase_04 AC 73 ms
52,448 KB
testcase_05 AC 331 ms
64,696 KB
testcase_06 AC 380 ms
64,700 KB
testcase_07 AC 392 ms
65,040 KB
testcase_08 AC 399 ms
64,396 KB
testcase_09 AC 400 ms
65,276 KB
testcase_10 AC 410 ms
65,440 KB
testcase_11 AC 411 ms
64,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest180413;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class E {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		long px = nl(), py = nl();
		long[][] M = new long[3][3];
		for(int i = 0;i < 3;i++)M[i][i] = 1;
		
		int[][] qs = new int[n][];
		for(int i = 0;i < n;i++){
			int xy = ni();
			if(xy == 1){
				int w = ni();
				qs[i] = new int[]{1, w};
			}else if(xy == 2){
				int w = ni();
				qs[i] = new int[]{2, w};
			}else if(xy == 3){
				qs[i] = new int[]{3};
			}
		}
		
		long[][] anss = new long[n][];
		for(int i = 0;i < n;i++){
			int[] q = qs[n-1-i];
			long[][] LM = new long[3][3];
			if(q[0] == 1){
				LM[0][0] = 1;
				LM[1][1] = 1;
				LM[2][2] = 1;
				LM[0][2] = q[1];
			}else if(q[0] == 2){
				LM[0][0] = 1;
				LM[1][1] = 1;
				LM[2][2] = 1;
				LM[1][2] = q[1];
			}else{
				LM[0][1] = 1;
				LM[1][0] = -1;
				LM[2][2] = 1;
			}
			M = mul(M, LM);
			long[] v = mul(M, new long[]{px, py, 1});
			anss[n-1-i] = v;
		}
		for(long[] v : anss){
			out.println(v[0] + " " + v[1]);
		}
	}
	
	// double行列*ベクトル
	public static long[] mul(long[][] A, long[] v)
	{
		int m = A.length;
		int n = v.length;
		long[] w = new long[m];
		for(int i = 0;i < m;i++){
			long sum = 0;
			for(int k = 0;k < n;k++){
				sum += A[i][k] * v[k];
			}
			w[i] = sum;
		}
		return w;
	}
	
	public static long[][] p2(long[][] A)
	{
		int n = A.length;
		long[][] C = new long[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 long[] pow(long[][] A, long[] v, long m)
	{
		long[][] mu = A;
		long[] r = v;
		for(;m > 0;m>>>=1){
			if((m&1)==1)r = mul(mu, r);
			mu = p2(mu);
		}
		return r;
	}
	
	// 普通の累乗計算セット ここまで
	
	// ret[n]=A^(2^n)
	public static long[][][] generateP2(long[][] A, int n)
	{
		long[][][] ret = new long[n+1][][];
		ret[0] = A;
		for(int i = 1;i <= n;i++)ret[i] = p2(ret[i-1]);
		return ret;
	}
	
	// A[0]^e*v
	// A[n]=A[0]^(2^n)
	public static long[] pow(long[][][] A, long[] v, long e)
	{
		for(int i = 0;e > 0;e>>>=1,i++) {
			if((e&1)==1)v = mul(A[i], v);
		}
		return v;
	}
	
	// メモ累乗計算セット ここまで
	
	public static long[][] add(long[][] A, long[][] B)
	{
		int m = A.length;
		int n = A[0].length;
		long[][] C = new long[m][n];
		for(int i = 0;i < m;i++){
			for(int j = 0;j < n;j++){
				C[i][j] = A[i][j] + B[i][j];
			}
		}
		return C;
	}
	
	public static long[][] sub(long[][] A, long[][] B)
	{
		int m = A.length;
		int n = A[0].length;
		long[][] C = new long[m][n];
		for(int i = 0;i < m;i++){
			for(int j = 0;j < n;j++){
				C[i][j] = A[i][j] - B[i][j];
			}
		}
		return C;
	}
	
	public static long[][] mul(long[][] A, long[][] B)
	{
		assert A[0].length == B.length;
		int m = A.length;
		int n = A[0].length;
		int o = B[0].length;
		long[][] C = new long[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;
	}

	
	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 E().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