Merge pull request '2508/2787' (#4) from 2508/2787 into main
Reviewed-on: #4
This commit is contained in:
24
202508/2787 numberOfWays/C#/C#.sln
Normal file
24
202508/2787 numberOfWays/C#/C#.sln
Normal file
@@ -0,0 +1,24 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.2.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "numberOfWays", "numberOfWays.csproj", "{1E81E272-AF2C-9CB4-F90B-0AAC263D10DC}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{1E81E272-AF2C-9CB4-F90B-0AAC263D10DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1E81E272-AF2C-9CB4-F90B-0AAC263D10DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1E81E272-AF2C-9CB4-F90B-0AAC263D10DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1E81E272-AF2C-9CB4-F90B-0AAC263D10DC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {AD18FC84-E2FB-4382-96DF-8AE7E50C1401}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
48
202508/2787 numberOfWays/C#/Program.cs
Normal file
48
202508/2787 numberOfWays/C#/Program.cs
Normal 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));
|
||||
|
||||
}
|
||||
}
|
10
202508/2787 numberOfWays/C#/numberOfWays.csproj
Normal file
10
202508/2787 numberOfWays/C#/numberOfWays.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user