diff --git a/202508/2787 numberOfWays/C#/C#.sln b/202508/2787 numberOfWays/C#/C#.sln new file mode 100644 index 0000000..7fd954e --- /dev/null +++ b/202508/2787 numberOfWays/C#/C#.sln @@ -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 diff --git a/202508/2787 numberOfWays/C#/Program.cs b/202508/2787 numberOfWays/C#/Program.cs new file mode 100644 index 0000000..9f51042 --- /dev/null +++ b/202508/2787 numberOfWays/C#/Program.cs @@ -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(); + 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)); + + } +} \ No newline at end of file diff --git a/202508/2787 numberOfWays/C#/numberOfWays.csproj b/202508/2787 numberOfWays/C#/numberOfWays.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/202508/2787 numberOfWays/C#/numberOfWays.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + +