Submission #338086


Source Code Expand

import java.io.*;
import java.util.*;

public class Main {
	FastScanner in = new FastScanner(System.in);
	PrintWriter out = new PrintWriter(System.out);

	long[][][][] dp;
	
	int MAX = 256;
	int MOD = 1000000007;
	public void run() {
		int N = in.nextInt(), X = in.nextInt();
		String s = in.next();
		
		dp = new long[2][MAX][MAX][2];
		dp[0][0][0][0] = 1;
		for (int i = 0; i < N; i++) {
			int cur = i % 2, next = (i + 1) % 2;
			for (int j = 0; j < MAX; j++) {
				for (int k = 0; k < MAX; k++) {
					dp[next][j][k][0] = 0;
					dp[next][j][k][1] = 0;
				}
			}
			for (int j = 0; j < 140; j++) {
				for (int k = 0; k <= X; k++) {
					if (s.charAt(i) == '?' || s.charAt(i) == '2') {
						dp[next][j][k][1] = (dp[next][j][k][1] + dp[cur][j][k][0]) % MOD;
					}
					
					if (s.charAt(i) == '?') {
						dp[next][0][k][0] = (dp[next][0][k][0] + dp[cur][j][k][0] * 9) % MOD;
					} else if (s.charAt(i) != '2') {
						dp[next][0][k][0] = (dp[next][0][k][0] + dp[cur][j][k][0]) % MOD;
					}
					
					if (s.charAt(i) == '?' || s.charAt(i) == '5') {
						int nk = Math.min(X, k + j + 1);
						dp[next][j+1][nk][0] = (dp[next][j+1][nk][0] + dp[cur][j][k][1]) % MOD;
					}
					
					if (s.charAt(i) == '?' || s.charAt(i) == '2') {
						dp[next][0][k][1] = (dp[next][0][k][1] + dp[cur][j][k][1]) % MOD;
					}
					
					if (s.charAt(i) == '?') {
						dp[next][0][k][0] = (dp[next][0][k][0] + dp[cur][j][k][1] * 8) % MOD;
					} else if (s.charAt(i) != '2' && s.charAt(i) != '5') {
						dp[next][0][k][0] = (dp[next][0][k][0] + dp[cur][j][k][1]) % MOD;
					}
				}
			}
		}
		
		long res = 0;
		for (int i = 0; i < MAX; i++) {
			res = (res + dp[N%2][i][X][0]) % MOD;
			res = (res + dp[N%2][i][X][1]) % MOD;
		}
		System.out.println(res);
		
		out.close();
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");

		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}

		System.out.println("----------------------------");
		System.out.println();
	}

	public void debug(Object... obj) {
		System.out.println(Arrays.deepToString(obj));
	}

	class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
			//stream = new FileInputStream(new File("dec.in"));

		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();

			return array;
		}

		int[][] nextIntMap(int n, int m) {
			int[][] map = new int[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextIntArray(m);
			}
			return map;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();

			return array;
		}

		long[][] nextLongMap(int n, int m) {
			long[][] map = new long[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextLongArray(m);
			}
			return map;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();

			return array;
		}

		double[][] nextDoubleMap(int n, int m) {
			double[][] map = new double[n][m];
			for (int i = 0; i < n; i++) {
				map[i] = in.nextDoubleArray(m);
			}
			return map;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}

Submission Info

Submission Time
Task A - ニコニコ文字列2
User hiro116s
Language Java (OpenJDK 1.7.0)
Score 20
Code Size 4996 Byte
Status AC
Exec Time 1943 ms
Memory 38360 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 487 ms 27760 KB
sample_02.txt AC 523 ms 29808 KB
subtask1_01.txt AC 453 ms 26788 KB
subtask1_02.txt AC 463 ms 26944 KB
subtask1_03.txt AC 462 ms 27072 KB
subtask1_04.txt AC 450 ms 26616 KB
subtask1_05.txt AC 1005 ms 38360 KB
subtask1_06.txt AC 782 ms 31156 KB
subtask1_07.txt AC 718 ms 29880 KB
subtask1_08.txt AC 868 ms 29680 KB
subtask1_09.txt AC 770 ms 31352 KB
subtask1_10.txt AC 676 ms 30680 KB
subtask1_11.txt AC 1062 ms 37352 KB
subtask1_12.txt AC 1490 ms 30604 KB
subtask1_13.txt AC 849 ms 29616 KB
subtask1_14.txt AC 1519 ms 30752 KB
subtask1_15.txt AC 1605 ms 31508 KB
subtask1_16.txt AC 1411 ms 30808 KB
subtask1_17.txt AC 1943 ms 31604 KB
subtask1_18.txt AC 1118 ms 29344 KB
subtask1_19.txt AC 1881 ms 31672 KB
subtask1_20.txt AC 1430 ms 30664 KB
subtask1_21.txt AC 726 ms 29588 KB
subtask1_22.txt AC 930 ms 38300 KB