結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 37zigen37zigen
提出日時 2016-05-05 03:00:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 380 ms / 5,000 ms
コード長 3,567 bytes
コンパイル時間 2,723 ms
コンパイル使用メモリ 82,508 KB
実行使用メモリ 65,676 KB
最終ジャッジ日時 2024-04-15 13:04:22
合計ジャッジ時間 14,491 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 179 ms
54,976 KB
testcase_01 AC 178 ms
54,876 KB
testcase_02 AC 258 ms
55,064 KB
testcase_03 AC 196 ms
54,456 KB
testcase_04 AC 222 ms
55,084 KB
testcase_05 AC 213 ms
54,992 KB
testcase_06 AC 209 ms
54,916 KB
testcase_07 AC 227 ms
55,060 KB
testcase_08 AC 183 ms
54,732 KB
testcase_09 AC 213 ms
54,928 KB
testcase_10 AC 207 ms
54,808 KB
testcase_11 AC 213 ms
54,932 KB
testcase_12 AC 219 ms
55,084 KB
testcase_13 AC 221 ms
55,068 KB
testcase_14 AC 188 ms
54,968 KB
testcase_15 AC 272 ms
55,228 KB
testcase_16 AC 221 ms
54,952 KB
testcase_17 AC 208 ms
55,052 KB
testcase_18 AC 218 ms
55,460 KB
testcase_19 AC 243 ms
55,212 KB
testcase_20 AC 182 ms
54,728 KB
testcase_21 AC 380 ms
65,676 KB
testcase_22 AC 178 ms
55,108 KB
testcase_23 AC 304 ms
59,716 KB
testcase_24 AC 345 ms
62,736 KB
testcase_25 AC 344 ms
63,136 KB
testcase_26 AC 348 ms
61,416 KB
testcase_27 AC 258 ms
59,332 KB
testcase_28 AC 301 ms
59,428 KB
testcase_29 AC 276 ms
63,684 KB
testcase_30 AC 238 ms
54,960 KB
testcase_31 AC 185 ms
54,724 KB
testcase_32 AC 217 ms
55,164 KB
testcase_33 AC 213 ms
55,060 KB
testcase_34 AC 215 ms
55,012 KB
testcase_35 AC 209 ms
55,196 KB
testcase_36 AC 220 ms
55,352 KB
testcase_37 AC 184 ms
55,180 KB
testcase_38 AC 223 ms
55,068 KB
testcase_39 AC 214 ms
55,180 KB
権限があれば一括ダウンロードができます

ソースコード

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();
		long k=sc.nextLong();
		if(n>100){
			long[] S=new long[(int)k+2];
			long sum=0;
			for(int i=0;i<n;i++){
				S[i+1]=sc.nextLong();
				if(i!=0)S[i+1]+=S[i];
				S[i+1]%=mod;
			}
			long f=-1;
			for(int i=n+1;i<=k;i++){
				//i番目のF
				f=(S[i-1]-S[i-n-1]+mod)%mod;
				f%=mod;
				S[i]=S[i-1]+f;
				S[i]%=mod;
			}
			System.out.println(f+" "+S[(int)k]);

			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