Hướng dẫn the power sum hackerrank solution python - giải pháp tổng hợp sức mạnh hackerrank python

Tìm số cách mà một số nguyên nhất định, có thể được thể hiện dưới dạng tổng sức mạnh của các số tự nhiên, duy nhất., can be expressed as the sum of the powers of unique, natural numbers.

Ví dụ, nếu và, chúng ta phải tìm thấy tất cả các kết hợp của các ô vuông độc đáo cộng lại. Giải pháp duy nhất là. and , we have to find all combinations of unique squares adding up to . The only solution is .

Mô tả chức năng

Hoàn thành chức năng Powersum trong trình soạn thảo bên dưới. Nó sẽ trả về một số nguyên đại diện cho số lượng kết hợp có thể.

Powersum có (các) tham số sau:

  • X: số nguyên để tổng
  • N: sức mạnh số nguyên để tăng số lên

Định dạng đầu vào

Dòng đầu tiên chứa một số nguyên. Dòng thứ hai chứa một số nguyên..
The second line contains an integer .

Định dạng đầu ra

Đầu ra một số nguyên duy nhất, số lượng kết hợp có thể được xử lý.

Giải thích 0

Nếu và, chúng ta cần tìm số lượng cách có thể được biểu diễn dưới dạng tổng bình phương của các số duy nhất. and , we need to find the number of ways that can be represented as the sum of squares of unique numbers.

Đây là cách duy nhất có thể được thể hiện dưới dạng tổng của các ô vuông duy nhất. can be expressed as the sum of unique squares.

Giải thích 2

có thể được thể hiện là tổng của các hình khối. . Không có cách nào khác để thể hiện là tổng của các hình khối..
. There is no other way to express as the sum of cubes.

Sắp xếp 323 thảo luận, bởi:

Vui lòng đăng nhập để đăng bình luận

  • Hướng dẫn the power sum hackerrank solution python - giải pháp tổng hợp sức mạnh hackerrank python

    3 tuần trước+ 0 bình luận+ 0 comments

        public static int powerSum(int X, int N, int n=1)
        {
            int result = (int) Math.Pow(n, N);
            if(result == X)
            {
                return 1;  //found the solution
            }
            
            if(result > X)
            {
               return 0; // stop recursion
            } 
            
            // find the solution for next number for same result and remaining result
            return powerSum(X, N, n+1) + powerSum(X-result, N, n+1);
        }
    

  • Hướng dẫn the power sum hackerrank solution python - giải pháp tổng hợp sức mạnh hackerrank python

  • Hướng dẫn the power sum hackerrank solution python - giải pháp tổng hợp sức mạnh hackerrank python

    1 tháng trước+ 1 bình luận+ 1 comment

    // đệ quy java

        public static int powerSum(int X, int N) {
        return powerSum(X, N, 1);
    }
    public static int powerSum(int X, int N, int start) {
        if(X == 0)
            return 1;
        if(X < 0)
            return 0;
    
        int count = 0;
        for (int i = start; Math.pow(i, N) <= X; i++) {
            count += powerSum((int) (X - Math.pow(i, N)), N, i+1);
        }
        return count;
    }****
    

  • Hướng dẫn the power sum hackerrank solution python - giải pháp tổng hợp sức mạnh hackerrank python

    1 tháng trước+ 0 bình luận+ 0 comments

    Python

    from itertools import combinations as comb
    def powerSum(X, N):
        lista = []
        for size in range(1,int(X**(1/N)+1)):
            lista.append(tuple(comb(range(1,int((X)**(1/N)+1)), size)))
        lista = sum(lista, ())
        return sum([1 for element in lista if (sum(element) >= X**(1/N)) and (sum([num ** N for num in element]) == X)])
    

  • Hướng dẫn the power sum hackerrank solution python - giải pháp tổng hợp sức mạnh hackerrank python

    1 tháng trước+ 0 bình luận+ 0 comments

    Python

    public static int powerSum(int X, int N) {
        // Write your code here
    
            List<List<Integer>> results = new ArrayList<>();
            int powerRoot = (int) Math.round(Math.pow(Math.E, Math.log(X)/N));
            
            for (int i = 1; i <= powerRoot; i++) {
                List<Integer> currentSum = new ArrayList<>();
                powerSumHelp(i, 0, powerRoot, X, N, results, currentSum);
            }
            
            return results.size();
    
        }
        
        public static void powerSumHelp(int currentInt, int currentTotal, int end, int max, int power, List<List<Integer>> results, List<Integer> currentSum) {
    
            if (currentInt > end) {
                return;
            }
            int total = currentTotal + (int) Math.pow(currentInt, power);
    
            if (total > max) {
                return;
            }
            if (total == max) {
                currentSum.add(currentInt);
                results.add(currentSum);
                return;
            }
            currentSum.add(currentInt);
            for (int i = currentInt+1; i <= end; i++) {
                powerSumHelp(i, total, end, max, power, results, currentSum);
            }
            
        }