QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#415995#8717. 骰子TLE_AutomatTL 4ms3548kbC++207.7kb2024-05-21 14:00:012024-05-21 14:00:02

Judging History

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

  • [2024-05-21 14:00:02]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:3548kb
  • [2024-05-21 14:00:01]
  • 提交

answer

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

#define SZ(x) ((int)((x).size()))
#define lb(x) ((x) & (-(x)))
#define bp(x) __builtin_popcount(x)
#define bpll(x) __builtin_popcountll(x)
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;

namespace Polynomial {
    const int Mod[] = {469762049, 998244353, 1004535809};
    const int G = 3;
    
    template<int mod>
    class Modint {
        private:
            unsigned int x;
        public:
            Modint() = default;
            Modint(unsigned int x): x(x) {}
        unsigned int getx() { return x; }
        friend istream& operator >> (istream& in, Modint& a) {return in >> a.x;}
        friend ostream& operator << (ostream& out, Modint a) {return out << a.x;}
        friend Modint operator + (Modint a, Modint b) {return (a.x + b.x) % mod;}
        friend Modint operator - (Modint a, Modint b) {return (a.x - b.x + mod) % mod;}
        friend Modint operator * (Modint a, Modint b) {return 1ULL * a.x * b.x % mod;}
        friend Modint operator / (Modint a, Modint b) {return a * ~b;}
        friend Modint operator ^ (Modint a, int b) {Modint ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}
        friend Modint operator ~ (Modint a) {return a ^ (mod - 2);}
        friend Modint operator - (Modint a) {return (mod - a.x) % mod;}
        friend Modint& operator += (Modint& a, Modint b) {return a = a + b;}
        friend Modint& operator -= (Modint& a, Modint b) {return a = a - b;}
        friend Modint& operator *= (Modint& a, Modint b) {return a = a * b;}
        friend Modint& operator /= (Modint& a, Modint b) {return a = a / b;}
        friend Modint& operator ^= (Modint& a, int b) {return a = a ^ b;}
        friend Modint& operator ++ (Modint& a) {return a += 1;}
        friend Modint operator ++ (Modint& a, int) {Modint x = a; a += 1; return x;}
        friend Modint& operator -- (Modint& a) {return a -= 1;}
        friend Modint operator -- (Modint& a, int) {Modint x = a; a -= 1; return x;}
        friend bool operator == (Modint a, Modint b) {return a.x == b.x;}
        friend bool operator != (Modint a, Modint b) {return !(a == b);}
    };

    typedef Modint<469762049> mintp0;
    typedef Modint<998244353> mintp1;
    typedef Modint<1004535809> mintp2;

