Submission #979896

#TimeUsernameProblemLanguageResultExecution timeMemory
979896vjudge1Genetics (BOI18_genetics)C++17
100 / 100
843 ms83388 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int maxN  = 4'123;

const ll Mod    = 1'696'969'997;
const ll maxW   = 1'234'567;

mt19937_64 rng(Mod);
uniform_int_distribution<ll> uid(1, maxW);

ll madd(ll x, ll y) { return (x + y) % Mod; }
ll msub(ll x, ll y) { return (x - y + Mod) % Mod; }
ll mmul(ll x, ll y) { return (x * y) % Mod; }

char id[256];

int N, M, K;
int A[maxN][maxN];
ll sum_w, row_w[maxN];
ll col_sum[4][maxN];

int main() {
    id['A'] = 0; id['C'] = 1; id['G'] = 2; id['T'] = 3;

    scanf("%d %d %d", &N, &M, &K);
    char buf;
    for (int i = 1; i <= N; i++) {
        row_w[i] = uid(rng);
        sum_w = madd(sum_w, row_w[i]);

        for (int j = 1; j <= M; j++) {
            scanf(" %c", &buf);
            A[i][j] = id[buf];
        }
    }

    for (int c = 0; c < 4; c++) {
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= M; j++) {
                if (A[i][j] == c) col_sum[c][j] = madd(col_sum[c][j], row_w[i]);
            }
        }
    }

    for (int r = 1; r <= N; r++) {
        ll target = mmul(M - K, msub(sum_w, row_w[r])); // must match at M - K positions

        ll cur = 0;
        for (int c = 1; c <= M; c++) {
            cur = madd(cur, msub(col_sum[A[r][c]][c], row_w[r]));
        }

        if (target == cur) {
            printf("%d\n", r);
            exit(0);
        }
    }
}

Compilation message (stderr)

genetics.cpp: In function 'int main()':
genetics.cpp:36:26: warning: array subscript has type 'char' [-Wchar-subscripts]
   36 |             A[i][j] = id[buf];
      |                          ^~~
genetics.cpp:28:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     scanf("%d %d %d", &N, &M, &K);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
genetics.cpp:35:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |             scanf(" %c", &buf);
      |             ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...