返回介绍

solution / 2700-2799 / 2787.Ways to Express an Integer as Sum of Powers / README_EN

发布于 2024-06-17 01:03:00 字数 4356 浏览 0 评论 0 收藏 0

2787. Ways to Express an Integer as Sum of Powers

中文文档

Description

Given two positive integers n and x.

Return _the number of ways _n_ can be expressed as the sum of the _xth_ power of unique positive integers, in other words, the number of sets of unique integers _[n1, n2, ..., nk]_ where _n = n1x + n2x + ... + nkx_._

Since the result can be very large, return it modulo 109 + 7.

For example, if n = 160 and x = 3, one way to express n is n = 23 + 33 + 53.

 

Example 1:

Input: n = 10, x = 2
Output: 1
Explanation: We can express n as the following: n = 32 + 12 = 10.
It can be shown that it is the only way to express 10 as the sum of the 2nd power of unique integers.

Example 2:

Input: n = 4, x = 1
Output: 2
Explanation: We can express n in the following ways:
- n = 41 = 4.
- n = 31 + 11 = 4.

 

Constraints:

  • 1 <= n <= 300
  • 1 <= x <= 5

Solutions

Solution 1

class Solution:
  def numberOfWays(self, n: int, x: int) -> int:
    mod = 10**9 + 7
    f = [[0] * (n + 1) for _ in range(n + 1)]
    f[0][0] = 1
    for i in range(1, n + 1):
      k = pow(i, x)
      for j in range(n + 1):
        f[i][j] = f[i - 1][j]
        if k <= j:
          f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod
    return f[n][n]
class Solution {
  public int numberOfWays(int n, int x) {
    final int mod = (int) 1e9 + 7;
    int[][] f = new int[n + 1][n + 1];
    f[0][0] = 1;
    for (int i = 1; i <= n; ++i) {
      long k = (long) Math.pow(i, x);
      for (int j = 0; j <= n; ++j) {
        f[i][j] = f[i - 1][j];
        if (k <= j) {
          f[i][j] = (f[i][j] + f[i - 1][j - (int) k]) % mod;
        }
      }
    }
    return f[n][n];
  }
}
class Solution {
public:
  int numberOfWays(int n, int x) {
    const int mod = 1e9 + 7;
    int f[n + 1][n + 1];
    memset(f, 0, sizeof(f));
    f[0][0] = 1;
    for (int i = 1; i <= n; ++i) {
      long long k = (long long) pow(i, x);
      for (int j = 0; j <= n; ++j) {
        f[i][j] = f[i - 1][j];
        if (k <= j) {
          f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
        }
      }
    }
    return f[n][n];
  }
};
func numberOfWays(n int, x int) int {
  const mod int = 1e9 + 7
  f := make([][]int, n+1)
  for i := range f {
    f[i] = make([]int, n+1)
  }
  f[0][0] = 1
  for i := 1; i <= n; i++ {
    k := int(math.Pow(float64(i), float64(x)))
    for j := 0; j <= n; j++ {
      f[i][j] = f[i-1][j]
      if k <= j {
        f[i][j] = (f[i][j] + f[i-1][j-k]) % mod
      }
    }
  }
  return f[n][n]
}
function numberOfWays(n: number, x: number): number {
  const mod = 10 ** 9 + 7;
  const f: number[][] = Array(n + 1)
    .fill(0)
    .map(() => Array(n + 1).fill(0));
  f[0][0] = 1;
  for (let i = 1; i <= n; ++i) {
    const k = Math.pow(i, x);
    for (let j = 0; j <= n; ++j) {
      f[i][j] = f[i - 1][j];
      if (k <= j) {
        f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod;
      }
    }
  }
  return f[n][n];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文