Submission #60255

# Submission time Handle Problem Language Result Execution time Memory
60255 2018-07-23T22:40:04 Z Benq Bowling (BOI15_bow) C++11
100 / 100
535 ms 9384 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;


struct hsh {
    size_t operator()(const pi& k) const {
        return (ll)MOD*k.f+k.s; // bad, but you get the point
    }
};

int q, n, num[10], ret;
ll m[2][3][301][301], M[2][3][301][301];
string tmp[10];

string gen(vi nex) {
    int lst = -1;
    string res;
    for (int i: nex) {
        if (i == -1) res += '-';
        else if (lst != -1 && i+lst == 10) {
            res += '/';
            lst = -1;
        } else if (i == 10) res += 'x';
        else {
            if (lst == -1) lst = 0;
            res += char('0'+i);
            lst += i;
        }
    }
    return res;
}

bool match(string bad, vi nex) {
    string t = gen(nex);
    F0R(i,sz(bad)) if (bad[i] != '?' && bad[i] != t[i]) return 0;
    return 1;
}


void tri(int a, int b, int c, int d, int x, vi y) {
    ll val = m[a][b][c][d];
    int cur = d;
    for (int i: y) if (i != -1) {
        cur += i;
        if (a) c += i, d += i, cur += i, a --;
        if (b) d += i, cur += i, b --;
    }
    
    int posi = 0;
    if (x != n-1) {
        if (y[0] == 10) posi = 2;
        else if (y[0]+y[1] == 10) posi = 1;
    }
    
    if (x >= 2 && a == 0 && num[x-2] != -1 && c != num[x-2]) return;
    if (x >= 1 && b == 0 && num[x-1] != -1 && d != num[x-1]) return;
    if (posi == 0 && num[x] != -1 && cur != num[x]) return;
    M[b][posi][d][cur] += val;
}

char get(int j) {
    if (j == 10) return 'x';
    return char('0'+j);
}
bool ok(int x, int y) {
    if (x < 0) return 1;
    if (num[x] == -1) return 1;
    return num[x] == y;
}

ll TMP[2][301][11], ans;

void TRI(int a, int b, int c, int d, int x, int i) {
    ll val = m[a][b][c][d];
    if (tmp[x][0] != '?' && tmp[x][0] != get(i)) return;
    
    int cur = d+i;
    if (a) a --, c += i, d += i, cur += i;
    if (b) b --, d += i, cur += i; 
    
    if (a == 0 && !ok(x-2,c)) return;
    if (b == 0 && !ok(x-1,d)) return;
    TMP[b][d][i] += val;
}

void TRI2(int b, int c, int i, int x, int j) {
    if (i+j > 10) return;
    ll val = TMP[b][c][i];
    int cur = c+i;
    if (i+j == 10) {
        if (i == 10) {
            if (tmp[x][1] == '?' || tmp[x][1] == '-') {
                M[b][2][c][cur] += val;
            }
        } else {
            cur += j;
            if (tmp[x][1] == '?' || tmp[x][1] == '/') {
                if (b) b --, c += j, cur += j;
                if (!ok(x-1,c)) return;
                M[b][1][c][cur] += val;
            }
        }
    } else {
        cur += j;
        if (b) b --, c += j, cur += j;
        if (!ok(x-1,c)) return;
        if (!ok(x,cur)) return;
        if (tmp[x][1] == '?' || tmp[x][1] == get(j)) {
            M[0][0][c][cur] += val;
        }
    }
}

ll twoStrike[301], oneStrike[301][10], lastChance[301], noChance[301];

void TRITRI(int b, int c, int i, int x, int j) { // CHECK
    ll val = TMP[b][c][i]; 
    int cur = c+i;
    if (i == 10) {
        if (tmp[x][1] == '?' || tmp[x][1] == get(j)) {
            cur += j;
            if (b) b --, c += j, cur += j;
            if (!ok(x-1,c)) return;
            if (j == 10) twoStrike[cur] += val;
            else oneStrike[cur][j] += val;
        }
        return;
    } 
    if (i+j > 10) return; 
    if (i+j == 10) {
        cur += j;
        if (tmp[x][1] == '?' || tmp[x][1] == '/') {
            if (b) b --, c += j, cur += j;
            if (!ok(x-1,c)) return;
            lastChance[cur] += val;
        }
    } else {
        cur += j;
        if (tmp[x][1] == '?' || tmp[x][1] == get(j)) {
            if (b) b --, c += j, cur += j;
            if (!ok(x-1,c)) return;
            noChance[cur] += val;
        }
    }
}

