QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#79978 | #5402. 术树数 | DCH233 | 0 | 1173ms | 9740kb | C++14 | 3.1kb | 2023-02-21 15:52:37 | 2023-02-21 15:52:39 |
Judging History
answer
#include <cstdio>
#include <algorithm>
using namespace std;
namespace IO {
#define isdigit(x) (x >= '0' && x <= '9')
template<typename T>
inline void read(T &x) {
x = 0; char ch = getchar(); int f = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
if(f) x = -x;
}
template<typename T>
inline void write(T x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
#undef isdigit
}
using namespace IO;
const int N = 2e5 + 10;
const int M = 25;
int Q, k, m;
int bit[M];
inline int get(int x, int p) {
return x / bit[p] % k;
}
inline int add(int x, int y) {
int z = 0;
for(int i = 0; i < m; ++i) {
int u = get(x, i), v = get(y, i);
if((u += v) >= k) u -= k;
z += u * bit[i];
}
return z;
}
inline int red(int x, int y) {
int z = 0;
for(int i = 0; i < m; ++i) {
int u = get(x, i), v = get(y, i);
if((u -= v) < 0) u += k;
z += u * bit[i];
}
return z;
}
inline int mul(int x, int y) {
int z = 0;
for(int i = 0; i < m; ++i) {
int u = get(x, i);
z += u * y % k * bit[i];
}
return z;
}
inline void out(int x) {
for(int i = m - 1; ~i; --i)
printf("%d",get(x,i));
puts("");
}
inline int kgcd(int x, int y, int p) {
int u = get(x, p), v = get(y, p);
if(v == 0) return x;
else return kgcd(y, red(x, mul(y, u / v)), p);
}
struct LinearBase {
int a[M];
inline void insert(int x) {
for(int i = m - 1; ~i; --i) {
int u = get(x, i);
if(u) {
if(!a[i]) {
a[i] = x;
x = mul(x, k / __gcd(k, u));
}
else a[i] = kgcd(a[i], x, i);
}
}
}
inline int query(int x) {
for(int i = m - 1; ~i; --i) {
int u = get(x, i);
if(u && a[i]) {
int v = get(a[i], i);
x = red(x, mul(a[i], u / v));
}
}
return x;
}
}base;
void init() {
bit[0] = 1;
for(int i = 1; i <= m - 1; ++i)
bit[i] = bit[i - 1] * k;
}
int dep[N], w[N];
int fa[N][18];
inline int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = 17; ~i; --i)
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if(x == y) return x;
for(int i = 17; ~i; --i)
if(fa[x][i] ^ fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int main() {
read(Q), read(k), read(m);
init();
int tot = 1;
for(int i = 1, op, x, y, v; i <= Q; ++i) {
read(op);
if(op == 1) {
read(x), read(v);
fa[++tot][0] = x;
for(int i = 1; i <= 17; ++i)
fa[tot][i] = fa[fa[tot][i - 1]][i - 1];
dep[tot] = dep[x] + 1;
w[tot] = add(w[x], v);
base.insert(mul(v, 2));
}
else if(op == 2) {
read(x), read(y), read(v);
int z = lca(x, y);
int len = red(add(w[x], w[y]), w[z]);
len = add(len, v);
base.insert(len);
}
else {
read(x), read(y);
int z = lca(x, y);
int len = red(add(w[x], w[y]), w[z]);
printf("%d\n",base.query(len));
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 1732kb
input:
30 7 3 1 1 301 1 1 236 1 2 278 3 2 4 3 2 4 2 1 4 265 1 1 242 1 4 278 1 6 337 3 2 3 2 5 7 304 2 5 6 34 1 4 178 3 6 7 3 5 7 3 1 4 1 1 178 3 3 4 3 1 6 3 3 4 2 6 7 131 1 1 213 3 1 3 2 3 10 11 3 4 6 2 5 9 169 1 6 9 2 5 10 29 1 9 111 3 9 11
output:
0 0 259 231 231 294 245 294 245 266 0 126
result:
wrong answer 3rd lines differ - expected: '0', found: '259'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #21:
score: 0
Wrong Answer
time: 1164ms
memory: 9740kb
input:
198517 3 16 1 1 40710744 1 2 21885097 1 1 23592366 1 4 7387074 1 5 16074177 1 1 41027400 1 4 18082971 1 2 12822448 1 1 2286557 1 1 27896295 1 11 14532760 1 8 2357296 1 11 9190559 1 6 40503152 3 4 11 3 1 7 3 3 7 3 8 14 3 12 15 3 2 3 1 10 34606866 1 13 42718465 1 16 30353561 3 5 11 3 2 6 3 16 18 1 3 2...
output:
18155504 22459655 3944246 5360391 36284497 39058176 17339031 25003410 23744523 41973333 35525079 7424613 9787797 10285848 18144246 4904145 21753666 32708331 4459236 16262307 1129716 37700388 31821012 32348304 36500625 36515241 39928500 40786821 24145749 12934146 24886521 31250163 42815064 7067352 38...
result:
wrong answer 1st lines differ - expected: '0', found: '18155504'
Subtask #4:
score: 0
Wrong Answer
Test #26:
score: 0
Wrong Answer
time: 68ms
memory: 7476kb
input:
198891 26426880 1 1 1 0 2 1 2 0 3 1 2 1 1 0 3 2 3 3 1 2 1 2 0 1 2 0 1 3 0 1 6 0 2 1 6 0 2 3 6 0 3 2 3 1 2 13213440 1 2 13213440 1 4 13213440 3 4 10 1 1 13213440 2 3 4 0 2 3 8 13213440 1 1 0 1 12 0 1 13 0 1 3 0 3 2 11 3 2 12 2 11 14 13213440 1 6 0 2 12 14 0 2 1 2 0 1 10 0 1 12 0 2 3 13 0 3 12 14 3 1 ...
output:
0 0 0 0 13213440 13213440 0 0 0 13213440 0 0 13213440 0 13213440 0 13213440 0 13213440 13213440 13213440 13213440 13213440 0 13213440 13213440 0 13213440 13213440 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
result:
wrong answer 33rd lines differ - expected: '13213440', found: '0'
Subtask #5:
score: 0
Wrong Answer
Test #30:
score: 0
Wrong Answer
time: 1173ms
memory: 7276kb
input:
199458 2 25 1 1 31252443 2 1 2 22827339 1 2 13517756 1 2 5635412 1 3 33397078 1 3 33542998 2 3 5 1484991 3 5 6 2 1 3 7938846 2 1 2 3665458 1 3 29150948 3 4 5 1 3 733545 1 7 4698781 1 7 21699192 1 6 10854390 3 3 8 3 4 8 1 2 6889338 2 1 12 27646676 2 6 8 24407215 1 11 20847453 3 4 13 1 6 16891344 3 4 ...
output:
17880679 22137228 18459918 21778522 13109031 21778522 10353267 17052888 27458150 1626541 3556839 5702417 24314533 1245275 82781 17498941 8454420 18227259 24813643 24813643 23105040 11717632 6597293 6597293 28370945 21141893 200352 18000133 2200580 14106157 10348800 18653618 1136310 12743694 7332744 ...
result:
wrong answer 1st lines differ - expected: '150016', found: '17880679'
Subtask #6:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 27ms
memory: 2812kb
input:
49958 1023 2 1 1 122428 1 1 917186 2 2 3 53148 1 3 876461 2 1 3 968146 2 2 4 569341 2 3 4 199413 2 1 4 238371 1 3 127427 1 2 887225 2 1 4 776059 2 4 6 155479 2 1 6 795533 1 5 159578 3 5 6 2 2 5 758778 2 5 6 601115 3 4 7 1 4 202224 2 5 6 902346 3 1 6 3 5 7 3 3 5 1 2 791251 1 5 502214 2 6 7 929048 1 6...
output:
461373 360096 1024023 551397 483879 180048 877734 742698 742698 270072 292578 180048 450120 450120 528891 461373 461373 326337 900240 427614 720192 1024023 528891 618915 686433 540144 483879 540144 742698 630168 798963 348843 540144 933999 123783 1012770 33759 675180 911493 438867 483879 753951 8327...
result:
wrong answer 1st lines differ - expected: '0', found: '461373'
Subtask #7:
score: 0
Skipped
Dependency #1:
0%