QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#420186#8717. 骰子ucup-team173WA 0ms3824kbC++202.7kb2024-05-24 15:13:412024-05-24 15:13:46

Judging History

你现在查看的是最新测评结果

  • [2024-05-24 15:13:46]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3824kb
  • [2024-05-24 15:13:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

constexpr int P = 1e9 + 7;

int qpow(int a, int b) {
    int res = 1;
    while(b) {
        if(b & 1) {
            res = 1LL * res * a % P;
        }
        a = 1LL * a * a % P;
        b >>= 1;
    }
    return res;
}
int inv(int a) {
    return qpow(a, P - 2);
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    vector<int> b(m + 1);
    for(int i = 0; i <= m; i++) {
        cin >> b[i];
    }
    vector p(n, vector<int>(m + 1)), ip(n, vector<int>(m + 1));
    vector<int> del(n, -1);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j <= m; j++) {
            cin >> p[i][j];
            p[i][j] >= P ? p[i][j] -= P : 0;
            if(p[i][j] && del[i] == -1) {
                del[i] = j;
            }
        }
        for(int j = del[i]; j <= m; j++) {
            p[i][j - del[i]] = p[i][j];
            p[i][j] = 0;
        }
        ip[i][0] = inv(p[i][0]);
        for(int j = 1; j <= m - del[i]; j++) {
            int now = 0;
            for(int k = 0; k < j; k++) {
                now = (now + 1LL * ip[i][k] * p[i][j - k]) % P;
            }
            ip[i][j] = (P - 1LL * now * ip[i][0]) % P;
        }
        if(i) {
            del[i] += del[i - 1];
        }
    }
    auto mul = [&](vector<int> &f, vector<int> &g) {
        vector<int> res(m + 1);
        for(int i = 0; i <= m; i++) {
            for(int j = 0; j <= i; j++) {
                res[i] = (res[i] + 1LL * f[j] * g[i - j]) % P;
            }
        }
        return res;  
    };
    vector pre(n, vector<int>(m + 1)), ipre(n, vector<int>(m + 1));
    pre[0] = p[0], ipre[0] = ip[0];
    for(int i = 1; i < n; i++) {
        pre[i] = mul(pre[i - 1], p[i]);
        ipre[i] = mul(ip[i], ipre[i - 1]);
    }
    reverse(b.begin(), b.end());
    for(int i = 0; i < n; i++) {
        pre[i] = mul(pre[i], b);
    }
    while(q--) {
        int l, r;
        cin >> l >> r;
        l--, r--;
        int delx = del[r] - (l ? del[l - 1] : 0);
        if(m - delx < 0) {
            cout << 0 << '\n';
            continue;
        }
        if(l == 0) {
            cout << pre[r][m - delx] << '\n';
            continue;
        }
        int res = 0;
        for(int i = 0; i <= m - delx; i++) {
            res = (res + 1LL * pre[r][i] * ipre[l - 1][m - delx - i]) % P;
        }
        cout << res << '\n';
    }
    return 0;
}
/*
3 3 3
4 3 2 1
0 1 0 1000000007
0 500000004 0 500000004
0 0 500000004 500000004
1 1
1 2
1 3

3 3 6
4 3 2 1
1000000007 0 1 0
1000000007 1 0 0
1000000007 0 1 0
1 1
1 2
1 3
2 2
2 3
3 3

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3524kb

input:

3 3 3
4 3 2 1
0 1 0 1000000007
0 500000004 0 500000004
0 0 500000004 500000004
1 1
1 2
1 3

output:

3
1
0

result:

ok 3 number(s): "3 1 0"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3612kb

input:

3 3 6
4 3 2 1
1000000007 0 1 0
1000000007 1 0 0
1000000007 0 1 0
1 1
1 2
1 3
2 2
2 3
3 3

output:

2
1
0
3
1
2

result:

ok 6 numbers

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3824kb

input:

1 1 1
604063100 57375033
742299910 257700098
1 1

output:

0

result:

wrong answer 1st numbers differ - expected: '148903503', found: '0'