QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#473122 | #5402. 术树数 | Doqe | 0 | 0ms | 0kb | C++14 | 1.9kb | 2024-07-11 22:05:17 | 2024-07-11 22:05:18 |
answer
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,B=20;
int q,k,m;
int fa[N][B],dep[N];
inline int LCA(int u,int v)
{
if(dep[u]>dep[v])swap(u,v);
int D=dep[v]-dep[u];
if(D)for(int i=__lg(D);~i;--i)if(D>>i&1)v=fa[v][i];
if(u==v)return u;
for(int i=__lg(dep[u]);~i;--i)
if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i];
return fa[u][0];
}
inline int add(int x,int y){int z=0,p=1;while(x||y)z+=(x%k+y%k)%k*p,x/=k,y/=k,p*=k;return z;}
inline int dec(int x,int y){int z=0,p=1;while(x||y)z+=(x%k+k-y%k)%k*p,x/=k,y/=k,p*=k;return z;}
inline int mul(int x,int w){int z=0,p=1;while(x )z+=(x%k*w)%k*p,x/=k,p*=k;return z;}
int Base[25];
void ini(){Base[0]=1;for(int i=1;i<=m;++i)Base[i]=Base[i-1]*k;}
inline int get(int x,int i){return x/Base[i]%k;}
inline int exgcd(int a,int b,int&x,int&y)
{
if(!b){x=1,y=0;return a;}
int d=exgcd(b,a%b,y,x);y-=a/b*x;
return d;
}
namespace Basis
{
int a[N];
inline void ins(int x)
{
for(int i=m-1,w;~i;--i)if(w=get(x,i))
{
if(!a[i])
{
int p,q,d=exgcd(get(x,i),k,p,q);
a[i]=mul(x,p);x=mul(x,k/d);
}
else
{
while(get(x,i))
{
int d=get(a[i],i)/get(x,i);
dec(a[i],mul(x,d));
swap(x,a[i]);
}
}
}
}
inline int ask(int x)
{
for(int i=m-1;~i;--i)
{
int u=get(x,i);
if(u&&a[i])x=dec(x,mul(a[i],u/get(a[i],i)));
}
return x;
}
}
int val[N],tt=1;
int main()
{
cin.tie(0)->sync_with_stdio(0);
cin>>q>>k>>m;
ini();
while(q--)
{
int o,x,y,v;cin>>o;
if(o==1)
{
int x,v;cin>>x>>v;
val[++tt]=add(val[x],v);
dep[tt]=dep[x]+1;
fa[tt][0]=x;
for(int i=1;i<B;++i)fa[tt][i]=fa[fa[tt][i-1]][i-1];
Basis::ins(mul(v,2));
}
else if(o==2)
{
cin>>x>>y>>v;
Basis::ins(add(dec(add(val[x],val[y]),mul(val[LCA(x,y)],2)),v));
}
else
{
cin>>x>>y;
cout<<Basis::ask(dec(add(val[x],val[y]),mul(val[LCA(x,y)],2)))<<"\n";
}
}
}
详细
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
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:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Time Limit Exceeded
Test #21:
score: 0
Time Limit Exceeded
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:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #26:
score: 0
Time Limit Exceeded
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:
result:
Subtask #5:
score: 0
Time Limit Exceeded
Test #30:
score: 0
Time Limit Exceeded
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:
result:
Subtask #6:
score: 0
Time Limit Exceeded
Test #33:
score: 0
Time Limit Exceeded
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:
result:
Subtask #7:
score: 0
Skipped
Dependency #1:
0%