結果

問題 No.673 カブトムシ
ユーザー tanzakutanzaku
提出日時 2017-03-18 11:55:52
言語 Java21
(openjdk 21)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 3,464 bytes
コンパイル時間 3,674 ms
コンパイル使用メモリ 81,476 KB
実行使用メモリ 50,232 KB
最終ジャッジ日時 2024-06-30 00:48:36
合計ジャッジ時間 4,590 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
50,232 KB
testcase_01 AC 46 ms
49,688 KB
testcase_02 AC 46 ms
50,048 KB
testcase_03 AC 46 ms
49,628 KB
testcase_04 AC 47 ms
49,452 KB
testcase_05 AC 46 ms
50,020 KB
testcase_06 AC 46 ms
49,508 KB
testcase_07 AC 45 ms
49,968 KB
testcase_08 AC 47 ms
49,812 KB
testcase_09 AC 46 ms
49,860 KB
testcase_10 AC 46 ms
49,472 KB
testcase_11 AC 45 ms
49,952 KB
testcase_12 AC 48 ms
50,044 KB
testcase_13 AC 48 ms
49,988 KB
testcase_14 AC 49 ms
49,768 KB
testcase_15 AC 49 ms
49,876 KB
testcase_16 AC 49 ms
49,884 KB
testcase_17 AC 48 ms
50,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


public class _p767_Validate {
	public static void main(String[] args) throws IOException {
		new _p767_Validate().solve();
	}

	static int mod = (int)1e9+7;
	void solve() throws IOException {
		try (final InputValidator in = new InputValidator(System.in)) {
//		try (final Scanner in = new Scanner(System.in)) {
			long B = in.nextLong(1, 1_000000_000000L) % mod;
			in.space();
			long C = in.nextLong(1, 1_000000_000000L) % mod;
			in.space();
			long D = in.nextLong(1, 1_000000_000000L);
			in.newLine();
			in.eof();
			long[][] mat = new long[][]{
				new long[]{C,1,},
				new long[]{0,1,},
			};
			mat = powmat(D, mod, mat);
			long[][] x = new long[][]{
				new long[]{0,},
				new long[]{C,},
			};
			long v = mulmat(mat, x, mod)[0][0];
			System.out.println(B * v % mod);
		}
	}

	// long
	// a [n,v] * b [v,m] => c[n,m]
	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 k = 0; k < v; k++) {
				final long aa = a[i][k];
				for(int j = 0; j < m; j++) {
					res[i][j] += aa * b[k][j];
					if(res[i][j] >= BIG) res[i][j] -= BIG;
				}
			}
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				res[i][j] %= mod;
		
		return res;
	}
	
	static long[][] powmat(long r, int mod, long[][] mat) {
		final int n = mat.length;
		long[][] x = new long[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, mod);
			}
			mat = mulmat(mat, mat, mod);
		}

		return x;
	}
	
	// for debug
	static void dump(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
	
	static class InputValidator implements Closeable {
		final InputStream in;
		
		final int NO_BUFFER = -2;
		int buffer;
		
		public InputValidator(final InputStream in) {
			this.in = in;
			buffer = NO_BUFFER;
		}
		
		int read() throws IOException {
			final int res = buffer == NO_BUFFER ? in.read() : buffer;
			buffer = NO_BUFFER;
			return res;
		}
		
		void unread(int ch) throws IOException {
			buffer = ch;
		}
		
		// [low, high]
		long nextLong(long low, long high) throws IOException {
			long val = 0;
			int ch = -1;
			boolean read = false;
			while (true) {
				ch = read();
				if (!Character.isDigit(ch)) break;
				read = true;
				val = val * 10 + ch - '0';
				check(val <= high);
			}
			check(read);
			check(ch >= 0);
			check(val >= low);
			unread(ch);
			return val;
		}
		int nextInt(int low, int high) throws IOException { return (int)nextLong(low, high); }
		int[] nextInts(int n, int low, int high) throws IOException {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt(low, high);
				if (i + 1 != n) space();
			}
			newLineOrEOF();
			return res;
		}
		
		void space() throws IOException { int ch = read(); check(ch == ' '); }
		void newLine() throws IOException {
			int ch = read();
			if (ch == '\r') ch = read();
			check(ch == '\n');
		}
		void newLineOrEOF() throws IOException { int ch = read(); check(ch == '\r' || ch == '\n' || ch < 0); }
		void eof() throws IOException { int ch = read(); check(ch < 0); }
		void check(boolean b) { if (!b) throw new RuntimeException(); }

		@Override
		public void close() throws IOException {
		}
	}
}
0