From 2c631558f54345f531c34bc561389b63fd750820 Mon Sep 17 00:00:00 2001 From: MH Hung Date: Mon, 11 Aug 2025 15:13:33 +0800 Subject: [PATCH] feat : add C# solution --- .../C#/2438 ProductQueries.sln | 24 +++++++ .../C#/ProductQueries.csproj | 10 +++ 202508/2438 ProductQueries/C#/Program.cs | 69 +++++++++++++++++++ 202508/2438 ProductQueries/description.md | 5 ++ 4 files changed, 108 insertions(+) create mode 100644 202508/2438 ProductQueries/C#/2438 ProductQueries.sln create mode 100644 202508/2438 ProductQueries/C#/ProductQueries.csproj create mode 100644 202508/2438 ProductQueries/C#/Program.cs create mode 100644 202508/2438 ProductQueries/description.md diff --git a/202508/2438 ProductQueries/C#/2438 ProductQueries.sln b/202508/2438 ProductQueries/C#/2438 ProductQueries.sln new file mode 100644 index 0000000..4edb15a --- /dev/null +++ b/202508/2438 ProductQueries/C#/2438 ProductQueries.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}") = "ProductQueries", "C#\ProductQueries.csproj", "{412F89C4-B1BF-D819-298B-9C873A098742}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {412F89C4-B1BF-D819-298B-9C873A098742}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {412F89C4-B1BF-D819-298B-9C873A098742}.Debug|Any CPU.Build.0 = Debug|Any CPU + {412F89C4-B1BF-D819-298B-9C873A098742}.Release|Any CPU.ActiveCfg = Release|Any CPU + {412F89C4-B1BF-D819-298B-9C873A098742}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BDF3A5A3-29B8-4169-8A46-A13A7DE900D8} + EndGlobalSection +EndGlobal diff --git a/202508/2438 ProductQueries/C#/ProductQueries.csproj b/202508/2438 ProductQueries/C#/ProductQueries.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/202508/2438 ProductQueries/C#/ProductQueries.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/202508/2438 ProductQueries/C#/Program.cs b/202508/2438 ProductQueries/C#/Program.cs new file mode 100644 index 0000000..686d918 --- /dev/null +++ b/202508/2438 ProductQueries/C#/Program.cs @@ -0,0 +1,69 @@ +using System; + +public class Solution +{ + public int[] ProductQueries(int n, int[][] queries) + { + // 1. n => calculate in powers of 2 + // 2. get number array without 0 + var powers = CalPower(n); + + return CalQueries(powers, queries); + + } + + private int[] CalPower(int n) + { + var result = new List(); + + while (n > 0) + { + var num = n % 2; + result.Add(num); + n /= 2; + } + + var newResult = new List(); + for (var i = 0; i < result.Count; i++) + { + if (result[i] != 0) + { + var ans = (int)Math.Pow(2, i); + newResult.Add(ans); + } + } + return newResult.ToArray(); + } + + private int[] CalQueries(int[] powers, int[][] queries) + { + const int MOD = 1000000007; + var result = new List(); + + foreach (var query in queries) + { + long count = 1; + for (var i = query[0]; i <= query[1]; i++) + { + count = (count * powers[i]) % MOD; + } + result.Add(count); + } + + return result.ToArray(); + } +} + +class Program +{ + static void Main() + { + var Solution = new Solution(); + + var ans = Solution.ProductQueries(15, [[0, 1], [2, 2], [0, 3]]); + Console.WriteLine(string.Join(", ", ans)); + + ans = Solution.ProductQueries(919, [[5,5],[4,4],[0,1],[1,5],[4,6],[6,6],[5,6],[0,3],[5,5],[5,6],[1,2],[3,5],[3,6],[5,5],[4,4],[1,1],[2,4],[4,5],[4,4],[5,6],[0,4],[3,3],[0,4],[0,5],[4,4],[5,5],[4,6],[4,5],[0,4],[6,6],[6,6],[6,6],[2,2],[0,5],[1,4],[0,3],[2,4],[5,5],[6,6],[2,2],[2,3],[5,5],[0,6],[3,3],[6,6],[4,4],[0,0],[0,2],[6,6],[6,6],[3,6],[0,4],[6,6],[2,2],[4,6]]); + Console.WriteLine(string.Join(", ", ans)); + } +} \ No newline at end of file diff --git a/202508/2438 ProductQueries/description.md b/202508/2438 ProductQueries/description.md new file mode 100644 index 0000000..3a44f57 --- /dev/null +++ b/202508/2438 ProductQueries/description.md @@ -0,0 +1,5 @@ +Given a positive integer n, there exists a 0-indexed array called powers, composed of the minimum number of powers of 2 that sum to n. The array is sorted in non-decreasing order, and there is only one way to form the array. + +You are also given a 0-indexed 2D integer array queries, where queries[i] = [lefti, righti]. Each queries[i] represents a query where you have to find the product of all powers[j] with lefti <= j <= righti. + +Return an array answers, equal in length to queries, where answers[i] is the answer to the ith query. Since the answer to the ith query may be too large, each answers[i] should be returned modulo 109 + 7. \ No newline at end of file