We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm not sure if we can get a solution in js/ts.
I have adapted a working c++ solution.
It gets tests 1-6 correct, 7-9 wrong, everything else times out.
Code:
'usestrict';process.stdin.resume();process.stdin.setEncoding('utf-8');letinputString:string='';letinputLines:string[]=[];process.stdin.on('data',function(inputStdin:string):void{inputString+=inputStdin;});functionreadLine():string{returninputLines.shift();}constK=1000000007;constL=200001;letparents:number[]=Array(L);letchildren:number[]=Array(L);functionresetChildren(){children=Array(L).fill(0);}letvaluesSet:boolean[]=Array(L-1);functionresetValuesSet(){valuesSet=Array(L-1).fill(false);}process.stdin.on('end',function():void{inputLines=inputString.split('\n');inputString='';main();});functionmain(){const[n,q]=initMain();// edgesprepareEdges(n);queryloop:for(leti=0;i<q;i++){constk=parseInt(readLine());if(k<2){// result = 0process.stdout.write(`0\n`);// skip next linereadLine();// continue loopcontinuequeryloop;}constquery=readLine().split(" ").map(v=>parseInt(v));// reset valueSetresetValuesSet();// reset childrenresetChildren();letvaluesSum=0;for(constqofquery){valuesSum+=q;valuesSet[q]=true;}letsum=0;for(letj=n;j>0;j--){leta=children[j];if(valuesSet[j]){a+=j;}if(a){letx=a*(valuesSum-a);// ((a % K) * ((valuesSum - a) % K)) % K; //// sum =(sum + x)%K;sum+=x;// if(sum>K) sum = sum % K;}children[parents[j]]+=a;}process.stdout.write(`${sum%K}\n`);}}functioninitMain(){returnreadLine().split(" ").map(v=>parseInt(v));}functionprepareEdges(n:number){for(leti=1;i<n;i++){const[a,b]=readLine().split(" ").map(v=>parseInt(v));if(a<b)parents[b]=a;elseparents[a]=b;}parents[1]=0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Kitty's Calculations on a Tree
You are viewing a single comment's thread. Return to all comments →
I'm not sure if we can get a solution in js/ts. I have adapted a working c++ solution. It gets tests 1-6 correct, 7-9 wrong, everything else times out.
Code: