码迷,mamicode.com
首页 > 其他好文 > 详细

[Daily Coding Problem] Find the total number of solutions of a linear equation of n variables

时间:2019-12-05 01:19:58      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:div   fun   write   ble   ++   solution   rhs   mina   NPU   

Given a linear equation of n variables, find the total number of non-negative integer solutions of it. All coefficients are positive. 

Example: 

input: x + 2 * y = 5

output: 3,  the 3 possible integer solutions are x = 1, y = 2; x = 3, y = 1; x = 5, y = 0.

 

This problem can be simply thought as a coin change problem.
You are given coins of different denominations and a total amount of money.
Write a function to compute the number of combinations that make up that amount.
You may assume that you have infinite number of each kind of coin.

rhs is the sum.
coeffients are the coin denominations.
each variable is the number of one coin denomination.

 

public class LinearEquationIntegerSolutions {
    public static int countSolutions(int[] coeff, int n, int rhs) {
        int[] dp = new int[rhs + 1];
        dp[0] = 1;  // if rhs == 0, there is only one solution: all variables are 0.

        for(int i = 0; i < n; i++) {
            for(int j = coeff[i]; j <= rhs; j++) {
                dp[j] += dp[j - coeff[i]];
            }
        }
        return dp[rhs];
    }

    public static void main(String[] args) {
        int[] coeff = {1,2};
        countSolutions(coeff, 2, 5);
    }
}

 

[Daily Coding Problem] Find the total number of solutions of a linear equation of n variables

标签:div   fun   write   ble   ++   solution   rhs   mina   NPU   

原文地址:https://www.cnblogs.com/lz87/p/11986598.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!