Skip to content

Commit

Permalink
Merge pull request #1108 from maspypy/matrix_mul_mod_2
Browse files Browse the repository at this point in the history
問題追加 matrix_product_mod_2
  • Loading branch information
maspypy authored Feb 13, 2024
2 parents e59a6e0 + 99579b1 commit c8bf3bd
Show file tree
Hide file tree
Showing 14 changed files with 448 additions and 0 deletions.
62 changes: 62 additions & 0 deletions math/matrix_product_mod_2/checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// https://github.com/MikeMirzayanov/testlib/blob/master/checkers/wcmp.cpp

// The MIT License (MIT)

// Copyright (c) 2015 Mike Mirzayanov

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "testlib.h"

using namespace std;

int main(int argc, char * argv[])
{
setName("compare sequences of tokens");
registerTestlibCmd(argc, argv);

int n = 0;
string j, p;

while (!ans.seekEof() && !ouf.seekEof())
{
n++;

ans.readWordTo(j);
ouf.readWordTo(p);

if (j != p)
quitf(_wa, "%d%s words differ - expected: '%s', found: '%s'", n, englishEnding(n).c_str(), compress(j).c_str(), compress(p).c_str());
}

if (ans.seekEof() && ouf.seekEof())
{
if (n == 1)
quitf(_ok, "\"%s\"", compress(j).c_str());
else
quitf(_ok, "%d tokens", n);
}
else
{
if (ans.seekEof())
quitf(_wa, "Participant output contains extra tokens");
else
quitf(_wa, "Unexpected EOF in the participants output");
}
}
5 changes: 5 additions & 0 deletions math/matrix_product_mod_2/gen/example_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
2 2 2
11
10
10
11
4 changes: 4 additions & 0 deletions math/matrix_product_mod_2/gen/example_01.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1 2 3
10
101
010
3 changes: 3 additions & 0 deletions math/matrix_product_mod_2/gen/example_02.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1 1 1
0
0
33 changes: 33 additions & 0 deletions math/matrix_product_mod_2/gen/max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>

#include "../params.h"
#include "random.h"

using namespace std;

int main(int, char* argv[]) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = N_MAX;
int m = N_MAX;
int k = N_MAX;

printf("%d %d %d\n", n, m, k);

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

return 0;
}
33 changes: 33 additions & 0 deletions math/matrix_product_mod_2/gen/middle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>

#include "../params.h"
#include "random.h"

using namespace std;

int main(int, char* argv[]) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = gen.uniform<int>(1, 1024);
int m = gen.uniform<int>(1, 1024);
int k = gen.uniform<int>(1, 1024);

printf("%d %d %d\n", n, m, k);

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

return 0;
}
34 changes: 34 additions & 0 deletions math/matrix_product_mod_2/gen/random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "random.h"

#include <iostream>

#include "../params.h"

using namespace std;

int main(int, char* argv[]) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = gen.uniform<int>(N_MAX / 2, N_MAX);
int m = gen.uniform<int>(N_MAX / 2, N_MAX);
int k = gen.uniform<int>(N_MAX / 2, N_MAX);

printf("%d %d %d\n", n, m, k);

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

return 0;
}
33 changes: 33 additions & 0 deletions math/matrix_product_mod_2/gen/small.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>

#include "../params.h"
#include "random.h"

using namespace std;

int main(int, char* argv[]) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = gen.uniform<int>(1, 128);
int m = gen.uniform<int>(1, 128);
int k = gen.uniform<int>(1, 128);

printf("%d %d %d\n", n, m, k);

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
printf("%d", gen.uniform<int>(0, MOD - 1));
}
printf("\n");
}

