fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Main {
  5. static final int MOD = 998244353;
  6. static StringTokenizer tokenizer;
  7.  
  8. public static void main(String[] args) throws Exception {
  9. char[] s = br.readLine().trim().toCharArray();
  10. int m = Integer.parseInt(br.readLine().trim());
  11. int len = s.length;
  12. int[] must = new int[len];
  13. Arrays.fill(must, -1);
  14. for (int i = 0; i < m; i++) {
  15. String[] parts = br.readLine().split(" ");
  16. int p = Integer.parseInt(parts[0]);
  17. int o = Integer.parseInt(parts[1]);
  18. if (p < len) {
  19. must[len - 1 - p] = o;
  20. }
  21. }
  22.  
  23. long[][][] dp = new long[len + 1][2][2];
  24. dp[0][1][0] = 1;
  25. for (int i = 0; i < len; i++) {
  26. for (int tight = 0; tight < 2; tight++) {
  27. for (int has = 0; has < 2; has++) {
  28. if (dp[i][tight][has] == 0) continue;
  29. int maxDigit = (tight == 1) ? (s[i] - '0') : 1;
  30. for (int d = 0; d <= maxDigit; d++) {
  31. if (must[i] != -1 && d != must[i]) continue;
  32. int newTight = (tight == 1 && d == maxDigit) ? 1 : 0;
  33. int newHas = (has == 1 || d == 1) ? 1 : 0;
  34. dp[i + 1][newTight][newHas] = (dp[i + 1][newTight][newHas] + dp[i][tight][has]) % MOD;
  35. }
  36. }
  37. }
  38. }
  39. long ans = 0;
  40. for (int tight = 0; tight < 2; tight++) {
  41. for (int has = 0; has < 2; has++) {
  42. ans = (ans + dp[len][tight][has]) % MOD;
  43. }
  44. }
  45. System.out.println(ans);
  46. }
  47. }
Success #stdin #stdout 0.07s 55012KB
stdin
1011
2
1 1
3 0
stdout
4