QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#421881 | #5402. 术树数 | Crying | 0 | 0ms | 0kb | C++14 | 3.0kb | 2024-05-26 09:02:07 | 2024-05-26 09:02:08 |
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10,M = 30;
int q,k,m,n;
int dep[MAXN],dis[MAXN],fa[20][MAXN];
namespace Num{
int p[M],q[M];
void fac(int n,int* p){ for(int x=1;x<=m;x++)p[x]=n%k,n/=k;}
int rfac(int* p){int num = 0; for(int i=M;i>=1;i--)num = num*k + p[i]; return num; }
int add(int a,int b){
fac(a,p); fac(b,q);
for(int i=1;i<=m;i++)p[i] = (p[i]+q[i])%k;
return rfac(p);
}
int mul(int a,int b){
fac(a,p);
for(int i=1;i<=m;i++)p[i] = 1ll*p[i]*b%k;
return rfac(p);
}
//
int a[M][M],b[M][M],tmp[M],cur[M];
void exgcd(int* p,int* q,int x){
if(!q[x])return;
int d = p[x]/q[x];
if(d)for(int i=x;i>=1;i--)p[i] = ((p[i]-1ll*q[i]*d)%k+k)%k;
exgcd(q,p,x);
}
void F(int* p,int* q,int x){
exgcd(p,q,x);
if(!p[x])for(int j=1;j<=x;j++)swap(p[j],q[j]);
}
void ins(int n){
fac(n,tmp);
for(int i=m;i>=1;i--)if(tmp[i])F(a[i],tmp,i);
}
void ins(int* tmp){
for(int i=m;i>=1;i--)if(tmp[i])F(a[i],tmp,i);
}
void _exgcd(int a,int b,int& x,int& y){
if(!b)return x=1,y=0,void();
_exgcd(b,a%b,x,y);
int x0 = x,y0 = y;
x = y0; y = x0 - (a/b)*y0;
}
void qry(int w){
fac(w,tmp); memcpy(b,a,sizeof b);
for(int i=m;i>=1;i--)if(a[i][i]){
int g = __gcd(a[i][i],k);
int to = tmp[i]%g,d = 0,dd = 0;
_exgcd(a[i][i],k,d,dd);
d *= (to-tmp[i]) / g; d = (d%k+k)%k;
for(int j=i;j>=1;j--)tmp[j] = (tmp[j]+1ll*a[i][j]*d)%k;
//将基底的k/g倍插入
memset(cur,0,sizeof cur);
for(int j=1;j<i;j++)cur[j] = 1ll*a[i][j] * (k/g)%k;
ins(cur);
}
memcpy(a,b,sizeof a);
w = rfac(tmp); cout<<w<<"\n";
}
}; using Num::add; using Num::mul; using Num::ins; using Num::qry;
int lca(int x,int y){
if(dep[x] < dep[y])swap(x,y);
for(int j=19;j--;)if(dep[x]-(1<<j) >= dep[y])x = fa[j][x];
if(x==y)return x;
for(int j=19;j--;)if(fa[j][x] != fa[j][y])x = fa[j][x],y = fa[j][y];
return fa[0][x];
}
int W(int x,int y){
int p = lca(x,y);
return add(add(dis[x],dis[y]),mul(dis[p],2*(k-1)));
}
int main(){
freopen("city.in","r",stdin); freopen("city.out","w",stdout);
ios::sync_with_stdio(false); cin.tie(0);
cin>>q>>k>>m; n = 1;
for(int o,x,y,v;q--;){
cin>>o;
if(o==1){
cin>>x>>v;
n++; dis[n] = add(dis[x],v),dep[n] = dep[x] + 1,fa[0][n] = x;
for(int j=1;j<20;j++)fa[j][n] = fa[j-1][fa[j-1][n]];
v = mul(v,2);
ins(v);
}else if(o==2){
cin>>x>>y>>v;
int w = add(W(x,y),v);
ins(w);
}else{
cin>>x>>y;
int w = W(x,y);
qry(w);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Dangerous Syscalls
Test #1:
score: 0
Dangerous Syscalls
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
Dangerous Syscalls
Test #21:
score: 0
Dangerous Syscalls
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
Dangerous Syscalls
Test #26:
score: 0
Dangerous Syscalls
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
Dangerous Syscalls
Test #30:
score: 0
Dangerous Syscalls
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
Dangerous Syscalls
Test #33:
score: 0
Dangerous Syscalls
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%