return 0;
}
50 changes: 50 additions & 0 deletions math/matrix_product_mod_2/hash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"example_00.in": "96f305e3c730c38406ea0188780fbe3124a0ac4b6a9f55dddb1482b6dd59e9b0",
"example_00.out": "d6b2d430af8a4eb757bcb3dd072e24225d36937678b462dcfeb34db42527b8e4",
"example_01.in": "02403333136c374714fee0acc2cf8bb8bd8927ecdc9bc7e96168bd49ec75832d",
"example_01.out": "39b8dc3fc8b44765c8e6f1adee04c5b465e555ab791cc42d0d9e810d5b64297c",
"example_02.in": "80e9fae65f4eba4943a7e81f257d954664b3588dcec7c79ff9febf142c26f142",
"example_02.out": "9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa",
"max_random_00.in": "2edf1287b9f47dfc0a1fcae626227c29c186ce7e173f78f607b0fcf03aa2d454",
"max_random_00.out": "8754bdad2edc8194bf4d301c75c387169ce803dfea687d043785353cfa5b9c52",
"max_random_01.in": "605d3ee8d917e50bb03993e5f6eaae834d9983b87a7ec6307e3d5f4d3d5bab89",
"max_random_01.out": "a4cfa21b1f2c7cdce5010f02b7a6afb74b3bdf4ed3506b84c1fec0bef007c314",
"max_random_02.in": "72fdeec90e3136c2dc74e25103116a9b7c28d89cb945e4d7120313964d21a112",
"max_random_02.out": "9b5ff1b674275ff91ffe6386fd2352998030144887d9e9f9648418e78fd5ee88",
"middle_00.in": "50ebc2cd18646f45f0fac17de57fbc490eb613cff3f259250ac2883c0a08ab50",
"middle_00.out": "65b67d9c43ebe22cdc31ef12651dbedc0d1aae13628eaf12aff81b56d2d7f283",
"middle_01.in": "d626efd5df6659147bd21719fcdf31c7bddf6496101d225f6f6f66409dcfc68e",
"middle_01.out": "354c55dc350691dfc98fb912ba2de022ebddfa223d5becda159e079e79d04f49",
"middle_02.in": "5a380177df475396e4e6d2f26883b4f755cadfd280ecf00f0862569196b9c60f",
"middle_02.out": "10866e3544d1468969272ece271daa64a294756db17461963a4e528347d03aad",
"middle_03.in": "9ba7e760e6bbb7da851b4c1b5dafd4a0d180b8eb53786bcde8ddcad4d995340f",
"middle_03.out": "6c303c3b3dc884216e458004023580eab8dc1c3fcc99e2270e102048cb37e504",
"middle_04.in": "d5bc0395af74aecbcfd92c848c1b8340291c71d9329bdb6f2e0c15df408ade61",
"middle_04.out": "90844ef5a3930dcff305bbb7831128871cd2a1c109fbe0968c45755ac7fa569a",
"random_00.in": "a2110f021e98e21819300fd7ca23edfe05d954bcb03a51d8a9bafb3667365cd3",
"random_00.out": "eed2a9dec608b72738eecbf8bb61f2e6e4452fbc16733f510cfaff725eaab98c",
"random_01.in": "651ad98516faab96d7fe513f4f131489eedf8be0fb908639ee3279ee3cdbeb4b",
"random_01.out": "e8bbf42ce6ee95e767c6bb92d6995dc1c9b0bd204b5afff2c5b03684d394d008",
"random_02.in": "d56ba411b3da74327a9c8db722cb9da585182f4a564f2f447b621de0f1514bfb",
"random_02.out": "dd87ba67a81f566ffadec7c58cf6e481cbfb97ab112b5f93a89daca2832b3e32",
"small_00.in": "dc3fb7e8f08a57e7518f32521c8dd6ae17e18d004432c9dd6da96828b402aa14",
"small_00.out": "d5259f869edc6ed4fc0fba28d8de715223816cf783b83ee301b294e256c5f868",
"small_01.in": "a18c882983097e59a33d6bb84b4af59a8b6434b31cf60d43e4f33f908dd252d3",
"small_01.out": "84a254a5495681b32be463897a1aed5262c9630a8140d9bcfbeda05a243480c9",
"small_02.in": "819e585b397d67c7a8af84c81cc9b3567ee4337676ef050975e0b86d66e1a74b",
"small_02.out": "cde59a247794c07d00e4125f898b3e832d5712a78403473e37e7ee8f3b134dc6",
"small_03.in": "3f2af274d7e81a3c22c92577c33a280dc6b13f773f1ae3ddb721afa58fe4791f",
"small_03.out": "f15e8d23e1cf7eb6e24c2246e25464db26efa1f22696283aa353e89ffc1ac508",
"small_04.in": "5f333dd50310ed2568dc0996f225f3a27333a84409e83359a56d5e7f47e47611",
"small_04.out": "ed90b17b15199f5d234578d9b74633c388bae4df1a54e380ac3c3c745af2dfbc",
"small_05.in": "209873e5eef57b376a6f99002998900519d948c3308b11e5dfac5a4196347334",
"small_05.out": "fea574ad93143a2cb264be4aba5e355fa29359af89bbd7c185f223d3e658f5eb",
"small_06.in": "29a10b7d7f21870de0375e6b8ce05ff614e49f610b24955fb72667f2c8e44847",
"small_06.out": "a3cfc1560b02dd30b70a6a3c37ebfffd3e471efc57e1a09ff0b39abda56d126d",
"small_07.in": "2f3b9f06ff280611b09b3ddd023569e2fb6a0f5016de6e0404ead7cf763d0431",
"small_07.out": "4d410d3ebcedf2e8a1f4fcfecacd8e3c53de1afcd9a16b4aaf7cc537a279d604",
"small_08.in": "261eafbe68a8b759b5abb394af039ee5bf271bc45ec73379a2ec2120123f1179",
"small_08.out": "e116103af42e723a261636d796fb9c1d5efbd5e11f407b42aa861cea8b03a7e5",
"small_09.in": "a2388f7ffeaaed8182b74a4ff3c5919a863ed3cc488d4572c290cc983b9cc9fe",
"small_09.out": "ee4846e704e96dcd8d0b7990474ab3de66526704f6d27e0a27e86bdc43411a6c"
}
28 changes: 28 additions & 0 deletions math/matrix_product_mod_2/info.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
title = 'Matrix Product (Mod 2)'
timelimit = 10.0
forum = "https://github.com/yosupo06/library-checker-problems/issues/896"

