Bead Ornaments

  • + 0 comments

    Java8

    private static final long MOD = 1000000007;
    
    public static long power(long a, int pow) {
        long res = a % MOD;
        int count = 1;
        
        while (count < pow) {
            res = (res * a) % MOD;
            count++;
        }
        if (pow == 0) {
            return 1;
        }
        
        return res;
    }
    
    public static int beadOrnaments(List<Integer> b) {
    // Write your code here
        int n = b.size();
        
        long sum = 0;
        long trees = 1;
        
        for (Integer i : 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); 
    }