답안 #381720

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
381720 2021-03-25T18:35:09 Z VEGAnn Semafor (COI20_semafor) C++14
100 / 100
242 ms 876 KB
#include <bits/stdc++.h>
#define i2 array<int,2>
#define sz(x) ((int)x.size())
using namespace std;
typedef long long ll;
const int PW = 5;
const int M = (1 << PW);
const int N = 100;
const int SN = 11;
const int md = int(1e9) + 7;
int digs[10];
bool mrk[M];
ll m, n, k, x;

struct matrix{
    int a[M][M];
};

matrix base, base_k, base_nw;

int sum(int x, int y){
    x += y;
    if (x >= md)
        x -= md;
    return x;
}

int mult(int x, int y) { return (1ll * x * y) % md; }

matrix mat_mult(matrix a, matrix b){
    matrix res;

    for (int i = 0; i < M; i++)
    for (int j = 0; j < M; j++) {
        int nw = 0;

        for (int k = 0; k < M; k++)
            nw = sum(nw, mult(a.a[i][k], b.a[k][j]));

        res.a[i][j] = nw;
    }

    return res;
}

matrix binpow(matrix a, ll po){
    matrix res;
    bool was = 0;

    while (po > 0){
        if (po & 1){
            if (!was)
                res = a;
            else res = mat_mult(res, a);

            was = 1;
        }

        a = mat_mult(a, a);
        po >>= 1;
    }

    return res;
}

struct matrix_sml{
    int a[SN][SN];
};

struct matrix_big{
    int a[N][N];
};

int cnt_ways[SN];

matrix_sml mat_mult_sml(matrix_sml a, matrix_sml b){
    matrix_sml res;

    for (int i = 0; i < SN; i++)
    for (int j = 0; j < SN; j++) {
        int nw = 0;

        for (int k = 0; k < SN; k++)
            nw = sum(nw, mult(a.a[i][k], b.a[k][j]));

        res.a[i][j] = nw;
    }

    return res;
}

matrix_sml binpow_sml(matrix_sml a, ll po){
    matrix_sml res;
    bool was = 0;

    while (po > 0){
        if (po & 1){
            if (!was)
                res = a;
            else res = mat_mult_sml(res, a);

            was = 1;
        }

        a = mat_mult_sml(a, a);
        po >>= 1;
    }

    return res;
}

int get_mask(int x){
    return (digs[x / 10] << PW) + digs[x % 10];
}

int P(ll n, ll k){
    int res = 1;

    for (ll x = 0; x < k; x++)
        res = mult(res, (n - x) % md);

    return res;
}

matrix_big mat_mult_big(matrix_big a, matrix_big b){
    matrix_big res;

    for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
        int nw = 0;

        for (int k = 0; k < N; k++)
            nw = sum(nw, mult(a.a[i][k], b.a[k][j]));

        res.a[i][j] = nw;
    }

    return res;
}

matrix_big binpow_big(matrix_big a, ll po){
    matrix_big res;
    bool was = 0;

    while (po > 0){
        if (po & 1){
            if (!was)
                res = a;
            else res = mat_mult_big(res, a);

            was = 1;
        }

        a = mat_mult_big(a, a);
        po >>= 1;
    }

    return res;
}