[[tests]]
name = "example.in"
number = 3
[[tests]]
name = "random.cpp"
number = 3
[[tests]]
name = "max_random.cpp"
number = 3
[[tests]]
name = "small.cpp"
number = 10
[[tests]]
name = "middle.cpp"
number = 5

[[solutions]]
name = "naive.cpp"
wrong = false
allow_tle = true

[params]
N_MAX = 4096
MOD = 2
43 changes: 43 additions & 0 deletions math/matrix_product_mod_2/sol/correct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <cstdio>
#include <iostream>
#include <vector>
#include <bitset>

using namespace std;

const int MAX = 4096 + 10;

using BS = bitset<MAX>;

BS A[MAX];
BS B[MAX];
BS C[MAX];

char BUF[MAX];

int main() {
int N, M, K;
scanf("%d %d %d", &N, &M, &K);

for (int i = 0; i < N; ++i) {
scanf("%s", BUF);
for (int j = 0; j < M; ++j) { A[i][j] = (BUF[j] - '0'); }
}

for (int j = 0; j < M; ++j) {
scanf("%s", BUF);
for (int k = 0; k < K; ++k) { B[j][k] = (BUF[k] - '0'); }
}

for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
// C[i][.] <- A[i][j] B[j][.]
if (A[i][j]) C[i] ^= B[j];
}
}

for (int i = 0; i < N; ++i) {
for (int k = 0; k < K; ++k) { printf("%d", 1 * C[i][k]); }
printf("\n");
}
}
39 changes: 39 additions & 0 deletions math/matrix_product_mod_2/sol/naive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 4096 + 10;

bool A[MAX][MAX];
bool B[MAX][MAX];
bool C[MAX][MAX];

char BUF[MAX];

int main() {
int N, M, K;
scanf("%d %d %d", &N, &M, &K);

for (int i = 0; i < N; ++i) {
scanf("%s", BUF);
for (int j = 0; j < M; ++j) { A[i][j] = (BUF[j] - '0'); }
}

for (int j = 0; j < M; ++j) {
scanf("%s", BUF);
for (int k = 0; k < K; ++k) { B[j][k] = (BUF[k] - '0'); }
}

for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
for (int k = 0; k < K; ++k) { C[i][k] ^= A[i][j] & B[j][k]; }
}
}

for (int i = 0; i < N; ++i) {
for (int k = 0; k < K; ++k) { printf("%d", 1 * C[i][k]); }
printf("\n");
}
}
Loading

0 comments on commit c8bf3bd

Please sign in to comment.