void finish() {
    F0R(x,301) F0R(i,11) if (tmp[n-1][2] == '?' || tmp[n-1][2] == get(i)) {
        int z = x+i;
        if (num[n-1] == -1 || num[n-1] == z) ans += twoStrike[x];
    }
    F0R(x,301) F0R(i,10) F0R(j,11-i) {
        if (i+j == 10) {
            if (tmp[n-1][2] == '?' || tmp[n-1][2] == '/') {
                int z = x+j;
                if (num[n-1] == -1 || num[n-1] == z) ans += oneStrike[x][i];
            }
        } else {
            if (tmp[n-1][2] == '?' || tmp[n-1][2] == get(j)) {
                int z = x+j;
                if (num[n-1] == -1 || num[n-1] == z) ans += oneStrike[x][i];
            }
        }
    }
    
    F0R(x,301) F0R(j,11) 
        if (tmp[n-1][2] == '?' || tmp[n-1][2] == get(j)) {
            int z = x+j;
            if (num[n-1] == -1 || num[n-1] == z) ans += lastChance[x];
        }
    
    F0R(x,301) 
        if (tmp[n-1][2] == '?' || tmp[n-1][2] == '-') 
            if (num[n-1] == -1 || num[n-1] == x) 
                ans += noChance[x];
}

void process(int x) {
    vector<vi> posi;
    
    if (x != n-1) {
        memset(TMP,0,sizeof TMP);
        F0R(a,2) F0R(b,3) F0R(c,301) FOR(d,c,301) if (m[a][b][c][d])
            F0R(i,11) TRI(a,b,c,d,x,i);
        F0R(b,2) F0R(c,301) F0R(i,11) if (TMP[b][c][i])
            F0R(j,11) TRI2(b,c,i,x,j);
    } else {
        memset(TMP,0,sizeof TMP);
        F0R(a,2) F0R(b,3) F0R(c,301) FOR(d,c,301) if (m[a][b][c][d])
            F0R(i,11) TRI(a,b,c,d,x,i);
        F0R(b,2) F0R(c,301) F0R(i,11) if (TMP[b][c][i])
            F0R(j,11) TRITRI(b,c,i,x,j);
        finish();
    }
            
    F0R(a,2) F0R(b,3) F0R(c,301) FOR(d,c,301) {
        m[a][b][c][d] = M[a][b][c][d];
        M[a][b][c][d] = 0;
    }
}

void solve() {
    cin >> n;
    string s; cin >> s;
    F0R(i,n-1) tmp[i] = s.substr(2*i,2);
    tmp[n-1] = s.substr(2*(n-1),3);
    F0R(i,n) cin >> num[i];
    
    memset(m,0,sizeof m);
    memset(twoStrike,0,sizeof twoStrike);
    memset(oneStrike,0,sizeof oneStrike);
    memset(lastChance,0,sizeof lastChance);
    memset(noChance,0,sizeof noChance);
    
    m[0][0][0][0] = 1;
    ans = 0;
    F0R(i,n) process(i);
    cout << ans << "\n";
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> q;
    F0R(i,q) solve();
}

