refactor: refactor project structure
This commit is contained in:
69
legacy/2438 ProductQueries/C#/Program.cs
Executable file
69
legacy/2438 ProductQueries/C#/Program.cs
Executable 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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user