feat : add C# solution

This commit is contained in:
2025-08-11 15:13:33 +08:00
parent 4f3e84394c
commit 2c631558f5
4 changed files with 108 additions and 0 deletions

View 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}") = "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

View 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>

View File

@@ -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<int>();
while (n > 0)
{
var num = n % 2;
result.Add(num);
n /= 2;
}
var newResult = new List<int>();
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<int>();
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));
}
}

View File

@@ -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.