Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 求和算法的幂_Java_Algorithm_Recursion_Dynamic Programming - Fatal编程技术网

Java 求和算法的幂

Java 求和算法的幂,java,algorithm,recursion,dynamic-programming,Java,Algorithm,Recursion,Dynamic Programming,有一个关于n次幂和的算法问题,在我在线检查解决方案之前,我尝试使用递归来解决这个问题,但这不起作用,我得到了以下结果: public class Main { public static void main(String[] args){ Scanner s = new Scanner(System.in); int x = s.nextInt(), n = s.nextInt(); int end = (int)Math.pow(x, 1.0/n); Sys

有一个关于n次幂和的算法问题,在我在线检查解决方案之前,我尝试使用递归来解决这个问题,但这不起作用,我得到了以下结果:

public class Main {

public static void main(String[] args){

    Scanner s = new Scanner(System.in);
    int x = s.nextInt(), n = s.nextInt();
    int end = (int)Math.pow(x, 1.0/n);
    System.out.print(sumOfPower(x, n, end));

}


static int sumOfPower(int number, int power, int end) {
    int[] temp = new int[number + 1];
    temp[0] = 1;
    for(int i = 1; i <= end; i++) {
        int value = (int)Math.pow(i, power);
        for(int j = number; j > value - 1; j--) {
            temp[j] += temp[j-value];
        }

    }
    return temp[number];
}
我知道循环和动态编程逻辑在某种程度上是如何与我使用
x=10
n=2
的日志一起工作的。日志如下所示:

10
2
j:10    j-value:9   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:9 j-value:8   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:8 j-value:7   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:7 j-value:6   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:6 j-value:5   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:5 j-value:4   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:4 j-value:3   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:3 j-value:2   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:2 j-value:1   temp[j]:0   temp[j-value]:0
1: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:1 j-value:0   temp[j]:0   temp[j-value]:1
1: [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:10    j-value:6   temp[j]:0   temp[j-value]:0
2: [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:9 j-value:5   temp[j]:0   temp[j-value]:0
2: [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:8 j-value:4   temp[j]:0   temp[j-value]:0
2: [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:7 j-value:3   temp[j]:0   temp[j-value]:0
2: [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:6 j-value:2   temp[j]:0   temp[j-value]:0
2: [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
j:5 j-value:1   temp[j]:0   temp[j-value]:1
2: [1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0]
j:4 j-value:0   temp[j]:0   temp[j-value]:1
2: [1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0]
j:10    j-value:1   temp[j]:0   temp[j-value]:1
3: [1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
j:9 j-value:0   temp[j]:0   temp[j-value]:1
3: [1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1]

目前我需要知道的是这背后的数学逻辑,我如何知道在循环之后,
temp['number']
x
可以表示为唯一自然数的
n
次幂的总和。非常感谢您的帮助。

让我们从问题的抽象模型开始,给出一个DAG,从一个节点到另一个节点有多少种方式

让我们调用函数来回答这个问题是
f(开始,结束)

我们很容易看出这一点

f(start, end) = sum f(x , end) with x are the neighbours of start
对于基本情况
f(end,end)=1
(有一种方法可以从一端移动到另一端,因为此图没有循环)。因为这是一个DAG,所以上面的函数会收敛

类似地,您可以看到相同的模型可以应用于此问题

假设我们需要计算
f(X,0)
,其中X是初始值,我们可以看到,从值X,我们可以得到所有的值
X-y
,其中
y
是第n次方数

所以

在您给出的代码中,
temp
正在存储
f
f(0,0)到f(值,0)
的答案


那么,为什么这是一个DAG?因为n次方的值是正的,所以我们无法回到以前的状态。

让我们从问题的抽象模型开始,给出一个DAG,从一个节点到另一个节点有多少种方式

让我们调用函数来回答这个问题是
f(开始,结束)

我们很容易看出这一点

f(start, end) = sum f(x , end) with x are the neighbours of start
对于基本情况
f(end,end)=1
(有一种方法可以从一端移动到另一端,因为此图没有循环)。因为这是一个DAG,所以上面的函数会收敛

类似地,您可以看到相同的模型可以应用于此问题

假设我们需要计算
f(X,0)
,其中X是初始值,我们可以看到,从值X,我们可以得到所有的值
X-y
,其中
y
是第n次方数

所以

在您给出的代码中,
temp
正在存储
f
f(0,0)到f(值,0)
的答案


那么,为什么这是一个DAG?因为n次方的值是正的,所以我们不可能回到以前的状态。

这对我来说是递归。只需小心限制(开始、结束和目标总和):

公共类powerSum{
静态int解=0;
静态无效过程(int currentSum、int targetSum、int currentsnumber、int n){
if(currentSum==targetSum){
解决方案++;
返回;
}

对于(int i=currentNumber;currentSum+(int)Math.pow(i,n),这对我来说是递归。只需小心限制(开始、结束和目标和):

公共类powerSum{
静态int解=0;
静态无效过程(int currentSum、int targetSum、int currentsnumber、int n){
if(currentSum==targetSum){
解决方案++;
返回;
}
对于(int i=currentNumber;currentSum+(int)Math.pow(i,n)
f(X, 0) = sum f(X - y, 0) with y is all Nth power number less than or equal X

f(0,0) = 1
public class powerSum {
    static int solutions = 0;

    static void process(int currentSum, int targetSum, int currentNumber, int n) {
    if (currentSum == targetSum) {
        solutions++;
        return;
    }

    for (int i = currentNumber; currentSum + (int) Math.pow(i, n) <= targetSum; i++)
        process(currentSum + (int) Math.pow(i, n), targetSum, i + 1, n);
    }

    public static void main(String[] args) {
        process(0, 100, 1, 2);
        System.out.println(solutions);
    }

}