Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[問題追加] pow of matrix #1052

Merged
merged 3 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions math/pow_of_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");
}
}
3 changes: 3 additions & 0 deletions math/pow_of_matrix/gen/example_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 7
0 1
1 1
4 changes: 4 additions & 0 deletions math/pow_of_matrix/gen/example_01.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3 0
0 0 0
0 0 0
0 0 0
4 changes: 4 additions & 0 deletions math/pow_of_matrix/gen/example_02.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
3 1000000000000000000
1 2 3
4 5 6
7 8 9
59 changes: 59 additions & 0 deletions math/pow_of_matrix/gen/lowrank_max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

using ll = long long;

void out(int n, ll K, vector<vector<ll>> a) {
printf("%d %lld\n", n, K);
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");
}
}

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());

ll K = gen.uniform<ll>(0, K_MAX);
out(n, K, a);
return 0;
}
33 changes: 33 additions & 0 deletions math/pow_of_matrix/gen/max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

using ll = long long;

void out(int n, ll K, vector<vector<ll>> a) {
printf("%d %lld\n", n, K);
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");
}
}

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

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

ll K = gen.uniform<ll>(0, K_MAX);
out(n, K, a);
return 0;
}
33 changes: 33 additions & 0 deletions math/pow_of_matrix/gen/max_random_worst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

using ll = long long;

void out(int n, ll K, vector<vector<ll>> a) {
printf("%d %lld\n", n, K);
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");
}
}

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

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

ll K = (1LL << 59) - 1;
out(n, K, a);
return 0;
}
31 changes: 31 additions & 0 deletions math/pow_of_matrix/gen/perm_max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;
using ll = long long;

void out(int n, ll K, vector<vector<ll>> a) {
printf("%d %lld\n", n, K);
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");
}
}

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<ll>> a(n, vector<ll>(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); }

ll K = gen.uniform<ll>(0, K_MAX);
out(n, K, a);
return 0;
}
33 changes: 33 additions & 0 deletions math/pow_of_matrix/gen/random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

using ll = long long;

void out(int n, ll K, vector<vector<ll>> a) {
printf("%d %lld\n", n, K);
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");
}
}

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

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

ll K = gen.uniform<ll>(0, K_MAX);
out(n, K, a);
return 0;
}
35 changes: 35 additions & 0 deletions math/pow_of_matrix/gen/signed_overflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <set>
#include <limits>

#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 val = MOD / 2 - 1;
unsigned long long max_ll = numeric_limits<long long>::max();

// Find smallest size n where n * val * val > max_ll
int n = 0;
for (unsigned long long res = 0; res <= max_ll;
res += (long long)val * val, ++n)
;
ll K = gen.uniform<ll>(0, K_MAX);

printf("%d %lld\n", n, K);

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j) printf(" ");
printf("%d", val);
}
printf("\n");
}
return 0;
}
37 changes: 37 additions & 0 deletions math/pow_of_matrix/gen/small.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include "random.h"
#include "../params.h"

using namespace std;

using ll = long long;

void out(int n, ll K, vector<vector<ll>> a) {
printf("%d %lld\n", n, K);
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");
}
}

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

vector<int> ns = {1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3};
vector<int> ks = {0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3};

int n = ns[seed % 12];
int k = ks[seed % 12];

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

out(n, k, a);
return 0;
}
39 changes: 39 additions & 0 deletions math/pow_of_matrix/gen/unsigned_overflow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <set>
#include <limits>

#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 val = MOD - 2;

// Find smallest size n where n * val * val > numeric_limits<unsigned long
// long>::max()
int n = 0;
unsigned long long res = 0;
long long val2 = (long long)val * val;
while (res + val2 >= res) // Check if adding val2 to res will overflow
{
res += val2;
++n;
}

ll K = gen.uniform<ll>(0, K_MAX);
printf("%d %lld\n", n, K);

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (j) printf(" ");
printf("%d", val);
}
printf("\n");
}
return 0;
}
Loading