#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
//snuke's mod int
// template <ll mod>
ll mod;
struct modint{
ll x; // typedef long long ll;
modint(ll x=0):x((x%mod+mod)%mod){}
modint operator-()const{return modint(-x);}
modint& operator+=(const modint a){if((x+=a.x)>=mod) x-=mod; return *this;}
modint& operator-=(const modint a){if((x+=mod-a.x)>=mod) x-=mod; return *this;}
modint& operator*=(const modint a){(x*=a.x)%=mod; return *this;}
modint operator+(const modint a)const{modint res(*this); return res+=a;}
modint operator-(const modint a)const{modint res(*this); return res-=a;}
modint operator*(const modint a)const{modint res(*this); return res*=a;}
modint pow(ll n)const{
modint res=1,x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modint inv()const{return pow(mod-2);}
};
//
// using mint=modint<998244353>;
signed main(){
_3xxEYjy;
int n,m;
cin>>n>>m;
mod=m;
using mint=modint;
ll 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;
}
}
vec(ll) pfct;
int _n=n;
for(int i=2;i<=nax;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);
ll res=0;
{
rep(msk,(1<<sz(pfct))){
ll mult=1;
rep(i,sz(pfct))if(msk>>i&1){
if(mult>n/pfct[i]){
mult=-1;
break;
}
mult*=pfct[i]*1ll;
}
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];
a[i]%=m;
}
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(vec(mint)) rbts(n,vec(mint)(70,1));
auto update=[&](int v,int d,int x){
rbts[v][d]*=mint(x);
while(1){
d-=1;
if(d<0) break;
v=par[v];
if(v<0) break;
rbts[v][d]*=mint(x);
}
};
auto binpow=[&](mint a,ll b)->mint{
mint res=1;
while(b){
if(b%2) res*=a;
a=a*a;
b/=2;
}
return res;
};
auto get=[&](int v){
mint res=1;
rep(d,42){
res*=rbts[v][d];
}
int d=1;
while(d<=42){
int u=v;
v=par[v];
if(v==-1) break;
rng(nd,d,42){
res*=rbts[v][nd];
// print(rbts[v][nd],rbts[u][nd+1]);
// break;
res*=binpow(rbts[u][nd+1].x,phi-1);
}
// 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;
mint res=get(v);
res*=mint(a[v]);
cout<<res.x<<"\n";
}
}
//
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
107 ms |
980 KB |
Output is correct |
5 |
Incorrect |
113 ms |
980 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
4089 ms |
133068 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
4089 ms |
133068 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
4070 ms |
138640 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Execution timed out |
4062 ms |
136628 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
107 ms |
980 KB |
Output is correct |
5 |
Incorrect |
113 ms |
980 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |