#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3xxEYjy ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
ll binpow(ll a,ll b,ll m){
ll res=1;
while(b){
if(b%2){
res=res*a%m;
res%=m;
}
a=a*a%m;
a%=m;
b/=2;
}
return res%m;
}
signed main(){
_3xxEYjy;
int n,m;
cin>>n>>m;
int phi;
{
auto eval_phi=[&](int n){
const int nax=4000;
vi ipr(nax);
rng(i,2,nax){
if(ipr[i]) continue;
ipr[i]=i;
for(int j=2*i;j<nax;j+=i){
ipr[j]=i;
}
}
vi pfct;
int _n=n;
for(int i=2;i<=sqrt(n);i++){
if(ipr[i]!=i) continue;
if(_n%i==0){
while(_n%i==0){
_n/=i;
}
pfct.pb(i);
}
}
if(_n>1) pfct.pb(_n);
int res=0;
{
rep(msk,(1<<sz(pfct))){
int mult=1;
rep(i,sz(pfct))if(msk>>i&1){
if(mult>n/pfct[i]){
mult=-1;
break;
}
mult*=pfct[i];
}
if(mult==-1) continue;
res+=(__builtin_popcount(msk)%2?-1:1)*(n/mult);
}
}
return res;
};phi=eval_phi(m);
}
vec(vi) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u-=1,v-=1;
adj[u].pb(v);
adj[v].pb(u);
}
vi a(n);
rep(i,n){
cin>>a[i];
}
vi par(n,-1);
{
auto dfs=[&](auto self,int v,int _par)->void{
for(auto u:adj[v]){
if(u==_par) continue;
par[u]=v;
self(self,u,v);
}
};
dfs(dfs,0,-1);
}
vec(vi) rbts(n,vi(70,1));
auto update=[&](int v,int d,int x){
rbts[v][d]*=x%m;
rbts[v][d]%=m;
while(1){
d-=1;
if(d<0) break;
v=par[v];
if(v<0) break;
rbts[v][d]*=x%m;
rbts[v][d]%=m;
}
};
auto get=[&](int v){
int res=1;
rep(d,42){
res*=rbts[v][d]%m;
res%=m;
}
int d=1;
while(d<=42){
int u=v;
v=par[v];
if(v==-1) break;
rng(nd,d,42){
res*=rbts[v][nd]%m;
// print(rbts[v][nd],rbts[u][nd+1]);
// break;
res%=m;
res*=binpow(rbts[u][nd+1],phi-1,m)%m;
res%=m;
}
// print(v,res);
d+=1;
}
return res;
};
int q;
cin>>q;
rep(_,q){
int t;
cin>>t;
if(t==1){
int v,d,x;
cin>>v>>d>>x;
v-=1;
update(v,d,x);
}else{
int v;
cin>>v;
v-=1;
int res=get(v);
res*=a[v]%m;
res%=m;
print(res);
}
}
//
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Runtime error |
1 ms |
464 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |