結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 37zigen37zigen
提出日時 2016-05-05 01:17:28
言語 Java21
(openjdk 21)
結果
MLE  
実行時間 -
コード長 2,722 bytes
コンパイル時間 3,067 ms
コンパイル使用メモリ 83,296 KB
実行使用メモリ 743,052 KB
最終ジャッジ日時 2024-04-15 12:53:08
合計ジャッジ時間 14,122 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
51,180 KB
testcase_01 AC 83 ms
51,432 KB
testcase_02 AC 193 ms
53,432 KB
testcase_03 AC 102 ms
52,992 KB
testcase_04 AC 145 ms
53,272 KB
testcase_05 AC 142 ms
53,632 KB
testcase_06 AC 147 ms
53,484 KB
testcase_07 AC 159 ms
53,500 KB
testcase_08 AC 102 ms
52,696 KB
testcase_09 AC 152 ms
53,584 KB
testcase_10 AC 125 ms
53,384 KB
testcase_11 AC 126 ms
53,420 KB
testcase_12 AC 133 ms
53,484 KB
testcase_13 AC 121 ms
53,492 KB
testcase_14 AC 96 ms
40,116 KB
testcase_15 AC 176 ms
40,508 KB
testcase_16 AC 163 ms
40,692 KB
testcase_17 AC 124 ms
40,636 KB
testcase_18 AC 169 ms
40,488 KB
testcase_19 AC 182 ms
40,696 KB
testcase_20 AC 93 ms
39,932 KB
testcase_21 MLE -
testcase_22 AC 83 ms
51,232 KB
testcase_23 MLE -
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;
public class Main{
	public static void main(String[] args)throws Exception{
		new Main().solve();
	}
	final long mod=1_000_000_000+7;
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		Long k=sc.nextLong();
		long ansS=0,ansF=0;
		int[][] a=new int[n+1][1];
		for(int i=0;i<n;i++){
			int d=(int)(sc.nextInt()%mod);
			if(i+1==n)ansF=d;
			a[0][0]+=d;
			a[n-i][0]=d;
			if(i+1==k)ansS=d;
		}
		//コンパニオン行列
		int[][] B=new int[n+1][n+1];
		for(int i=0;i<n+1;i++)B[0][i]=1;
		for(int i=1;i<n+1;i++)B[1][i]=1;
		for(int i=0;i<n-1;i++)B[i+2][i+1]=1;
		
		int[][] ans=MtPow(k-n,B,a);
		if(n<k){
		System.out.println(ans[1][0]+" "+ans[0][0]);
		}else{
			System.out.println(ansF+" "+ansS);
		}
		
	}
	
	
	//行列のN乗;R=A^n*v
	//MLE回避
	//注意:10000行*10000列ぐらいの行列を入れるとMLEする。
	//正方行列でないと計算できない。
	int[][] MtPow(long n,int[][] A,int[][] v){
		while(n>=1){
			if(n%2==1){
				v=MtPrd(A,v,mod);
				n--;
			}else if(n%2==0){
				n/=2;
				A=MtPrd(A,A,mod);
			}
		}
		return v;
	}
	int[][] MtPrd(int[][] A,int[][] B,long mod){
		int[][] C=new int[A.length][B[0].length];
		for(int i=0;i<A.length;i++){
			for(int j=0;j<B[0].length;j++){
				for(int k=0;k<A[0].length;k++){
					C[i][j]+=((A[i][k]%mod)*(B[k][j]%mod))%mod;
					C[i][j]%=mod;
				}
				
			}
		}
		return C;
	}
	void showMt(long[][] A){
		for(int i=0;i<A.length;i++){
			for(int j=0;j<A[0].length;j++){
				System.out.print(A[i][j]+" ");
			}
			System.out.println();
		}
	}
	void tr(Object...o){System.out.println(Arrays.deepToString(o));}
	private static class Scanner{
		BufferedReader br;
		Iterator<String> it;
		Scanner(InputStream in){
			br=new BufferedReader(new InputStreamReader(in));
		}
		String next()throws RuntimeException{
			try{
				if(it==null||!it.hasNext())
					it=Arrays.asList(br.readLine().split(" ")).iterator();
				return it.next();
			}catch(IOException e){
				throw new IllegalStateException();
			}
		}
		int nextInt() throws RuntimeException{
			return Integer.parseInt(next());
		}
		long nextLong() throws RuntimeException{
			return Long.parseLong(next());
		}
		double nextDouble() throws RuntimeException{
			return Double.parseDouble(next());
		}
		void close(){
			try{
				br.close();
			}catch(IOException e){
				throw new IllegalStateException();
			}
		}
	}
	private static class Printer extends PrintWriter{
		Printer(PrintStream out){
			super(out);
		}
	}
}
0