refactor: refactor project structure
This commit is contained in:
48
legacy/2787 numberOfWays/C#/Program.cs
Executable file
48
legacy/2787 numberOfWays/C#/Program.cs
Executable file
@@ -0,0 +1,48 @@
|
||||
// knapsack problem
|
||||
public class Solution
|
||||
{
|
||||
public int NumberOfWays(int n, int x)
|
||||
{
|
||||
const int MOD = 1_000_000_007;
|
||||
// list all the powers which smaller than n
|
||||
var powers = new List<int>();
|
||||
var num = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
int pow = (int)Math.Pow(num, x);
|
||||
if (pow > n)
|
||||
break;
|
||||
powers.Add(pow);
|
||||
num++;
|
||||
}
|
||||
|
||||
// dynamic programming
|
||||
var dp = new int[n + 1];
|
||||
dp[0] = 1;
|
||||
|
||||
foreach (var power in powers)
|
||||
{
|
||||
for (var i = n; i >= power; i--)
|
||||
{
|
||||
dp[i] = (dp[i] + dp[i - power]) % MOD;
|
||||
}
|
||||
}
|
||||
|
||||
return dp[n];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
var solution = new Solution();
|
||||
|
||||
Console.WriteLine(solution.NumberOfWays(4, 1));
|
||||
Console.WriteLine(solution.NumberOfWays(100, 3));
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user