Compare commits

..

2 Commits

Author SHA1 Message Date
a3131bf6d0 2906. Construct Product Matrix 2026-03-25 11:50:01 +03:00
1e973f5a9e начало 2906 2026-03-24 22:29:36 +03:00
5 changed files with 201 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>_2906._Construct_Product_Matrix</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,74 @@
static string FormatMatrix(int[][] matrix)
{
var rows = new string[matrix.Length];
for (var i = 0; i < matrix.Length; i++)
rows[i] = "[" + string.Join(",", matrix[i]) + "]";
return "[" + string.Join(",", rows) + "]";
}
var sol = new Solution();
var cases = new (int[][] input, string expected)[]
{
(
new[]
{
new[] { 1, 2 },
new[] { 3, 4 }
},
"[[24,12],[8,6]]"
),
(
new[]
{
new[] { 12345 },
new[] { 2 },
new[] { 1 }
},
"[[2],[0],[0]]"
),
(
new[]
{
new[] { 4, 8, 8 },
new[] { 6, 2, 5 },
new[] { 7, 3, 7 },
new[] { 6, 3, 5 }
},
"[[3525,7935,7935],[6465,7050,2820],[7305,585,7305],[6465,585,2820]]"
),
(
new[]
{
new[] { 4, 3, 9 },
new[] { 3, 9, 10 },
new[] { 9, 7, 8 },
new[] { 8, 4, 7 },
new[] { 6, 1, 3 }
},
"[[3255,225,75],[225,75,11178],[75,1860,7800],[7800,3255,1860],[6285,675,225]]"
),
(
new[]
{
new []{1,2,2},
new []{1,4,3}
},
"[[48,24,24],[48,12,16]]"
),
(
new[]
{
new[] { 414750857 },
new[] { 449145368 },
new[] { 767292749 }
},
"[[1462],[3103],[9436]]"
)
};
foreach (var (input, expected) in cases)
{
var actual = sol.ConstructProductMatrix(input);
Console.WriteLine($"{FormatMatrix(input)} -> {FormatMatrix(actual)} (expected: {expected})");
}

View File

@@ -0,0 +1,80 @@
public class Solution
{
struct MulInfo
{
public long Left;
public long Right;
}
public int[][] ConstructProductMatrix(int[][] grid)
{
var mod = 12345;
var table = new MulInfo[grid.Length][];
var lastRow = grid.Length - 1;
var lastCol = grid[0].Length - 1;
// прямой проход
for (var i = 0; i < grid.Length; i++)
{
table[i] = new MulInfo[grid[i].Length];
for (var j = 0; j < grid[i].Length; j++)
{
if (j == 0 && i == 0)
{
table[i][j].Left = grid[i][j] % mod;
}
else if (j > 0)
{
table[i][j].Left = table[i][j - 1].Left * grid[i][j] % mod;
}
else if (i > 0)
{
table[i][j].Left = table[i - 1][lastCol].Left * grid[i][j];
table[i][j].Left %= mod;
}
}
}
// обратный проход
for (var i = lastRow; i >= 0; i--)
{
for (var j = lastCol; j >= 0; j--)
{
if (j == lastCol && i == lastRow)
{
table[i][j].Right = grid[i][j] % mod;
}
else if (j < lastCol)
{
table[i][j].Right = table[i][j + 1].Right * grid[i][j] % mod;
}
else if (i < lastRow)
{
table[i][j].Right = table[i + 1][0].Right * grid[i][j] % mod;
}
var left = 1L;
var right = 1L;
if (i == 0 && j == 0)
left = 1;
else if (j > 0)
left = table[i][j - 1].Left;
else if (i > 0)
left = table[i - 1][lastCol].Left;
if (i == lastRow && j == lastCol)
right = 1;
else if (j < lastCol)
right = table[i][j + 1].Right;
else if (i < lastRow)
right = table[i + 1][0].Right;
grid[i][j] = (int)(left * right % mod);
}
}
return grid;
}
}

View File

@@ -179,6 +179,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "5. Longest Palindromic Subs
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10. Regular Expression Matching", "10. Regular Expression Matching\10. Regular Expression Matching.csproj", "{0A6CECBE-AD75-4829-AAB2-D6D29EEECEB5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "2906. Construct Product Matrix", "2906. Construct Product Matrix\2906. Construct Product Matrix.csproj", "{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -1245,6 +1247,18 @@ Global
{0A6CECBE-AD75-4829-AAB2-D6D29EEECEB5}.Release|x64.Build.0 = Release|Any CPU
{0A6CECBE-AD75-4829-AAB2-D6D29EEECEB5}.Release|x86.ActiveCfg = Release|Any CPU
{0A6CECBE-AD75-4829-AAB2-D6D29EEECEB5}.Release|x86.Build.0 = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Debug|x64.ActiveCfg = Debug|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Debug|x64.Build.0 = Debug|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Debug|x86.ActiveCfg = Debug|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Debug|x86.Build.0 = Debug|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|Any CPU.Build.0 = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x64.ActiveCfg = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x64.Build.0 = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.ActiveCfg = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

22
create_project.sh Normal file
View File

@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "Usage: $0 \"Project Name\""
exit 1
fi
project_name="$1"
if [[ -z "$project_name" ]]; then
echo "Project name cannot be empty"
exit 1
fi
if [[ -d "$project_name" ]]; then
echo "Directory already exists: $project_name"
exit 1
fi
dotnet new console -n "$project_name"
dotnet sln add "$project_name"