結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 37zigen37zigen
提出日時 2016-05-05 02:37:23
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 3,512 bytes
コンパイル時間 2,560 ms
コンパイル使用メモリ 82,760 KB
実行使用メモリ 65,388 KB
最終ジャッジ日時 2024-04-15 13:01:34
合計ジャッジ時間 14,270 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
55,072 KB
testcase_01 AC 162 ms
54,856 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 166 ms
55,112 KB
testcase_21 RE -
testcase_22 AC 164 ms
55,088 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 3,669 ms
65,388 KB
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 AC 164 ms
54,928 KB
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.Arrays;
import java.util.Scanner;
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();
		int k=sc.nextInt();
		if(n>1000){
			long[] f=new long[k+2];
			long sum=0;
			for(int i=0;i<n;i++){
				f[i+1]=sc.nextInt();
				sum+=f[i+1];
			}
			for(int i=n;i<=k;i++){
				for(int j=i-n-1;j<i;j++){
					f[i]+=f[j];
					f[i]%=mod;
				}
				sum+=f[i];
				sum%=mod;
			}
			System.out.println(f[k]+" "+sum);
			return;
		}
		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;


		a=MtPow(k-n,B,a);
		if(n<k){
			System.out.println(a[1][0]+" "+a[0][0]);
		}else{
			System.out.println(ansF+" "+ansS);
		}

	}
	//nが非常に大きいケース


	//行列のN乗;R=A^n*v
	//MLE回避
	//注意:10000行*10000列ぐらいの行列を入れるとMLEする。
	//正方行列でないと計算できない。
	int[][] MtPow(long n,int[][] A,int[][] v){
		for(;n>0;n=n>>1){
			if((n&1)==1)v=MtPrd(A,v,mod);
			A=p2(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++){
			long sum=0;
			for(int j=0;j<B[0].length;j++){
				for(int k=0;k<A[0].length;k++){
					sum+=((long)A[i][k]*B[k][j]);
					if(sum>=BIG)sum-=BIG;
				}
				C[i][j]=(int)(sum%mod);
			}
		}
		return C;
	}

	int[][] p2(int[][] A,long mod){
		int n=A.length;
		int[][] C=new int[n][n];
		for(int i=0;i<n;i++){
			long[] sum=new long[n];
			for(int k=0;k<n;k++){
				for(int j=0;j<n;j++){
					sum[j]+=((long)A[i][k] * A[k][j]);
					if(sum[j]>=BIG)sum[j]-=BIG;
				}
			}
			for(int j=0;j<n;j++)C[i][j]=(int)(sum[j]%mod);
		}
		return C;
	}
	final long m2=(long)mod*mod;
	final long BIG=8L*m2;
	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 showMt(int[][] 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