Submission #412837

# Submission time Handle Problem Language Result Execution time Memory
412837 2021-05-27T16:18:47 Z couplefire Semafor (COI20_semafor) C++17
100 / 100
258 ms 836 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int MOD = 1000000007;

inline int add(int a, int b){return (a+b>=MOD)?a+b-MOD:a+b;}
inline void inc(int& a, int b){a = add(a, b);}
inline int sub(int a, int b){return (a-b<0)?a-b+MOD:a-b;}
inline void dec(int &a, int b){a = sub(a, b);}
inline int mul(int a, int b){return 1ll*a*b%MOD;}
inline void grow(int &a, int b){a = mul(a, b);}

int bpow(int a, int b){
    int res = 1;
    while(b > 0){
        if(b&1) grow(res, a);
        grow(a, a);
        b >>= 1;
    }
    return res;
}

int inv(int a){return bpow(a, MOD-2);}

struct Matrix{
    int n, m;
    vector<vector<int>> mat;
    Matrix(){}
    Matrix(int _n, int _m){
        n = _n, m = _m;
        mat.assign(n, vector<int>(m, 0));
    }
    void setID(){
        assert(n == m);
        for(int i = 0; i<n; i++)
            mat[i][i] = 1;
    }
    Matrix operator * (const Matrix &o) const{
        Matrix res(n, o.m);
        for(int i = 0; i<n; i++)
            for(int j = 0; j<o.m; j++)
                for(int k = 0; k<m; k++)
                    inc(res.mat[i][j], mul(mat[i][k], o.mat[k][j]));
        return res; 
    }
};

Matrix matpow(Matrix a, int b){
    assert(a.n == a.m);
    Matrix res(a.n, a.n); res.setID();
    while(b > 0){
        if(b&1) res = res*a;
        a = a*a;
        b >>= 1;
    }
    return res;
}

int n_mask[10] = {10, 2, 9, 7, 18, 21, 12, 3, 29, 23};

int m, n, k, x;
Matrix help, g, tmp_help;
int comb[15][15];
Matrix trans, ans, tmp_trans;

void solveHelp(int siz){
    help = Matrix(1, siz+1);
    help.mat[0][0] = 1;
    g = Matrix(siz+1, siz+1);
    for(int i = 0; i<siz; i++)
        g.mat[i][i+1] = mul(mul(comb[siz][i], siz-i), inv(comb[siz][i+1])),
        g.mat[i+1][i] = mul(mul(comb[siz][i+1], i+1), inv(comb[siz][i]));
    tmp_help = help*matpow(g, n%k);
    help = help*matpow(g, k);
}