/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( ) 
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 161 ms 8952 KB Output is correct
2 Correct 167 ms 9064 KB Output is correct
3 Correct 170 ms 9116 KB Output is correct
4 Correct 176 ms 9116 KB Output is correct
5 Correct 213 ms 9116 KB Output is correct
6 Correct 251 ms 9116 KB Output is correct
7 Correct 336 ms 9240 KB Output is correct
8 Correct 293 ms 9240 KB Output is correct
9 Correct 224 ms 9240 KB Output is correct
10 Correct 232 ms 9240 KB Output is correct
11 Correct 352 ms 9240 KB Output is correct
12 Correct 95 ms 9240 KB Output is correct
13 Correct 28 ms 9240 KB Output is correct
14 Correct 87 ms 9272 KB Output is correct
15 Correct 36 ms 9272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 9272 KB Output is correct
2 Correct 208 ms 9272 KB Output is correct
3 Correct 248 ms 9272 KB Output is correct
4 Correct 209 ms 9272 KB Output is correct
5 Correct 239 ms 9272 KB Output is correct
6 Correct 372 ms 9272 KB Output is correct
7 Correct 321 ms 9272 KB Output is correct
8 Correct 402 ms 9272 KB Output is correct
9 Correct 285 ms 9272 KB Output is correct
10 Correct 361 ms 9272 KB Output is correct
11 Correct 302 ms 9272 KB Output is correct
12 Correct 332 ms 9272 KB Output is correct
13 Correct 376 ms 9272 KB Output is correct
14 Correct 341 ms 9272 KB Output is correct
15 Correct 357 ms 9272 KB Output is correct
16 Correct 399 ms 9272 KB Output is correct
17 Correct 347 ms 9272 KB Output is correct
18 Correct 311 ms 9324 KB Output is correct
19 Correct 336 ms 9324 KB Output is correct
20 Correct 371 ms 9324 KB Output is correct
21 Correct 343 ms 9324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 9324 KB Output is correct
2 Correct 232 ms 9324 KB Output is correct
3 Correct 214 ms 9324 KB Output is correct
4 Correct 209 ms 9324 KB Output is correct
5 Correct 186 ms 9324 KB Output is correct
6 Correct 399 ms 9324 KB Output is correct
7 Correct 396 ms 9352 KB Output is correct
8 Correct 274 ms 9352 KB Output is correct
9 Correct 322 ms 9352 KB Output is correct
10 Correct 384 ms 9352 KB Output is correct
11 Correct 414 ms 9352 KB Output is correct
12 Correct 382 ms 9352 KB Output is correct
13 Correct 309 ms 9352 KB Output is correct
14 Correct 357 ms 9352 KB Output is correct
15 Correct 320 ms 9352 KB Output is correct
16 Correct 335 ms 9352 KB Output is correct
17 Correct 293 ms 9352 KB Output is correct
18 Correct 320 ms 9352 KB Output is correct
19 Correct 358 ms 9352 KB Output is correct
20 Correct 386 ms 9352 KB Output is correct
21 Correct 381 ms 9352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 9352 KB Output is correct
2 Correct 206 ms 9352 KB Output is correct
3 Correct 216 ms 9352 KB Output is correct
4 Correct 196 ms 9352 KB Output is correct
5 Correct 215 ms 9352 KB Output is correct
6 Correct 211 ms 9352 KB Output is correct
7 Correct 201 ms 9352 KB Output is correct
8 Correct 173 ms 9352 KB Output is correct
9 Correct 232 ms 9352 KB Output is correct
10 Correct 252 ms 9352 KB Output is correct
11 Correct 253 ms 9352 KB Output is correct
12 Correct 243 ms 9352 KB Output is correct
13 Correct 249 ms 9352 KB Output is correct
14 Correct 310 ms 9352 KB Output is correct
15 Correct 335 ms 9352 KB Output is correct
16 Correct 396 ms 9352 KB Output is correct
17 Correct 319 ms 9352 KB Output is correct
18 Correct 339 ms 9352 KB Output is correct
19 Correct 293 ms 9352 KB Output is correct
20 Correct 304 ms 9352 KB Output is correct
21 Correct 294 ms 9352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 8952 KB Output is correct
2 Correct 167 ms 9064 KB Output is correct
3 Correct 170 ms 9116 KB Output is correct
4 Correct 176 ms 9116 KB Output is correct
5 Correct 213 ms 9116 KB Output is correct
6 Correct 251 ms 9116 KB Output is correct
7 Correct 336 ms 9240 KB Output is correct
8 Correct 293 ms 9240 KB Output is correct
9 Correct 224 ms 9240 KB Output is correct
10 Correct 232 ms 9240 KB Output is correct
11 Correct 352 ms 9240 KB Output is correct
12 Correct 95 ms 9240 KB Output is correct
13 Correct 28 ms 9240 KB Output is correct
14 Correct 87 ms 9272 KB Output is correct
15 Correct 36 ms 9272 KB Output is correct
16 Correct 145 ms 9272 KB Output is correct
17 Correct 208 ms 9272 KB Output is correct
18 Correct 248 ms 9272 KB Output is correct
19 Correct 209 ms 9272 KB Output is correct
20 Correct 239 ms 9272 KB Output is correct
21 Correct 372 ms 9272 KB Output is correct
22 Correct 321 ms 9272 KB Output is correct
23 Correct 402 ms 9272 KB Output is correct
24 Correct 285 ms 9272 KB Output is correct
25 Correct 361 ms 9272 KB Output is correct
26 Correct 302 ms 9272 KB Output is correct
27 Correct 332 ms 9272 KB Output is correct
28 Correct 376 ms 9272 KB Output is correct
29 Correct 341 ms 9272 KB Output is correct
30 Correct 357 ms 9272 KB Output is correct
31 Correct 399 ms 9272 KB Output is correct
32 Correct 347 ms 9272 KB Output is correct
33 Correct 311 ms 9324 KB Output is correct
34 Correct 336 ms 9324 KB Output is correct
35 Correct 371 ms 9324 KB Output is correct
36 Correct 343 ms 9324 KB Output is correct
37 Correct 89 ms 9324 KB Output is correct
38 Correct 232 ms 9324 KB Output is correct
39 Correct 214 ms 9324 KB Output is correct
40 Correct 209 ms 9324 KB Output is correct
41 Correct 186 ms 9324 KB Output is correct
42 Correct 399 ms 9324 KB Output is correct
43 Correct 396 ms 9352 KB Output is correct
44 Correct 274 ms 9352 KB Output is correct
45 Correct 322 ms 9352 KB Output is correct
46 Correct 384 ms 9352 KB Output is correct
47 Correct 414 ms 9352 KB Output is correct
48 Correct 382 ms 9352 KB Output is correct
49 Correct 309 ms 9352 KB Output is correct
50 Correct 357 ms 9352 KB Output is correct
51 Correct 320 ms 9352 KB Output is correct
52 Correct 335 ms 9352 KB Output is correct
53 Correct 293 ms 9352 KB Output is correct
54 Correct 320 ms 9352 KB Output is correct
55 Correct 358 ms 9352 KB Output is correct
56 Correct 386 ms 9352 KB Output is correct
57 Correct 381 ms 9352 KB Output is correct
58 Correct 177 ms 9352 KB Output is correct
59 Correct 206 ms 9352 KB Output is correct
60 Correct 216 ms 9352 KB Output is correct
61 Correct 196 ms 9352 KB Output is correct
62 Correct 215 ms 9352 KB Output is correct
63 Correct 211 ms 9352 KB Output is correct
64 Correct 201 ms 9352 KB Output is correct
65 Correct 173 ms 9352 KB Output is correct
66 Correct 232 ms 9352 KB Output is correct
67 Correct 252 ms 9352 KB Output is correct
68 Correct 253 ms 9352 KB Output is correct
69 Correct 243 ms 9352 KB Output is correct
70 Correct 249 ms 9352 KB Output is correct
71 Correct 310 ms 9352 KB Output is correct
72 Correct 335 ms 9352 KB Output is correct
73 Correct 396 ms 9352 KB Output is correct
74 Correct 319 ms 9352 KB Output is correct
75 Correct 339 ms 9352 KB Output is correct
76 Correct 293 ms 9352 KB Output is correct
77 Correct 304 ms 9352 KB Output is correct
78 Correct 294 ms 9352 KB Output is correct
79 Correct 124 ms 9352 KB Output is correct
80 Correct 220 ms 9352 KB Output is correct
81 Correct 209 ms 9352 KB Output is correct
82 Correct 245 ms 9352 KB Output is correct
83 Correct 195 ms 9352 KB Output is correct
84 Correct 369 ms 9352 KB Output is correct
85 Correct 288 ms 9352 KB Output is correct
86 Correct 379 ms 9352 KB Output is correct
87 Correct 368 ms 9352 KB Output is correct
88 Correct 313 ms 9352 KB Output is correct
89 Correct 364 ms 9352 KB Output is correct
90 Correct 336 ms 9352 KB Output is correct
91 Correct 388 ms 9352 KB Output is correct
92 Correct 281 ms 9352 KB Output is correct
93 Correct 427 ms 9352 KB Output is correct
94 Correct 535 ms 9352 KB Output is correct
95 Correct 491 ms 9372 KB Output is correct
96 Correct 495 ms 9384 KB Output is correct
97 Correct 449 ms 9384 KB Output is correct
98 Correct 435 ms 9384 KB Output is correct
99 Correct 533 ms 9384 KB Output is correct
100 Correct 471 ms 9384 KB Output is correct