結果

問題 No.950 行列累乗
ユーザー uwiuwi
提出日時 2019-12-13 00:21:18
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 7,572 bytes
コンパイル時間 4,488 ms
コンパイル使用メモリ 84,828 KB
実行使用メモリ 63,420 KB
最終ジャッジ日時 2024-06-26 00:58:45
合計ジャッジ時間 12,830 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 104 ms
52,628 KB
testcase_02 AC 52 ms
49,924 KB
testcase_03 AC 52 ms
50,276 KB
testcase_04 AC 53 ms
50,312 KB
testcase_05 AC 128 ms
56,784 KB
testcase_06 AC 52 ms
50,320 KB
testcase_07 AC 53 ms
50,540 KB
testcase_08 WA -
testcase_09 AC 52 ms
50,296 KB
testcase_10 AC 53 ms
50,160 KB
testcase_11 AC 52 ms
50,044 KB
testcase_12 AC 53 ms
50,460 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 54 ms
49,932 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 53 ms
50,160 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 169 ms
61,084 KB
testcase_22 WA -
testcase_23 AC 148 ms
56,740 KB
testcase_24 AC 162 ms
59,264 KB
testcase_25 AC 133 ms
56,780 KB
testcase_26 AC 145 ms
56,768 KB
testcase_27 AC 167 ms
58,984 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 171 ms
59,224 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 157 ms
58,576 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 53 ms
49,904 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 53 ms
50,332 KB
testcase_40 AC 162 ms
61,232 KB
testcase_41 AC 160 ms
61,120 KB
testcase_42 AC 187 ms
61,160 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 187 ms
61,044 KB
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 AC 53 ms
50,504 KB
testcase_55 WA -
testcase_56 WA -
testcase_57 AC 143 ms
56,640 KB
testcase_58 WA -
testcase_59 AC 52 ms
50,108 KB
testcase_60 AC 52 ms
50,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package adv2019;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;

public class D13 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int mod = ni();
		int[][] a = new int[2][2];
		int[][] b = new int[2][2];
		for(int i = 0;i < 2;i++) {
			a[i] = na(2);
		}
		for(int i = 0;i < 2;i++) {
			b[i] = na(2);
		}
		
		long DA = (long)a[0][0] * a[1][1] - (long)a[0][1] * a[1][0];
		long DB = (long)b[0][0] * b[1][1] - (long)b[0][1] * b[1][0];
		DA %= mod;
		DB %= mod;
		
		if(DA != 0 && DB != 0) {
			long res = bsgs(a, b, mod);
			out.println(res);
		}else if(DA == 0 && DB == 0) {
			long SA = (long)a[0][0] + a[0][1] + a[1][0] + a[1][1];
			long SB = (long)b[0][0] + b[0][1] + b[1][0] + b[1][1];
			if(SA == 0 && SB == 0) {
				out.println(1);
			}else if(SA != 0 && SB != 0) {
				long V = b[0][0] * invl(a[0][0], mod) % mod;
				long M = (a[0][0]+a[1][1]) % mod;
				if(M != 0) {
					for(int i = 0;i < 2;i++) {
						for(int j = 0;j < 2;j++) {
							if((long)a[i][j] * V % mod != b[i][j]) {
								out.println(-1);
								return;
							}
						}
					}
					out.println(bsgs(M, V, mod));
				}else {
					out.println(-1);
				}
			}else if(SA != 0 && SB == 0) {
				long M = (a[0][0]+a[1][1]) % mod;
				if(M == 0) {
					out.println(2);
				}else {
					out.println(-1);
				}
			}else {
				out.println(-1);
			}
		}else {
			out.println(-1);
		}
	}
	
	public static long bsgs(long alpha, long beta, int p)
	{
		int m = (int)Math.sqrt(p)+1;
		long[] table = new long[m];
		long val = 1;
		for(int j = 0;j < m;j++){
			table[j] = val<<32|j;
			val = val * alpha % p;
		}
		Arrays.sort(table);
		long ainvm = invl(val, p);
		
		long g = beta;
		for(int i = 0;i < m;i++){
			int ind = Arrays.binarySearch(table, g<<32);
			if(ind < 0)ind = -ind-1;
			if(ind < m && table[ind]>>>32 == g){
				if((long)i*m+(int)table[ind] == 0)continue;
				return i*m+(int)table[ind];
			}
			g = g * ainvm % p;
		}
		return -1;
	}

	
	static class Datum
	{
		int[][] M;
		int e;
		public Datum(int[][] m, int e) {
			super();
			M = m;
			this.e = e;
		}
		
		
	}
	
	static long h(int[][] a)
	{
		long x = 1;
		for(int[] row : a) {
			for(int v : row) {
				x = x * 1000000009L + v;
			}
		}
		return x;
	}
	
	public static long bsgs(int[][] alpha, int[][] beta, int p)
	{
		int m = (int)Math.sqrt(p)+1;
		Map<Long, Datum> table = new HashMap<>();
		int[][] val = new int[2][2];
		val[0][0] = val[1][1] = 1;
		for(int j = 0;j < m;j++){
			table.put(h(val), new Datum(val, j));
			val = mul(val, alpha, p);
		}
		long D = ((long)val[0][0] * val[1][1] - (long)val[0][1] * val[1][0]);
		D %= p;
		if(D < 0)D += p;
		long ID = invl(D, p);
		int[][] ainvm = new int[][] {
			{val[1][1], p-val[0][1]},
			{p-val[1][0], val[0][0]}
		};
		for(int i= 0;i < 2;i++) {
			for(int j = 0;j < 2;j++) {
				ainvm[i][j] = (int)(ainvm[i][j] * ID % p);
			}
		}
		
		int[][] g = beta;
		for(int i = 0;i < m+1;i++){
			long code = h(g);
			if(table.containsKey(code)) {
				if(table.get(code).e + (long)i*m == 0)continue;
				return table.get(code).e + (long)i*m;
			}
			g = mul(g, ainvm, p);
		}
		return -1;
	}
	
	public static long invl(long a, long mod) {
		long b = mod;
		long p = 1, q = 0;
		while (b > 0) {
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		return p < 0 ? p + mod : p;
	}

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

	
	public static int[] pow(int[][] A, int[] v, long e, int mod)
	{
		int[][] MUL = A;
		for(int i = 0;i < v.length;i++)v[i] %= mod;
		for(;e > 0;e>>>=1) {
			if((e&1)==1)v = mul(MUL, v, mod);
			MUL = p2(MUL, mod);
		}
		return v;
	}
	
	
	// int matrix * int vector
	public static int[] mul(int[][] A, int[] v, int mod)
	{
		int m = A.length;
		int n = v.length;
		int[] w = new int[m];
		for(int i = 0;i < m;i++){
			long sum = 0;
			for(int k = 0;k < n;k++){
				sum += (long)A[i][k] * v[k];
				sum %= mod;
			}
			w[i] = (int)sum;
		}
		return w;
	}
	
	// int matrix^2 (cannot ensure negative values)
	public static int[][] p2(int[][] A, int mod)
	{
		int n = A.length;
		int[][] C = new int[n][n];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < n;j++){
				long sum = 0;
				for(int k = 0;k < n;k++){
					sum += (long)A[i][k] * A[k][j];
					sum %= mod;
				}
				C[i][j] = (int)sum;
			}
		}
		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 D13().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