답안 #998454

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998454 2024-06-14T02:23:43 Z jampm Sprinkler (JOI22_sprinkler) C++17
0 / 100
2474 ms 210156 KB
//Emmanuel B
//Sprinker




#include <bits/stdc++.h>
using namespace std;
using lli=long long int;
#define pb push_back
#define deb(x) cout<<#x<<": "<<x<<endl;
#define deb2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl;


void dfs(lli n, lli par, vector<lli> &parent, vector<vector<lli>> &adj, vector<vector<lli>> &sons){
    parent[n]=par;
    for(lli x: adj[n]){
        if(x==par) continue;
        sons[n].pb(x);
        dfs(x,n,parent,adj,sons);
    }
}
lli L;

lli binexp (lli b, lli p, lli MOD){
    if(p==0) return 1;
    if(p==1) return b%MOD;
    lli x=binexp(b, p/2,MOD);
    x*=x;
    x%=MOD;
    if(p%2) x*=b;
    x%=MOD;
    return x;

}

int main(){
    lli N;
    cin>>N>>L;
    vector<vector<lli>> adj (N+5);
    for(lli i=0; i<N-1; ++i){
        lli a,b;
        cin>>a>>b;
        adj[a].pb(b);
        adj[b].pb(a);
    
    }
    lli constante=sqrt(L)+5;
    
    vector<bool> nums (constante+5, false);
    vector<lli> criba;
    for(lli i=2; i<=constante; ++i){
        if(nums[i]) continue;
        criba.pb(i);
        for(lli j=i; i*j<=constante; ++j){
            nums[i*j]=true;
        }
    } 

    vector<lli> parent (N+5);
    vector<vector<lli>> sons (N+5);
    vector<lli> mult1 (N+5,1);
    vector<lli> mult2 (N+5,1);
    dfs(1,0,parent,adj,sons);
    vector<lli> values (N+5);
    vector<vector<lli>> resolver (N+5, vector<lli> (44,1));
    vector<vector<lli>> numof0s (N+5, vector<lli> (44,0));
    for(lli i=1; i<=N; ++i){
        cin>>values[i];
    }
    lli Q;
    cin>>Q;
    while(Q--){
        lli type;
        cin>>type;
        if(type==1){
            lli x,d,w;
            cin>>x>>d>>w;
            while(d>=0 && x!=0){
      //          deb(d);
     //           deb(x);
            for(lli i=0;  i<=d; ++i){
                lli val=resolver[x][i]*w;
        //        deb(val);
                if(val==0){
                    resolver[x][i]=0;
                    continue;
                }
                while(val%L==0){
                    numof0s[x][i]++;
                    val/=L;
                }
                resolver[x][i]=val;
                resolver[x][i]%=L;
            }
            d--;
            x=parent[x];
            }
        }
        else{
            lli x;
            cin>>x;
            lli ans=values[x]*resolver[x][0];
            ans%=L;
            lli act=parent[x];
            lli cont=1;

            while(act!=0 && cont+1<resolver.size()){
                
                if(numof0s[act][cont]>numof0s[x][cont+1]){
                    ans=0;
                    break;
                }
                if(resolver[x][cont+1]==0){
                    ans=0;
                    break;
                }
                lli u=resolver[act][cont];
                lli v=resolver[x][cont+1];
                lli xd=__gcd(L, v);
                lli La=L/xd;
                v=v/xd;
                lli phi=1;
                lli va=v;
                for(lli primo: criba){
                    if(va==1) break;
                    if(va%primo==0){
                        phi*=primo;
                        va/=primo;
                    }
                    while(va%primo==0){
                        phi*=(primo-1);
                        va/=primo;
                    }
            
                }
                lli ayuda=binexp(La, phi-1, v);
                lli xxd=-u*ayuda;

                lli val=(resolver[act][cont]+xxd*L)/resolver[x][cont+1];
                lli aaaaaaa=val/L;
                aaaaaaa*=-1;
                val+=(aaaaaaa+1)*L;
                
                val%=L;
                ans*=val;
                ans%=L;
                x=act;
                act=parent[act];
                cont++;
            }
            cout<<ans<<endl;
        }
    }
    
}

Compilation message

sprinkler.cpp: In function 'int main()':
sprinkler.cpp:108:35: warning: comparison of integer expressions of different signedness: 'lli' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |             while(act!=0 && cont+1<resolver.size()){
      |                             ~~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 53 ms 1372 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1486 ms 188372 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1486 ms 188372 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Incorrect 1544 ms 210156 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Incorrect 2474 ms 204592 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 53 ms 1372 KB Output isn't correct
5 Halted 0 ms 0 KB -