Submission #98213


Source Code Expand

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	
	static void solve()
	{
		int K = ni(), n = ni();
		int mod = 1000000007;
		out.println(lrx(K, n-1, mod));
	}
	
	public static long lrx(int m, long n, long mod)
	{
//		long[][] m2 = new long[m-1][m];
		int[][] m2 = new int[m-1][m];
		Arrays.fill(m2[0], 1);
		for(int i = 1;i < m-1;i++){
			for(int j = 0;j < m;j++){
				int x = m2[i-1][m-1];
				if(j-1 >= 0)x += m2[i-1][j-1];
				if(x >= mod)x -= mod;
				m2[i][j] = x;
			}
		}
		
		long[] ret = new long[m];
		ret[0] = 1;
		long[] temp = new long[2*m];
		for(int l = 31;l >= 0;l--){
			if(n<<63-l<0){
				for(int i = 0;i < m;i++)temp[i] = ret[m-1];
				for(int i = 1;i < m;i++)temp[i] += ret[i-1];
				for(int i = 0;i < m;i++){
					ret[i] = temp[i];
					if(ret[i] >= mod)ret[i] -= mod;
				}
			}
			if(l > 0){
				Arrays.fill(temp, 0L);
				for(int i = 0;i < m;i++){
					if(ret[i] == 0)continue;
					for(int j = 0;j < m;j++){
						if(ret[j] == 0)continue;
						temp[i+j] += ret[i] * ret[j];// % mod;
//						temp[i+j] += ret[i] * ret[j];
						if(temp[i+j] >= 8000000000000000000L){
							temp[i+j] %= mod;
						}
//						}else{
//							tr(temp[i+j]);
//						}
					}
				}
				for(int i = 0;i < 2*m;i++){
					temp[i] %= mod;
				}
				for(int i = 0;i < m;i++){
					long s = temp[i];
					for(int j = m;j < 2*m-1;j++){
						s += temp[j] * m2[j-m][i];// % mod;
						if(s >= 8000000000000000000L)s %= mod;
					}
					ret[i] = s % mod;
				}
			}
		}
		
		long s = 0;
		for(int i = 0;i < m;i++){
			s += ret[i];
		}
		return s % mod;
	}
	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static boolean eof()
	{
		if(lenbuf == -1)return true;
		int lptr = ptrbuf;
		while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
		
		try {
			is.mark(1000);
			while(true){
				int b = is.read();
				if(b == -1){
					is.reset();
					return true;
				}else if(!isSpaceChar(b)){
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}
	
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;
	
	private static int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static double nd() { return Double.parseDouble(ns()); }
	private static char nc() { return (char)skip(); }
	
	private static String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private static char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private static char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private static int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private static int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}

Submission Info

Submission Time
Task T - フィボナッチ
User uwi
Language Java (OpenJDK 1.7.0)
Score 8
Code Size 4932 Byte
Status AC
Exec Time 1761 ms
Memory 27372 KB

Judge Result

Set Name All
Score / Max Score 8 / 8
Status
AC × 7
Set Name Test Cases
All 00, 01, 02, 03, 04, 90, 91
Case Name Status Exec Time Memory
00 AC 1740 ms 27300 KB
01 AC 1099 ms 25892 KB
02 AC 1761 ms 27364 KB
03 AC 524 ms 25256 KB
04 AC 1639 ms 27372 KB
90 AC 375 ms 18724 KB
91 AC 378 ms 18796 KB