Submission #223833


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 n = ni(), m = ni(), Q = ni();
		int[] a = na(n);
		
		int mod = 1000000007;
		int[][] dpf = new int[n+1][m+1];
		dpf[0][0] = 1;
		for(int i = 0;i < n;i++){
			int cum = 0;
			for(int j = 0;j <= m;j++){
				cum += dpf[i][j];
				if(cum >= mod)cum -= mod;
				dpf[i+1][j] = cum;
				if(j >= a[i]){
					cum -= dpf[i][j-a[i]];
					if(cum < 0)cum += mod;
				}
			}
		}
		
		int[][] dpb = new int[n+1][m+1];
		dpb[n][0] = 1;
		for(int i = n-1;i >= 0;i--){
			int cum = 0;
			for(int j = 0;j <= m;j++){
				cum += dpb[i+1][j];
				if(cum >= mod)cum -= mod;
				dpb[i][j] = cum;
				if(j >= a[i]){
					cum -= dpb[i+1][j-a[i]];
					if(cum < 0)cum += mod;
				}
			}
		}
		
		int[][] cache = new int[n][m+1];
		for(int i = 0;i < n;i++){
			Arrays.fill(cache[i], -1);
		}
		long BIG = 8L*mod*mod;
		for(int q = 0;q < Q;q++){
			int k = ni()-1, x = ni();
			if(cache[k][x] != -1){
				out.println(cache[k][x]);
			}else{
				long ret = 0;
				for(int j = 0;j <= m-x;j++){
					ret += (long)dpf[k][j] * dpb[k+1][m-x-j];
					if(ret >= BIG)ret -= BIG;
				}
				cache[k][x] = (int)(ret%mod);
				out.println(cache[k][x]);
			}
		}
	}
	
	public static void main(String[] args) throws Exception
	{
//		int n = 2000, m = 2000;
//		int q = 100000;
//		Random gen = new Random();
//		StringBuilder sb = new StringBuilder();
//		sb.append(n + " ");
//		sb.append(m + " ");
//		sb.append(q + " ");
//		for (int i = 0; i < n; i++) {
//			sb.append(m + " ");
//		}
//		for(int i = 0;i < q;i++){
//			sb.append(gen.nextInt(n)+1 + " ");
//			sb.append(1 + " ");
//		}
//		INPUT = sb.toString();
		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 boolean isSpaceChar(int c) { return !(c >= 32 && 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))){
			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 D - 注文の多い高橋商店
User uwi
Language Java (OpenJDK 1.7.0)
Score 80
Code Size 5008 Byte
Status TLE
Exec Time 2048 ms
Memory 106268 KB

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 10 / 10 20 / 20 50 / 50 0 / 20
Status
AC × 2
AC × 10
AC × 13
AC × 13
AC × 18
TLE × 3
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
Subtask1 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt
Subtask2 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt
Subtask3 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt
Subtask4 subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt, subtask4_01.txt, subtask4_02.txt, subtask4_03.txt, subtask4_04.txt, subtask4_05.txt, subtask4_06.txt, subtask4_07.txt
Case Name Status Exec Time Memory
sample_01.txt AC 320 ms 20672 KB
sample_02.txt AC 284 ms 20536 KB
subtask1_01.txt AC 289 ms 20656 KB
subtask1_02.txt AC 301 ms 20656 KB
subtask1_03.txt AC 290 ms 20660 KB
subtask1_04.txt AC 289 ms 20788 KB
subtask1_05.txt AC 301 ms 21220 KB
subtask1_06.txt AC 291 ms 21176 KB
subtask1_07.txt AC 870 ms 21176 KB
subtask1_08.txt AC 292 ms 21176 KB
subtask2_01.txt AC 415 ms 27020 KB
subtask2_02.txt AC 438 ms 31160 KB
subtask2_03.txt AC 446 ms 29980 KB
subtask3_01.txt AC 439 ms 45252 KB
subtask3_02.txt AC 541 ms 70740 KB
subtask3_03.txt AC 483 ms 70864 KB
subtask4_01.txt TLE 2046 ms 71876 KB
subtask4_02.txt TLE 2046 ms 87924 KB
subtask4_03.txt AC 550 ms 32832 KB
subtask4_04.txt AC 550 ms 32936 KB
subtask4_05.txt TLE 2048 ms 88740 KB
subtask4_06.txt AC 849 ms 95324 KB
subtask4_07.txt AC 938 ms 106268 KB