QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#166316 | #5402. 术树数 | Lynkcat# | 0 | 0ms | 0kb | C++20 | 4.4kb | 2023-09-06 08:28:56 | 2024-07-04 01:52:53 |
answer
#include<bits/stdc++.h>
#define poly vector<int>
#define IOS ios::sync_with_stdio(false)
#define ll long long
#define mp make_pair
#define mt make_tuple
#define pa pair < int,int >
#define fi first
#define se second
#define inf 1e18
#define mod 998244353
#define sz(x) ((int)((x).size()))
#define int ll
// #define N
using namespace std;
int quickPower(int x,int y)
{
int res=1;
while (y){
if (y&1) res=res*x;
x=x*x;
y>>=1;
}
return res;
}
void BellaKira()
{
int Q,K,m;
cin>>Q>>K>>m;
int V=quickPower(K,m);
vector<int>gd(V+1,0);
for (int i=1;i<=V;i++) gd[i]=__gcd(i,V);
vector<poly>fa(Q+1,poly(20,0)),dis(Q+1,poly(30,0));
vector<poly>basis(30,poly());
vector<int>dep(Q+1,0);
dep[1]=1;
int n=1;
function<poly(poly,poly)>add=[&](poly x,poly y)
{
for(int i=0;i<30;i++) x[i]=(x[i]+y[i])%K;
return x;
};
function<poly(poly,int)>mul=[&](poly x,int y)
{
for(int i=0;i<30;i++) x[i]=(x[i]*y)%K;
return x;
};
function<poly(poly,poly)>del=[&](poly x,poly y)
{
for(int i=0;i<30;i++) x[i]=(x[i]-y[i]+K)%K;
return x;
};
function<poly(int)>trans=[&](int x)
{
poly res;
for (int i=0;i<30;i++) res.push_back(x%K),x/=K;
return res;
};
function<void(poly&,poly&,int)>work=[&](poly &x,poly &y,int i)
{
while (y[i]!=0)
{
if (x[i]<y[i]) swap(x,y);
int t=x[i]/y[i];
for (int j=0;j<30;j++)
x[j]=(x[j]-t*y[j]+K)%K;
}
};
function<void(poly)>ins=[&](poly x)
{
for (int i=29;i>=0;i--)
if (x[i])
{
if (basis[i].empty())
{
basis[i]=x;
// cout<<"!!!!"<<i<<endl;
return;
} else
{
work(basis[i],x,i);
}
}
};
function<poly(poly)>qry=[&](poly x)
{
for (int i=29;i>=0;i--)
if (x[i])
{
if (!basis[i].size()) continue;
x=del(x,mul(basis[i],(x[i]/basis[i][i])));
}
return x;
};
function<int(int,int)>lca=[&](int x,int y)
{
// cout<<x<<","<<y<<endl;
if (dep[x]<dep[y]) swap(x,y);
// cout<<x<<","<<y<<endl;
for (int i=18;i>=0;i--)
if (dep[fa[x][i]]>=dep[y]) x=fa[x][i];
// cout<<x<<","<<y<<endl;
if (x==y) return x;
for (int i=18;i>=0;i--)
if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
return fa[x][0];
};
while (Q--)
{
int opt,x,y;
cin>>opt>>x>>y;
// cout<<"!!"<<opt<<" "<<x<<" "<<y<<endl;
if (opt==1)
{
poly v=trans(y);
// for (auto u:v) cout<<u<<",";
// cout<<endl;
n++;
dep[n]=dep[x]+1;
fa[n][0]=x;
for (int i=1;i<=18;i++)
fa[n][i]=fa[fa[n][i-1]][i-1];
// cout<<"!!"<<endl;
dis[n]=add(dis[x],v);
// cout<<"!!"<<endl;
ins(add(v,v));
}
else
if (opt==2)
{
int w;
cin>>w;
poly v=trans(w);
int z=lca(x,y);
ins(add(v,v));
v=add(v,dis[x]);
v=add(v,dis[y]);
v=del(v,dis[z]);
v=del(v,dis[z]);
ins(v);
}
else
if (opt==3)
{
int z=lca(x,y);
// cout<<x<<","<<y<<" "<<z<<endl;
poly v=del(add(dis[x],dis[y]),dis[z]);
v=del(v,dis[z]);
// for (auto u:v) cout<<u<<",";
// cout<<endl;
poly now=qry(v);
reverse(now.begin(),now.end());
int ans=0;
for (auto u:now) ans=ans*K+u;
cout<<ans<<'\n';
}
// for (int i=29;i>=0;i--)
// {
// if (basis[i].size())
// {
// for (int j=29;j>=0;j--) cout<<basis[i][j]<<" ";
// cout<<endl;
// }
// }
// cout<<endl;
}
}
signed main()
{
// freopen("A.in","r",stdin);
IOS;
cin.tie(0);
int T=1;
while (T--)
{
BellaKira();
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
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
Runtime Error
Test #21:
score: 0
Runtime Error
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
Runtime Error
Test #26:
score: 0
Runtime Error
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
Runtime Error
Test #30:
score: 0
Runtime Error
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
Runtime Error
Test #33:
score: 0
Runtime Error
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%