#include <cstdio> #include <map> #include <tuple> std::map<std::tuple<int, int, int, int>, int> map; int a1[10001]; int a2[10001]; int a3[10001]; int dp(int n, int r1, int r2, int r3) { if (n == 0) return 1; auto t = std::make_tuple(n, r1, r2, r3); auto itr = map.find(t); if (itr != map.end()) return itr->second; int u1, u2, u3; int ans = 0; u1 = r1 - a1[n]; u2 = r2 - a2[n]; u3 = r3 - a3[n]; if (u1 >= 0 || u2 >= 0 || u3 >= 0) ans += dp(n - 1, u1, u2, u3); u1 = r1 - !a1[n]; u2 = r2 - !a2[n]; u3 = r3 - !a3[n]; if (u1 >= 0 || u2 >= 0 || u3 >= 0) ans += dp(n - 1, u1, u2, u3); ans %= 1000000007; map[t] = ans; return ans; } int main() { int n; scanf("%d", &n); int r1, r2, r3; char c; scanf("%d", &r1); for (int i = 1; i <= n; i++) { scanf(" %c", &c); a1[i] = (c == '1'); } scanf("%d", &r2); for (int i = 1; i <= n; i++) { scanf(" %c", &c); a2[i] = (c == '1'); } scanf("%d", &r3); for (int i = 1; i <= n; i++) { scanf(" %c", &c); a3[i] = (c == '1'); } printf("%d\n", dp(n, r1, r2, r3)); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include <cstdio> #include <map> #include <tuple> std::map<std::tuple<int, int, int, int>, int> map; int a1[10001]; int a2[10001]; int a3[10001]; int dp(int n, int r1, int r2, int r3) { if (n == 0) return 1; auto t = std::make_tuple(n, r1, r2, r3); auto itr = map.find(t); if (itr != map.end()) return itr->second; int u1, u2, u3; int ans = 0; u1 = r1 - a1[n]; u2 = r2 - a2[n]; u3 = r3 - a3[n]; if (u1 >= 0 || u2 >= 0 || u3 >= 0) ans += dp(n - 1, u1, u2, u3); u1 = r1 - !a1[n]; u2 = r2 - !a2[n]; u3 = r3 - !a3[n]; if (u1 >= 0 || u2 >= 0 || u3 >= 0) ans += dp(n - 1, u1, u2, u3); ans %= 1000000007; map[t] = ans; return ans; } int main() { int n; scanf("%d", &n); int r1, r2, r3; char c; scanf("%d", &r1); for (int i = 1; i <= n; i++) { scanf(" %c", &c); a1[i] = (c == '1'); } scanf("%d", &r2); for (int i = 1; i <= n; i++) { scanf(" %c", &c); a2[i] = (c == '1'); } scanf("%d", &r3); for (int i = 1; i <= n; i++) { scanf(" %c", &c); a3[i] = (c == '1'); } printf("%d\n", dp(n, r1, r2, r3)); } |