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.
privatestaticfinallongMOD=1000000007;publicstaticlongpower(longa,intpow){longres=a%MOD;intcount=1;while(count<pow){res=(res*a)%MOD;count++;}if(pow==0){return1;}returnres;}publicstaticintbeadOrnaments(List<Integer>b){// Write your code hereintn=b.size();longsum=0;longtrees=1;for(Integeri:b){sum=(sum+i)%MOD;//this i - 2 + 1 is also from Cayley formula//first: number of sub trees are found using power(i, i - 2)//one leaf of a sub tree will connect with root of another// sub tree//hence there are i ways of connecting as there should be // i leaves in each tree//therefore power(i, i - 2) * i == power(i, i - 2 + 1)trees=(trees*power(i,i-2+1))%MOD;}if(n>=2){//Cayley formular //https://www.geeksforgeeks.org/cayleys-formula/trees=(trees*power(sum,n-2))%MOD;}else{trees=(long)Math.floor(trees/sum);}return(int)(trees%MOD);}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bead Ornaments
You are viewing a single comment's thread. Return to all comments →
Java8