結果
問題 | No.194 フィボナッチ数列の理解(1) |
ユーザー |
|
提出日時 | 2016-05-05 00:30:50 |
言語 | Java (openjdk 23) |
結果 |
MLE
|
実行時間 | - |
コード長 | 2,831 bytes |
コンパイル時間 | 2,857 ms |
コンパイル使用メモリ | 86,388 KB |
実行使用メモリ | 744,376 KB |
最終ジャッジ日時 | 2024-10-05 09:07:11 |
合計ジャッジ時間 | 17,737 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 TLE * 1 MLE * 5 -- * 12 |
ソースコード
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; 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(); long ansS=0,ansF=0; long[][] a=new long[n+1][1]; for(int i=0;i<n;i++){ int d=sc.nextInt(); if(i+1==n)ansF=d; a[0][0]+=d; a[n-i][0]=d; if(i+1==k)ansS=d; } //コンパニオン行列 long[][] B=new long[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; long[][] ans=MtPow(k-n,B); ans=MtPrd(ans, a,mod); if(n<k){ System.out.println(ans[1][0]+" "+ans[0][0]); }else{ System.out.println(ansF+" "+ansS); } } //行列のN乗;R=A^n //注意:10000行*10000列ぐらいの行列を入れるとMLEする。 //正方行列でないと計算できない。 long[][] MtPow(long n,long[][] A){ long[][] B=new long[A.length][A[0].length]; for(int i=0;i<A.length;i++){ B[i][i]=1; } while(n>=1){ if(n%2==1){ B=MtPrd(A,B,mod); n--; }else if(n%2==0){ n/=2; A=MtPrd(A,A,mod); } } return B; } long[][] MtPrd(long[][] A,long[][] B,long mod){ long[][] C=new long[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); 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); } } }