void calc_cnt_ways(ll k){
    matrix_sml base;

    for (int i = 0; i < SN; i++){
        for (int j = 0; j < SN; j++)
            base.a[i][j] = 0;

        if (i > 0)
            base.a[i][i - 1] = i;

        if (i + 1 < SN)
            base.a[i][i + 1] = (10 - i);
    }

    matrix_sml cur = binpow_sml(base, k);

    for (int i = 0; i < 11; i++)
        cnt_ways[i] = cur.a[i][0];
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#endif // _LOCAL

    cin >> m >> n >> k >> x;

    digs[1] = 8;
    digs[2] = 18;
    digs[3] = 28;
    digs[4] = 9;
    digs[5] = 21;
    digs[6] = 6;
    digs[7] = 24;
    digs[8] = 23;
    digs[9] = 29;
    digs[0] = 10;

    if (m == 1){
        for (int i = 0; i < 10; i++)
            mrk[digs[i]] = 1;

        for (int i = 0; i < M; i++){
            for (int j = 0; j < PW; j++)
                base.a[i][(i ^ (1 << j))] = 1;
        }

        base_k = binpow(base, k);

        /// in base_k you need transitions only between digits

        for (int i = 0; i < M; i++)
        for (int j = 0; j < M; j++)
            if (!mrk[i] || !mrk[j])
                base_k.a[i][j] = 0;

        base_k = binpow(base_k, n / k);

        if (n % k > 0){
            /// calc one more matirix pls

            base_nw = binpow(base, n % k);

            for (int i = 0; i < M; i++)
            for (int j = 0; j < M; j++)
                if (!mrk[i] || !mrk[j])
                    base_nw.a[i][j] = 0;

            base_k = mat_mult(base_k, base_nw);
        }

        for (int i = 0; i < 10; i++)
            cout << base_k.a[digs[x]][digs[i]] << '\n';

        return 0;
    }

    calc_cnt_ways(k);

    matrix_big base;

    /// calc base (big)

    for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++){
        int fi_msk = get_mask(i);
        int se_msk = get_mask(j);

        int dif = __builtin_popcount(fi_msk ^ se_msk);

//        if (dif > k){
//            base.a[i][j] = 0;
//            continue;
//        }

        base.a[i][j] = cnt_ways[dif];
    }

    matrix_big base_n = binpow_big(base, n / k);

    if (n % k > 0){
        calc_cnt_ways(n % k);

        matrix_big base_nw;

        for (int i = 0; i < 100; i++)
        for (int j = 0; j < 100; j++){
            int fi_msk = get_mask(i);
            int se_msk = get_mask(j);

            int dif = __builtin_popcount(fi_msk ^ se_msk);

            if (dif > k){
                base_nw.a[i][j] = 0;
                continue;
            }

            base_nw.a[i][j] = cnt_ways[dif];
        }

        base_n = mat_mult_big(base_n, base_nw);
    }

    for (int i = 0; i < 100; i++)
        cout << base_n.a[x][i] << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 3 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 ms 364 KB Output is correct
