結果

問題 No.3038 フィボナッチ数列の周期
ユーザー tanzakutanzaku
提出日時 2018-04-01 23:52:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 581 ms / 3,000 ms
コード長 3,564 bytes
コンパイル時間 2,571 ms
コンパイル使用メモリ 88,696 KB
実行使用メモリ 68,988 KB
最終ジャッジ日時 2023-09-08 13:04:48
合計ジャッジ時間 11,196 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,756 KB
testcase_01 AC 129 ms
55,924 KB
testcase_02 AC 127 ms
56,008 KB
testcase_03 AC 128 ms
55,664 KB
testcase_04 AC 126 ms
55,844 KB
testcase_05 AC 128 ms
55,812 KB
testcase_06 AC 126 ms
56,036 KB
testcase_07 AC 151 ms
55,920 KB
testcase_08 AC 149 ms
55,876 KB
testcase_09 AC 145 ms
55,992 KB
testcase_10 AC 148 ms
55,896 KB
testcase_11 AC 144 ms
55,900 KB
testcase_12 AC 145 ms
55,936 KB
testcase_13 AC 148 ms
55,880 KB
testcase_14 AC 146 ms
55,992 KB
testcase_15 AC 146 ms
55,860 KB
testcase_16 AC 555 ms
68,936 KB
testcase_17 AC 564 ms
68,780 KB
testcase_18 AC 548 ms
68,792 KB
testcase_19 AC 548 ms
68,348 KB
testcase_20 AC 540 ms
66,760 KB
testcase_21 AC 581 ms
68,304 KB
testcase_22 AC 548 ms
68,336 KB
testcase_23 AC 559 ms
68,988 KB
testcase_24 AC 548 ms
68,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
	void solveTestcase(final Scanner in, final PrintWriter out) {
		final int mod = (int)(1e9)+7;
		int n = in.nextInt();

		TreeMap<Integer, Integer> e = new TreeMap<>();
		for (int i = 0; i < n; i++) {
			int p = in.nextInt();
			int k = in.nextInt();
			TreeMap<Integer, Integer> e2 = new TreeMap<>();
			e2.put(p, k - 1);
			e2.put(p, k - 1);
			int peri = (int)fibonacciPeriod(p);
			for (int j = 2; j*j <= peri; j++) {
				int c = 0;
				while (peri % j == 0) {
					peri /= j;
					c++;
				}
				if (c > 0) e2.put(j, e2.getOrDefault(j, 0) + c);
			}
			if (peri > 1) {
				e2.put(peri, e2.getOrDefault(peri, 0) + 1);
			}
			for (Entry<Integer, Integer> en : e2.entrySet()) {
				int key = en.getKey();
				e.put(key, Math.max(e.getOrDefault(key, 0), en.getValue()));
			}
		}
		
		long ans = 1;
		for (Entry<Integer, Integer> en : e.entrySet()) {
			for (int i = 0; i < en.getValue(); i++) {
				ans = ans * en.getKey() % mod;
			}
		}
		out.println(ans);
	}
	
	void solve() {
		try (final PrintWriter out = new PrintWriter(System.out)) {
			try (final Scanner in = new Scanner(System.in)) {
//				int t = in.nextInt();
				int t = 1;
				while (t-- > 0) {
					solveTestcase(in, out);
				}
			}
		}
	}

	public static void main(String[] args) {
		new Main().solve();
	}
	
	static long fibonacciPeriod(int mod) {
		if (mod == 2) {
			return 3;
		}
		if (mod == 5) {
			return 20;
		}
		long ans = Long.MAX_VALUE;
		if (mod % 10 == 1 || mod % 10 == 9) {
			final int d = mod - 1;
			for (int i = 1; i*i <= d && i < ans; i++) {
				if (d % i == 0) {
					if (periodic(d / i, mod)) {
						ans = Math.min(ans, d / i);
					}
					if (periodic(i, mod)) {
						ans = Math.min(ans, i);
					}
				}
			}
		}
		if (mod % 10 == 3 || mod % 10 == 7) {
			final int d = 2 * (mod + 1);
			for (int i = 1; i*i <= d && i < ans; i++) {
				if (d % i == 0) {
					if (i % 2 == 1 && periodic(d / i, mod)) {
//					if (periodic(d / i, mod)) {
						ans = Math.min(ans, d / i);
					}
					if (d / i % 2 == 1 && periodic(i, mod)) {
//					if (periodic(i, mod)) {
						ans = Math.min(ans, i);
					}
				}
			}
		}
		return ans;
	}
	
	static boolean periodic(int p, int mod) {
		return fib(p, mod) == 0 && fib(p+1, mod) == 1;
	}
	
	// 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;
	}
	
	static long fib(long n, int mod) {
		long[][] mat = new long[][]{
				new long[]{1, 1,},
				new long[]{1, 0,},
		};
		return powmat(n, mod, mat)[1][0];
	}
	
	static void dump(Object...objects) { System.err.println(Arrays.deepToString(objects)); }
}
0