結果

問題 No.426 往復漸化式
ユーザー tanzakutanzaku
提出日時 2016-09-23 02:13:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,995 ms / 5,000 ms
コード長 11,166 bytes
コンパイル時間 2,977 ms
コンパイル使用メモリ 90,968 KB
実行使用メモリ 251,412 KB
最終ジャッジ日時 2024-04-28 20:27:25
合計ジャッジ時間 39,293 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
51,588 KB
testcase_01 AC 123 ms
52,432 KB
testcase_02 AC 124 ms
53,604 KB
testcase_03 AC 258 ms
60,516 KB
testcase_04 AC 259 ms
49,772 KB
testcase_05 AC 737 ms
73,308 KB
testcase_06 AC 746 ms
83,408 KB
testcase_07 AC 1,722 ms
246,368 KB
testcase_08 AC 1,719 ms
246,264 KB
testcase_09 AC 2,013 ms
249,116 KB
testcase_10 AC 1,992 ms
249,184 KB
testcase_11 AC 1,824 ms
246,388 KB
testcase_12 AC 2,332 ms
246,456 KB
testcase_13 AC 2,364 ms
251,412 KB
testcase_14 AC 2,317 ms
246,840 KB
testcase_15 AC 2,134 ms
246,340 KB
testcase_16 AC 2,995 ms
246,352 KB
testcase_17 AC 2,778 ms
246,332 KB
testcase_18 AC 2,919 ms
246,504 KB
testcase_19 AC 1,295 ms
229,164 KB
testcase_20 AC 1,465 ms
245,892 KB
testcase_21 AC 1,576 ms
245,820 KB
testcase_22 AC 1,470 ms
245,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

import static java.util.Arrays.*;

public class Main {
	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 n = io.nextInt() + 1;
			a = new long[3][1];
			b = new long[2][1];
			for(int i = 0; i < a.length; i++) a[i][0] = io.nextLong();
			for(int i = 0; i < b.length; i++) b[i][0] = io.nextLong();
			int q = io.nextInt();
			
			Seg2 seg = new Seg2(n + 1);