signed main(){
    // freopen("a.in", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> m >> n >> k >> x;
    comb[0][0] = 1;
    for(int i = 1; i<=10; i++){
        comb[i][0] = comb[i][i] = 1;
        for(int j = 1; j<i; j++)
            comb[i][j] = add(comb[i-1][j-1], comb[i-1][j]);
    }
    solveHelp(m*5);
    if(m == 1){
        trans = Matrix(10, 10);
        tmp_trans = Matrix(10, 10);
        ans = Matrix(1, 10);
        for(int i = 0; i<10; i++)
            for(int j = 0; j<10; j++)
                trans.mat[i][j] = help.mat[0][__builtin_popcount(n_mask[i]^n_mask[j])],
                tmp_trans.mat[i][j] = tmp_help.mat[0][__builtin_popcount(n_mask[i]^n_mask[j])];
        ans.mat[0][x] = 1;
        ans = ans*matpow(trans, n/k);
        ans = ans*tmp_trans;
        for(int i = 0; i<10; i++)
            cout << ans.mat[0][i] << '\n';
    } else{
        trans = Matrix(100, 100);
        tmp_trans = Matrix(100, 100);
        ans = Matrix(1, 100);
        for(int a = 0; a<10; a++)
            for(int b = 0; b<10; b++)
                for(int c = 0; c<10; c++)
                    for(int d = 0; d<10; d++)
                        trans.mat[a+10*b][c+10*d] = 
                        help.mat[0][__builtin_popcount(n_mask[a]^n_mask[c])+__builtin_popcount(n_mask[b]^n_mask[d])],
                        tmp_trans.mat[a+10*b][c+10*d] = 
                        tmp_help.mat[0][__builtin_popcount(n_mask[a]^n_mask[c])+__builtin_popcount(n_mask[b]^n_mask[d])];
        ans.mat[0][x] = 1;
        ans = ans*matpow(trans, n/k);
        ans = ans*tmp_trans;
        for(int i = 0; i<100; i++)
            cout << ans.mat[0][i] << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 732 KB Output is correct
2 Correct 8 ms 668 KB Output is correct
3 Correct 8 ms 716 KB Output is correct
4 Correct 8 ms 716 KB Output is correct
5 Correct 8 ms 716 KB Output is correct
6 Correct 8 ms 732 KB Output is correct
7 Correct 8 ms 716 KB Output is correct
8 Correct 8 ms 716 KB Output is correct
9 Correct 9 ms 732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 716 KB Output is correct
2 Correct 131 ms 736 KB Output is correct
3 Correct 198 ms 716 KB Output is correct
4 Correct 258 ms 736 KB Output is correct
5 Correct 241 ms 740 KB Output is correct
6 Correct 228 ms 728 KB Output is correct
7 Correct 251 ms 836 KB Output is correct
8 Correct 236 ms 716 KB Output is correct
9 Correct 244 ms 732 KB Output is correct
10 Correct 244 ms 836 KB Output is correct
11 Correct 29 ms 716 KB Output is correct
12 Correct 12 ms 728 KB Output is correct
13 Correct 245 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 716 KB Output is correct
2 Correct 131 ms 736 KB Output is correct
3 Correct 198 ms 716 KB Output is correct
4 Correct 258 ms 736 KB Output is correct
5 Correct 241 ms 740 KB Output is correct
6 Correct 228 ms 728 KB Output is correct
7 Correct 251 ms 836 KB Output is correct
8 Correct 236 ms 716 KB Output is correct
9 Correct 244 ms 732 KB Output is correct
10 Correct 244 ms 836 KB Output is correct
11 Correct 29 ms 716 KB Output is correct
12 Correct 12 ms 728 KB Output is correct
13 Correct 245 ms 716 KB Output is correct
14 Correct 25 ms 736 KB Output is correct
15 Correct 97 ms 728 KB Output is correct
16 Correct 146 ms 736 KB Output is correct
17 Correct 194 ms 716 KB Output is correct
18 Correct 214 ms 732 KB Output is correct
19 Correct 178 ms 716 KB Output is correct
20 Correct 208 ms 716 KB Output is correct
21 Correct 242 ms 836 KB Output is correct
22 Correct 237 ms 720 KB Output is correct
23 Correct 194 ms 716 KB Output is correct
24 Correct 215 ms 716 KB Output is correct
25 Correct 200 ms 736 KB Output is correct
26 Correct 15 ms 732 KB Output is correct
27 Correct 22 ms 700 KB Output is correct
28 Correct 151 ms 736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 732 KB Output is correct
2 Correct 8 ms 668 KB Output is correct
3 Correct 8 ms 716 KB Output is correct
4 Correct 8 ms 716 KB Output is correct
5 Correct 8 ms 716 KB Output is correct
6 Correct 8 ms 732 KB Output is correct
7 Correct 8 ms 716 KB Output is correct
8 Correct 8 ms 716 KB Output is correct
9 Correct 9 ms 732 KB Output is correct
10 Correct 50 ms 716 KB Output is correct
11 Correct 131 ms 736 KB Output is correct
12 Correct 198 ms 716 KB Output is correct
13 Correct 258 ms 736 KB Output is correct
14 Correct 241 ms 740 KB Output is correct
15 Correct 228 ms 728 KB Output is correct
16 Correct 251 ms 836 KB Output is correct
17 Correct 236 ms 716 KB Output is correct
18 Correct 244 ms 732 KB Output is correct
19 Correct 244 ms 836 KB Output is correct
20 Correct 29 ms 716 KB Output is correct
21 Correct 12 ms 728 KB Output is correct
22 Correct 245 ms 716 KB Output is correct
23 Correct 25 ms 736 KB Output is correct
24 Correct 97 ms 728 KB Output is correct
25 Correct 146 ms 736 KB Output is correct
26 Correct 194 ms 716 KB Output is correct
27 Correct 214 ms 732 KB Output is correct
28 Correct 178 ms 716 KB Output is correct
29 Correct 208 ms 716 KB Output is correct
30 Correct 242 ms 836 KB Output is correct
31 Correct 237 ms 720 KB Output is correct
32 Correct 194 ms 716 KB Output is correct
33 Correct 215 ms 716 KB Output is correct
34 Correct 200 ms 736 KB Output is correct
35 Correct 15 ms 732 KB Output is correct
36 Correct 22 ms 700 KB Output is correct
37 Correct 151 ms 736 KB Output is correct
38 Correct 12 ms 728 KB Output is correct
39 Correct 8 ms 716 KB Output is correct
40 Correct 9 ms 716 KB Output is correct
41 Correct 9 ms 732 KB Output is correct
42 Correct 9 ms 716 KB Output is correct
43 Correct 11 ms 736 KB Output is correct
44 Correct 9 ms 716 KB Output is correct
45 Correct 247 ms 720 KB Output is correct
46 Correct 229 ms 716 KB Output is correct
47 Correct 14 ms 716 KB Output is correct
48 Correct 9 ms 732 KB Output is correct
49 Correct 9 ms 716 KB Output is correct
50 Correct 8 ms 716 KB Output is correct
51 Correct 8 ms 732 KB Output is correct
52 Correct 73 ms 716 KB Output is correct