# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1136134 | Math4Life2020 | September (APIO24_september) | C++20 | 0 ms | 0 KiB |
//testing online c++ -> c converters
#include <cstdio.h>
#include <cstdlib.h>
typedef long long ll;
typedef __int128 lll;
const lll p1 = 144403552893599;
const lll a1 = 981372590237;
const lll p2 = 270969106631203;
const lll a2 = 898374183250;
int solve(int N, int M, int* F, int** S1) {
int** S = (int**)malloc(M * sizeof(int*));
for (int i = 0; i < M; i++) {
S[i] = (int*)malloc((N + 1) * sizeof(int));
for (int j = 0; j < N; j++) {
S[i][j] = S1[i][j];
}
S[i][N] = 0;
}
lll* hsh1 = (lll*)malloc(M * sizeof(lll));
lll* hsh2 = (lll*)malloc(M * sizeof(lll));
for (int i = 0; i < M; i++) {
hsh1[i] = 1;
hsh2[i] = 1;
}
ll* stops = (ll*)malloc((N + 1) * sizeof(ll));
stops[0] = 0;
int stop_count = 1;
for (ll i = 0; i < N; i++) {
for (ll j = 0; j < M; j++) {
hsh1[j] = (hsh1[j] * (a1 + S[j][i])) % p1;
hsh2[j] = (hsh2[j] * (a2 + S[j][i])) % p2;
}
int split = 1;
for (ll j = 0; j < M; j++) {
split = split && (hsh1[j] == hsh1[0]) && (hsh2[j] == hsh2[0]);
}
if (split) {
stops[stop_count++] = i + 1;
for (ll j = 0; j < M; j++) {
hsh1[j] = 1;
hsh2[j] = 1;
}
}
}
ll* kv = (ll*)malloc(N * sizeof(ll));
ll kind = 0;
ll* delactive = (ll*)malloc(N * sizeof(ll));
for (ll i = 0; i < N; i++) {
delactive[i] = 0;
if (stops[kind + 1] <= i) {
kind++;
}
kv[S[0][i]] = kind;
}
for (ll i = 1; i < N; i++) {
ll x = kv[i];
ll y = kv[F[i]];
if (y < x) {
delactive[y]++;
delactive[x]--;
}
}
ll nact = 0;
ll ans = 0;
for (ll i = 0; i < (stop_count - 1); i++) {
nact += delactive[i];
if (nact == 0) {
ans++;
}
}
free(hsh1);
free(hsh2);
free(kv);
free(delactive);
for (int i = 0; i < M; i++) {
free(S[i]);
}
free(S);
free(stops);
return (ans - 1);
}