Skip to content

Commit

Permalink
Merge pull request yosupo06#1270 from adamant-pwn/adjugate
Browse files Browse the repository at this point in the history
Add adjugate_matrix
  • Loading branch information
maspypy authored Oct 28, 2024
2 parents ac9b085 + 0814e80 commit c6bfe96
Show file tree
Hide file tree
Showing 21 changed files with 1,166 additions and 0 deletions.
62 changes: 62 additions & 0 deletions linear_algebra/adjugate_matrix/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");
}
}
25 changes: 25 additions & 0 deletions linear_algebra/adjugate_matrix/gen/almost_identity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

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

int n = N_MAX;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; i++) {
a[i][i] = 1;
}
int t = gen.uniform(0, n - 1);
a[t][t] = 0;
printf("%d\n", n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d%c", a[i][j], " \n"[j + 1 == n]);
}
}
return 0;
}
28 changes: 28 additions & 0 deletions linear_algebra/adjugate_matrix/gen/almost_perm_max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include "random.h"
#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(N_MAX - 10, N_MAX);
vector<vector<int>> a(n, vector<int>(n));
vector<int> p = gen.perm<int>(n);
for (int i = 0; i < n; i++) {
a[i][p[i]] = gen.uniform<int>(0, MOD - 1);
}
int t = gen.uniform(0, n - 1);
a[t][p[t]] = 0;
printf("%d\n", n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d%c", a[i][j], " \n"[j + 1 == n]);
}
}
return 0;
}
4 changes: 4 additions & 0 deletions linear_algebra/adjugate_matrix/gen/anti55588_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
1 2 3
0 0 1
0 1 0
4 changes: 4 additions & 0 deletions linear_algebra/adjugate_matrix/gen/example_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
3 1 4
1 5 9
2 6 5
4 changes: 4 additions & 0 deletions linear_algebra/adjugate_matrix/gen/example_01.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3
1 2 3
4 5 6
7 8 9
3 changes: 3 additions & 0 deletions linear_algebra/adjugate_matrix/gen/example_02.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2
0 1
1 0
2 changes: 2 additions & 0 deletions linear_algebra/adjugate_matrix/gen/example_03.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
0
49 changes: 49 additions & 0 deletions linear_algebra/adjugate_matrix/gen/highrank_max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

int main(int, char* argv[]) {


long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = N_MAX;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = gen.uniform<int>(0, MOD - 1);
}
}

if(gen.uniform01()) { // create a linear dependency in rows
int i = gen.uniform(0, n - 1);
int j = gen.uniform(0, n - 1);
int k = gen.uniform(0, n - 1);
auto A = gen.uniform<int64_t>(0, MOD - 1);
auto B = gen.uniform<int64_t>(0, MOD - 1);
for(int l = 0; l < n; l++) {
a[i][l] = (A * a[j][l] + B * a[k][l]) % MOD;
}
}
if(gen.uniform01()) { // create a linear dependency in columns
int i = gen.uniform(0, n - 1);
int j = gen.uniform(0, n - 1);
int k = gen.uniform(0, n - 1);
auto A = gen.uniform<int64_t>(0, MOD - 1);
auto B = gen.uniform<int64_t>(0, MOD - 1);
for(int l = 0; l < n; l++) {
a[l][i] = (A * a[l][j] + B * a[l][k]) % MOD;
}
}

printf("%d\n", n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d%c", a[i][j], " \n"[j + 1 == n]);
}
}
return 0;
}
60 changes: 60 additions & 0 deletions linear_algebra/adjugate_matrix/gen/lowrank_max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

using ll = long long;

int main(int, char* argv[]) {


long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = gen.uniform(N_MAX - 10, N_MAX);
int rank = gen.uniform(1, n - 1);

vector<vector<ll>> a(n, vector<ll>(n));
for (int i = 0; i < rank; i++) {
a[i][i] = gen.uniform(1LL, MOD - 1);
for (int j = i + 1; j < n; j++) {
a[i][j] = gen.uniform(0LL, MOD - 1);
}
}
for (int ph = 0; ph < 10000; ph++) {
int x = gen.uniform(0, n - 1);
int y = gen.uniform(0, n - 1);
ll freq = gen.uniform(1LL, MOD - 1);
if (x == y) continue;
if (gen.uniform_bool()) {
for (int i = 0; i < n; i++) {
a[x][i] += freq * a[y][i];
a[x][i] %= MOD;
}
} else {
for (int i = 0; i < n; i++) {
a[i][x] += freq * a[i][y];
a[i][x] %= MOD;
}
}
}

gen.shuffle(a.begin(), a.end());
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
swap(a[i][j], a[j][i]);
}
}
gen.shuffle(a.begin(), a.end());

printf("%d\n", n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%lld", a[i][j]);
if (j + 1 != n) printf(" ");
}
printf("\n");
}
return 0;
}
30 changes: 30 additions & 0 deletions linear_algebra/adjugate_matrix/gen/max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

int main(int, char* argv[]) {


long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = N_MAX;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = gen.uniform<int>(0, MOD - 1);
}
}

printf("%d\n", n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d", a[i][j]);
if (j + 1 != n) printf(" ");
}
printf("\n");
}
return 0;
}
Loading

0 comments on commit c6bfe96

Please sign in to comment.