    void init_convo(vector<int>& rev, int& lim, int n, int m) {
        int s = 0;
        for (lim = 1; lim <= n + m; lim <<= 1, s++);
        rev.resize(lim);
        for (int i = 0; i < lim; i++) {
            rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (s - 1));
        }
    }

    template <typename mint>
    void NTT(vector<mint>& f, vector<int>& rev, int lim, int type, int mod) {
        f.resize(lim, 0);
        for (int i = 0; i < lim; i++) {
            if (i < rev[i]) {
                swap(f[i], f[rev[i]]);
            }
        }
        for (int i = 1; i < lim; i <<= 1) {
            mint mul = (mint)G ^ ((mod - 1) / (i << 1));
            if (type == -1) {
                mul = ~mul;
            }
            for (int j = 0; j < lim; j += (i << 1)) {
                mint w = 1;
                for (int k = 0; k < i; k++, w *= mul) {
                    mint x = f[j + k], y = w * f[j + k + i];
                    f[j + k] = x + y;
                    f[j + k + i] = x - y;
                }
            }
        }
    }

    template <typename mint>
    vector<mint> convolution(vector<mint> f, vector<mint> g, int mod) {
        int n = SZ(f) - 1, m = SZ(g) - 1, lim;
        vector<int> rev;
        init_convo(rev, lim, n, m);

        NTT(f, rev, lim, 1, mod);
        NTT(g, rev, lim, 1, mod);
        vector<mint> h(lim);
        for (int i = 0; i < lim; i++) {
            h[i] = f[i] * g[i];
        }
        NTT(h, rev, lim, -1, mod);
        mint invlim = ~(mint)lim;
        for (int i = 0; i < lim; i++) {
            h[i] = h[i] * invlim;
        }
        h.resize(n + m + 1);
        return h;
    }

    ll ksm(ll x, ll y, ll mod) {
        ll res = 1;
        while (y) {
            if (y & 1) {
                res = res * x % mod;
            }
            x = x * x % mod;
            y >>= 1;
        }
        return res;
    }
    ll inv(ll x, ll mod) {
        return ksm(x, mod - 2, mod);
    }
    ll mul(ll a, ll b, ll mod){
	    return (a * b - (ll)((long double)a / mod * b) * mod + mod) % mod;
    }

    vector<int> convolution(const vector<int>& f, const vector<int>& g, int mod) {
        int n = SZ(f) - 1, m = SZ(g) - 1;
        vector res(3, vector(n + m + 1, 0));

        {
            vector<mintp0> F, G; 
            F.resize(n + 1);
            G.resize(m + 1);
            for (int i = 0; i <= n; i++) {
                F[i] = f[i];
            }
            for (int i = 0; i <= m; i++) {
                G[i] = g[i];
            }
            auto cur = convolution(F, G, Mod[0]);
            for (int i = 0; i <= n + m; i++) {
                res[0][i] = cur[i].getx();
            }
        }

        {
            vector<mintp1> F, G; 
            F.resize(n + 1);
            G.resize(m + 1);
            for (int i = 0; i <= n; i++) {
                F[i] = f[i];
            }
            for (int i = 0; i <= m; i++) {
                G[i] = g[i];
            }
            auto cur = convolution(F, G, Mod[1]);
            for (int i = 0; i <= n + m; i++) {
                res[1][i] = cur[i].getx();
            }
        }

        {
            vector<mintp2> F, G; 
            F.resize(n + 1);
            G.resize(m + 1);
            for (int i = 0; i <= n; i++) {
                F[i] = f[i];
            }
            for (int i = 0; i <= m; i++) {
                G[i] = g[i];
            }
            auto cur = convolution(F, G, Mod[2]);
            for (int i = 0; i <= n + m; i++) {
                res[2][i] = cur[i].getx();
            }
        }
        
        vector<int> h;
        h.resize(n + m + 1);
        ll p0 = Mod[0], p1 = Mod[1], p2 = Mod[2];
        ll k0 = inv(p1, p0), k1 = inv(p0, p1);
        ll ip0 = inv(p0, p2), ip1 = inv(p1, p2);
        for (int i = 0; i <= n + m; i++) {
            ll a0 = res[0][i], a1 = res[1][i], a2 = res[2][i];
            ll m = p0 * p1;
            ll a3 = (mul(a0, mul(k0, p1, m), m) + mul(a1, mul(k1, p0, m), m)) % m;
            ll a4 = mul(mul(((a2 - a3) % p2 + p2) % p2, ip0, p2), ip1, p2);
            h[i] = (mul(mul(a4, p0, mod), p1, mod) + a3) % mod; 
        }
        return h;
    }
}
using Polynomial::convolution;

const int MAXN = 1510;
const int Mod = 1e9 + 7;

vector<int> ans[MAXN][MAXN];

void solve() {
    int n, m, q;
    cin >> n >> m >> q;
    vector b(m + 1, 0);
    for (int i = 0; i <= m; i++) {
        cin >> b[i];
    }

    vector p(n + 1, vector(m + 1, 0));
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            cin >> p[i][j];
            p[i][j] %= Mod;
        }
    }

    for (int i = 1; i <= n; i++) {
        ans[i][i] = p[i];
        for (int j = i + 1; j <= n; j++) {
            ans[i][j] = convolution(ans[i][j - 1], p[j], Mod);
            ans[i][j].resize(m + 1);
        }
    }

    while (q--) {
        int l, r;
        cin >> l >> r;
        int res = 0;
        for (int i = 0; i <= m; i++) {
            res = (res + 1ll * ans[l][r][i] * b[i]) % Mod;
        }
        cout << res << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int T = 1;
    while (T--) solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 3528kb

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: 3528kb

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: 0
Accepted
time: 2ms
memory: 3548kb

input:

1 1 1
604063100 57375033
742299910 257700098
1 1

output:

148903503

result:

ok 1 number(s): "148903503"

Test #4:

score: -100
Time Limit Exceeded

input:

1500 200 600000
253665324 876103781 804024983 929290295 908790466 176299158 528078340 696679927 416465140 509641654 705083449 361711737 250659645 735832780 35321360 383752049 203979021 178832532 785212637 514502839 169840231 65809146 504755349 516829442 382478309 901925498 142312128 782336477 741339...

output:


result: