結果

問題 No.950 行列累乗
ユーザー 37zigen37zigen
提出日時 2019-12-13 01:18:15
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,852 bytes
コンパイル時間 2,503 ms
コンパイル使用メモリ 76,484 KB
実行使用メモリ 63,876 KB
最終ジャッジ日時 2023-09-08 09:24:06
合計ジャッジ時間 15,120 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 126 ms
55,708 KB
testcase_02 AC 125 ms
53,848 KB
testcase_03 AC 124 ms
55,484 KB
testcase_04 AC 126 ms
55,928 KB
testcase_05 AC 171 ms
60,304 KB
testcase_06 AC 126 ms
55,736 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 125 ms
55,908 KB
testcase_10 AC 124 ms
55,732 KB
testcase_11 WA -
testcase_12 AC 126 ms
55,480 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 126 ms
56,064 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 127 ms
55,672 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 182 ms
60,840 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 195 ms
60,456 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 182 ms
59,960 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 192 ms
60,492 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 181 ms
60,504 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 129 ms
55,692 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 128 ms
55,856 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 191 ms
60,424 KB
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 193 ms
60,752 KB
testcase_46 WA -
testcase_47 TLE -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	long MOD;
	
	long[][] pow(long[][] a, long n){
		long[][] ret=new long[2][2];
		ret[0][0]=ret[1][1]=1;
		for(;n>0;n>>=1,a=mul(a,a)){
			if(n%2==1) ret=mul(ret,a);
		}
		return ret;
	}
	
	long[][] mul(long[][] a, long[][] b){
		long[][] ret=new long[a.length][b[0].length];
		for(int i=0;i<a.length;++i){
			for(int j=0;j<b[i].length;++j){
				for(int k=0;k<a[i].length;++k){
					ret[i][j]+=a[i][k]*b[k][j]%MOD;
				}
				ret[i][j]%=MOD;
			}
		}
		return ret;
	}
	
	long det(long[][] a){
		return (a[0][0]*a[1][1]%MOD-a[0][1]*a[1][0]%MOD+MOD)%MOD; 
	}
	
	void run() {
	 	Scanner sc = new Scanner(System.in);
		/*
		for(long a=2;a<10;++a){
			for(long b=2;b<10;++b){
				long u=discretelog(a,b);
				tr(pow(a,u),u,a,b);
				for(long u2=0;u2<MOD;++u2){
					if(pow(a,u2)==b){
						tr(pow(a,u2),u2,a,b);
						break;
					}
				}
			}
		}
		*/
		MOD=sc.nextLong();
		long[][] a=new long[2][2];
		long[][] b=new long[2][2];
		for(int i=0;i<2;++i)for(int j=0;j<2;++j)a[i][j]=sc.nextLong();
		for(int i=0;i<2;++i)for(int j=0;j<2;++j)b[i][j]=sc.nextLong();
		long det0=det(a);
		long det1=det(b);
		long x=discretelog(det0,det1);
		if(x==-1){
			System.out.println(-1);
			return;
		}
		long[][] na=pow(a,x);
		long ans=x;
		long ord=ord(det0,MOD);
		long[][] c=pow(a,ord);
		for(int i=0;i<MOD;i+=ord){
			boolean equiv=true;
			for(int j=0;j<2;++j)
				for(int k=0;k<2;++k)
					equiv&=na[j][k]==b[j][k];
			if(equiv){
				System.out.println(x+i);
				return;
			}
			na=mul(na,c);
		}
		System.out.println(-1);
	}
	
	long inv(long a){
		return pow(a, MOD-2);
	}
	
	long ord(long a, long p) {
		long ret = p - 1;
		for (long div = 2; div * div <= p - 1; ++div) {
			if ((p - 1) % div != 0) continue;
			if (pow(a, div) == 1) ret = Math.min(ret, div);
			else if (pow(a, (p - 1) / div) == 1) ret = Math.min(ret, (p - 1) / div);
		}
		return ret;
	}
	
	long pow(long a, long n){
		long ret=1;
		for(;n>0;n>>=1,a=a*a%MOD){
			if(n%2==1)ret=ret*a%MOD;
		}
		return ret;
	}
	
	// return x s.t. a^x = b && x>0
	long discretelog(long a, long b){
		if(a==1){
			if(b==1)
				return 1;
			else return -1;
		}else if(a==0){
			if(b==0)return 1;
			else return -1;
		}
		// a^(um+v) = b
		// a^v = b a^(-m)^u
		int m=(int)Math.sqrt(MOD);
		long pw=1;
		HashMap<Long,Integer> map=new HashMap<>();
		for(int v=0;v<=m;++v){
			map.put(pw,v);
			pw=pw*a%MOD;
		}
		long ima=pow(inv(a),m);
		long ipw=1;
		for(int i=0;i<=m;++i){
			if(map.containsKey(b*ipw)){
				long ret=i*m+map.get(b*ipw);
				if(ret!=0)return ret;
			}
			ipw=ipw*ima%MOD;
		}
		return -1;
	}
	
	long gcd(long a,long b){
		if(a>b)return gcd(b,a);
		if(a==0)return b;
		return gcd(a,b%a);
	}
	
	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
0