Submission #337865


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(), K = ni();
		char[] s = ns().toCharArray();
		long[][] pre = new long[K+1][n+1]; // [get][len]
		long[][] cur = new long[K+1][n+1];
		pre[0][0] = 1;
		int mod = 1000000007;
		for(int i = 0;i < n;i++){
			for(int j = 0;j <= K;j++){
				Arrays.fill(cur[j], 0);
			}
			for(int j = 0;j <= K;j++){
				for(int k = 0;k <= n;k++){
					if(pre[j][k] == 0)continue;
					if(s[i] == '2' || s[i] == '?'){
						int nk = k % 2 == 0 ? k+1 : 1;
						cur[j][nk] += pre[j][k];
						if(cur[j][nk] >= mod)cur[j][nk] -= mod;
					}
					if(s[i] == '5' || s[i] == '?'){
						int nk = k % 2 == 1 ? k+1 : 0;
						int nj = Math.min(K, j+nk/2);
						cur[nj][nk] += pre[j][k];
						if(cur[nj][nk] >= mod)cur[nj][nk] -= mod;
					}
					if(s[i] != '?' && s[i] != '2' && s[i] != '5'){
						cur[j][0] += pre[j][k];
						if(cur[j][0] >= mod)cur[j][0] -= mod;
					}
					if(s[i] == '?'){
						cur[j][0] += pre[j][k] * 8L;
						cur[j][0] %= mod;
					}
				}
			}
			long[][] dum = pre; pre = cur; cur = dum;
		}
		long ret = 0;
		for(int i = 0;i <= n;i++){
			ret += pre[K][i];
		}
		out.println(ret%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 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 A - ニコニコ文字列2
User uwi
Language Java (OpenJDK 1.7.0)
Score 20
Code Size 4568 Byte
Status AC
Exec Time 653 ms
Memory 24944 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 20 / 20
Status
AC × 2
AC × 22
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt, subtask1_18.txt, subtask1_19.txt, subtask1_20.txt, subtask1_21.txt, subtask1_22.txt
Case Name Status Exec Time Memory
sample_01.txt AC 458 ms 20840 KB
sample_02.txt AC 452 ms 20588 KB
subtask1_01.txt AC 456 ms 20632 KB
subtask1_02.txt AC 445 ms 20664 KB
subtask1_03.txt AC 456 ms 20772 KB
subtask1_04.txt AC 452 ms 20664 KB
subtask1_05.txt AC 485 ms 22900 KB
subtask1_06.txt AC 505 ms 23320 KB
subtask1_07.txt AC 495 ms 23036 KB
subtask1_08.txt AC 508 ms 23296 KB
subtask1_09.txt AC 494 ms 23168 KB
subtask1_10.txt AC 476 ms 21968 KB
subtask1_11.txt AC 515 ms 23308 KB
subtask1_12.txt AC 615 ms 24384 KB
subtask1_13.txt AC 520 ms 23300 KB
subtask1_14.txt AC 622 ms 24944 KB
subtask1_15.txt AC 636 ms 24416 KB
subtask1_16.txt AC 619 ms 24344 KB
subtask1_17.txt AC 653 ms 24304 KB
subtask1_18.txt AC 591 ms 24176 KB
subtask1_19.txt AC 638 ms 24240 KB
subtask1_20.txt AC 607 ms 24160 KB
subtask1_21.txt AC 489 ms 22712 KB
subtask1_22.txt AC 471 ms 21120 KB