Bead Ornaments

Sort by

recency

|

9 Discussions

|

  • + 0 comments

    I've read this problem multiple times and I still don't know how it is trying to make these beads. This is the first problem out of all the problems thus far that I can't even understand the premise (even the examples aren't good enough). If you're counting tree configurations rather than linear arrangements, why is there no picture of a freaking tree?

    This problem needs a rewrite if anyone on this website is paying attention.

  • + 0 comments

    I could not figure out the correct answer for [4]. Here is my manual solution, which is 6 instead of the official answer of 16:

        1) 1234,2341,3412,4123
        2) 1243,2431,4312,3124
        3) 1324,3241,2413,4132
        4) 1342,3421,4213,2134
        5) 1423,4231,2314,3142
        6) 1432,4321,3214,2143
    
  • + 0 comments

    Python

    def beadOrnaments(b):
        sumColor=0
        intercolor=1
        for color in b:
            intercolor*=color**(color-1)
            sumColor+=color
        intercolor*=sumColor**(len(b)-2)
        return int(intercolor%(10**9+7))
    
  • + 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); 
    }
    
  • + 0 comments

    Python 3|Simple|Easy to Understand

    import  functools
    
    def beadOrnaments(b):
        ans = functools.reduce(lambda ans, a: ans * (a ** (a - 1)), b,1)
        sm = functools.reduce(lambda sm, a: sm + a, b,0)
        if (len(b) - 2 >= 0):
            ans *= sm ** (len(b) - 2)
        else:
            ans //= sm
        return ans % 1000000007