			long[][] A = new long[3][3];
			long[][] B = new long[2][2];
			for(int qq = 0; qq < q; qq++) {
				char[] t = io.next();
				if(t[0] == 'a') {
					int ii = io.nextInt();
					for(int i = 0; i < 3; i++) {
						for(int j = 0; j < 3; j++) {
							A[i][j] = io.nextLong();
						}
					}
					seg.set(ii, ii + 1, A, null);
					continue;
				}
				if(t[0] == 'b') {
					int ii = io.nextInt();
					for(int i = 0; i < 2; i++) {
						for(int j = 0; j < 2; j++) {
							B[i][j] = io.nextLong();
						}
					}
					seg.set(ii, ii + 1, null, B);
					continue;
				}
				if(t[1] == 'a') {
					int ii = io.nextInt();
					long[][] a = seg.getAa(0, ii);
					for(int i = 0; i < 3; i++) io.out.print(a[i][0] + (i==2?"\n":" "));
				}
				if(t[1] == 'b') {
					int ii = io.nextInt();
					long[][] a = seg.getB(ii, n);
					for(int i = 0; i < 2; i++) io.out.print(a[i][0] + (i==1?"\n":" "));
				}
				io.out.flush();
			}
		}
	}

	static long[][] a;
	static long[][] b;
	static
	class Seg2 {
		final int n;
		final long[][][] segA;
		final long[][][] segB;
		final long[][][] segS;
		
		public Seg2(final int n) {
			this.n = Integer.highestOneBit(n) << 1;
			segS = new long[this.n << 1][2][3];
			segA = new long[this.n << 1][3][3];
			segB = new long[this.n << 1][2][2];
			set(0, n, getId(3), getId(2));
		}

		static long[][] zero2 = new long[2][1];
		static long[][] zero23 = new long[2][3];
		static long[][] id2 = getId(2);
		static long[][] id3 = getId(3);
		static long[][] getId(int n) {
			long[][] m = new long[n][n];
			for(int i = 0; i < n; i++) m[i][i] = 1;
			return m;
		}

		long[][] getAa(int l, int r) { return mulmat(getA(l, r, 0, 0, n), a, mod); }
		long[][] getA(int l, int r) { return getA(l, r, 0, 0, n); }
		long[][] getA(int l, int r, int k, int curL, int curR) {
			if(curR <= l || curL >= r) return id3;
			if(l <= curL && curR <= r) { return segA[k]; }
			final int curM = (curL + curR) / 2;
			return mulmat(
					getA(l, r, 2 * k + 2, curM, curR),
					getA(l, r, 2 * k + 1, curL, curM), mod);
		}

//		long[][] getB(int l, int r) { return addmat(mulmat(getBB(l+1, r), b, mod), getB(l+1, r, 0, 0, n), mod); }
		long[][] getB(int l, int r) {
			long[][] bb = mulmat(getBB(l+1, r), b, mod);
			long[][] aa = mulmat(mulmat(getS(l+1, r), getA(0, l+1), mod), a, mod);
			return addmat(aa, bb, mod);
		}
		long[][] getB(int l, int r, int k, int curL, int curR) {
			if(curR <= l || curL >= r) return zero2;
			if(l <= curL && curR <= r) { return mulmat(segS[k], mulmat(getA(0, k), a, mod), mod); }
			final int curM = (curL + curR) / 2;
			final int ll = Math.max(l, curL);
			return addmat(
					getB(l, r, 2 * k + 1, curL, curM),
					mulmat(getBB(ll, curM), getB(l, r, 2 * k + 2, curM, curR), mod), mod);
		}

		long[][] getBB(int l, int r) { return getBB(l, r, 0, 0, n); }
		long[][] getBB(int l, int r, int k, int curL, int curR) {
			if(curR <= l || curL >= r) return id2;
			if(l <= curL && curR <= r) { return segB[k]; }
			final int curM = (curL + curR) / 2;
			return mulmat(
					getBB(l, r, 2 * k + 1, curL, curM),
					getBB(l, r, 2 * k + 2, curM, curR), mod);
		}

		long[][] getS(int l, int r) { return getS(l, r, 0, 0, n); }
		long[][] getS(int l, int r, int k, int curL, int curR) {
			if(curR <= l || curL >= r) return zero23;
			if(l <= curL && curR <= r) { return segS[k]; }
			final int curM = (curL + curR) / 2;
			final int L = 2 * k + 1;
			final int R = 2 * k + 2;
			final long[][] lb = getBB(l, r, 2 * k + 1, curL, curM);
			final long[][] la = getA(l, r, 2 * k + 1, curL, curM);
			return addmat(mulmat(mulmat(lb, getS(l, r, R, curM, curR), mod), la, mod), getS(l, r, L, curL, curM), mod);
		}

		void set(int l, int r, long[][] A, long[][] B) {
			set(l, r, 0, 0, n, A, B);
		}
		void set(int l, int r, int k, int curL, int curR, long[][] A, long[][] B) {
			if(curR <= l || curL >= r) return;
			if(curR - curL == 1 && l <= curL && curR <= r) {
				if(A != null) {
					for(int i = 0; i < 3; i++) {
						for(int j = 0; j < 3; j++) {
							segA[k][i][j] = A[i][j];
						}
					}
				}
				if(B != null) {
					for(int i = 0; i < 2; i++) {
						for(int j = 0; j < 2; j++) {
							segB[k][i][j] = B[i][j];
						}
					}
				}
				long K = curL;
				long[][] c = new long[][]{
					new long[]{6*K+0,6*K+1,6*K+2,},
					new long[]{6*K+3,6*K+4,6*K+5,}};
				segS[k] = c;
				return;
			}
			if(curR - curL == 1) throw new RuntimeException();
			
			final int curM = (curL + curR) / 2;
			set(l, r, 2 * k + 1, curL, curM, A, B);
			set(l, r, 2 * k + 2, curM, curR, A, B);

			final int L = 2 * k + 1;
			final int R = 2 * k + 2;
			if(A != null) segA[k] = mulmat(segA[R], segA[L], mod);
			if(B != null) segB[k] = mulmat(segB[L], segB[R], mod);
			segS[k] = addmat(mulmat(mulmat(segB[L], segS[R], mod), segA[L], mod), segS[L], mod);
		}
	}

	static long[][] addmat(long[][] a, long[][] b, int mod) {
		assert(a.length == b.length);
		assert(a[0].length == b[0].length);
		
		final int n = a.length;
		final int m = b[0].length;

		long[][] res = new long[n][m];
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) {
				res[i][j] = a[i][j] + b[i][j];
				if(res[i][j] >= mod) res[i][j] -= mod;
			}
		
		return res;
	}

	static long[][] mulmat(long[][] a, long[][] b, int mod) {
		final long BIG = (2L * mod) * (2L * mod);
		
		assert(a[0].length == b.length);
		
		final int n = a.length;
		final int v = b.length;
		final int m = b[0].length;

		long[][] res = new long[n][m];
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) {
				res[i][j] = 0;
				for(int k = 0; k < v; k++) {
					res[i][j] += a[i][k] * b[k][j];
					if(res[i][j] >= BIG) res[i][j] -= BIG;
				}
				res[i][j] %= mod;
			}
		
		return res;
	}
	
	/// 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; }
	
	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 Main().main(); }
	
	static class EndOfFileRuntimeException extends RuntimeException {
		private static final long serialVersionUID = -8565341110209207657L; }

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

		void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); }
		void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); }
		void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); }

		private static int pos, readLen;
		private static final char[] buffer = new char[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 int 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 { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int 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 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 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