3 Correct 3 ms 364 KB Output is correct
4 Correct 2 ms 364 KB Output is correct
5 Correct 2 ms 364 KB Output is correct
6 Correct 2 ms 364 KB Output is correct
7 Correct 2 ms 364 KB Output is correct
8 Correct 2 ms 364 KB Output is correct
9 Correct 2 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 6 ms 364 KB Output is correct
12 Correct 8 ms 364 KB Output is correct
13 Correct 12 ms 364 KB Output is correct
14 Correct 18 ms 364 KB Output is correct
15 Correct 15 ms 364 KB Output is correct
16 Correct 16 ms 364 KB Output is correct
17 Correct 15 ms 364 KB Output is correct
18 Correct 8 ms 364 KB Output is correct
19 Correct 12 ms 456 KB Output is correct
20 Correct 19 ms 364 KB Output is correct
21 Correct 11 ms 364 KB Output is correct
22 Correct 8 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 620 KB Output is correct
2 Correct 5 ms 620 KB Output is correct
3 Correct 4 ms 620 KB Output is correct
4 Correct 4 ms 620 KB Output is correct
5 Correct 4 ms 620 KB Output is correct
6 Correct 4 ms 620 KB Output is correct
7 Correct 4 ms 620 KB Output is correct
8 Correct 4 ms 620 KB Output is correct
9 Correct 4 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 772 KB Output is correct
2 Correct 113 ms 632 KB Output is correct
3 Correct 192 ms 760 KB Output is correct
4 Correct 236 ms 760 KB Output is correct
5 Correct 231 ms 748 KB Output is correct
6 Correct 210 ms 760 KB Output is correct
7 Correct 242 ms 748 KB Output is correct
8 Correct 224 ms 620 KB Output is correct
9 Correct 226 ms 620 KB Output is correct
10 Correct 212 ms 748 KB Output is correct
11 Correct 23 ms 620 KB Output is correct
12 Correct 8 ms 748 KB Output is correct
13 Correct 220 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 772 KB Output is correct
2 Correct 113 ms 632 KB Output is correct
3 Correct 192 ms 760 KB Output is correct
4 Correct 236 ms 760 KB Output is correct
5 Correct 231 ms 748 KB Output is correct
6 Correct 210 ms 760 KB Output is correct
7 Correct 242 ms 748 KB Output is correct
8 Correct 224 ms 620 KB Output is correct
9 Correct 226 ms 620 KB Output is correct
10 Correct 212 ms 748 KB Output is correct
11 Correct 23 ms 620 KB Output is correct
12 Correct 8 ms 748 KB Output is correct
13 Correct 220 ms 748 KB Output is correct
14 Correct 25 ms 748 KB Output is correct
15 Correct 86 ms 748 KB Output is correct
16 Correct 134 ms 876 KB Output is correct
17 Correct 172 ms 748 KB Output is correct
18 Correct 193 ms 760 KB Output is correct
19 Correct 162 ms 748 KB Output is correct
20 Correct 190 ms 876 KB Output is correct
21 Correct 208 ms 620 KB Output is correct
22 Correct 214 ms 876 KB Output is correct
23 Correct 177 ms 748 KB Output is correct
24 Correct 182 ms 760 KB Output is correct
25 Correct 180 ms 876 KB Output is correct
26 Correct 14 ms 748 KB Output is correct
27 Correct 22 ms 876 KB Output is correct
28 Correct 133 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 620 KB Output is correct
2 Correct 5 ms 620 KB Output is correct
3 Correct 4 ms 620 KB Output is correct
4 Correct 4 ms 620 KB Output is correct
5 Correct 4 ms 620 KB Output is correct
6 Correct 4 ms 620 KB Output is correct
7 Correct 4 ms 620 KB Output is correct
8 Correct 4 ms 620 KB Output is correct
9 Correct 4 ms 620 KB Output is correct
10 Correct 47 ms 772 KB Output is correct
11 Correct 113 ms 632 KB Output is correct
12 Correct 192 ms 760 KB Output is correct
13 Correct 236 ms 760 KB Output is correct
14 Correct 231 ms 748 KB Output is correct
15 Correct 210 ms 760 KB Output is correct
16 Correct 242 ms 748 KB Output is correct
17 Correct 224 ms 620 KB Output is correct
18 Correct 226 ms 620 KB Output is correct
19 Correct 212 ms 748 KB Output is correct
20 Correct 23 ms 620 KB Output is correct
21 Correct 8 ms 748 KB Output is correct
22 Correct 220 ms 748 KB Output is correct
23 Correct 25 ms 748 KB Output is correct
24 Correct 86 ms 748 KB Output is correct
25 Correct 134 ms 876 KB Output is correct
26 Correct 172 ms 748 KB Output is correct
27 Correct 193 ms 760 KB Output is correct
28 Correct 162 ms 748 KB Output is correct
29 Correct 190 ms 876 KB Output is correct
30 Correct 208 ms 620 KB Output is correct
31 Correct 214 ms 876 KB Output is correct
32 Correct 177 ms 748 KB Output is correct
33 Correct 182 ms 760 KB Output is correct
34 Correct 180 ms 876 KB Output is correct
35 Correct 14 ms 748 KB Output is correct
36 Correct 22 ms 876 KB Output is correct
37 Correct 133 ms 748 KB Output is correct
38 Correct 14 ms 780 KB Output is correct
39 Correct 8 ms 748 KB Output is correct
40 Correct 8 ms 748 KB Output is correct
41 Correct 8 ms 748 KB Output is correct
42 Correct 9 ms 748 KB Output is correct
43 Correct 8 ms 748 KB Output is correct
44 Correct 8 ms 748 KB Output is correct
45 Correct 213 ms 748 KB Output is correct
46 Correct 206 ms 620 KB Output is correct
47 Correct 12 ms 748 KB Output is correct
48 Correct 8 ms 748 KB Output is correct
49 Correct 4 ms 620 KB Output is correct
50 Correct 4 ms 620 KB Output is correct
51 Correct 8 ms 748 KB Output is correct
52 Correct 65 ms 876 KB Output is correct