QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#618546 | #6425. Harmonious Rectangle | stavage | AC ✓ | 3ms | 3644kb | C++17 | 1.9kb | 2024-10-06 23:30:49 | 2024-10-06 23:30:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define int LL
const int N = 2e3 + 5;
const int MOD = 1e9 + 7;
int ksm(int a, int b, int c)
{
int ans = 1;
while (b) {
if (b & 1) ans = ans * a % c;
b >>= 1;
a = a * a % c;
}
return ans;
}
int a[15][15];
int lx, ly;
int dfs(int x, int y)
{
int ans = 0;
for (int i = 1; i <= 3; i++) {
a[x][y] = i;
int f = 1;
for (int j = x - 1; j >= 1; j--) {
for (int z = y - 1; z >= 1 && f; z--) {
if (a[x][y] == a[j][y] && a[x][z] == a[j][z]) f = 0;
if (a[x][y] == a[x][z] && a[j][y] == a[j][z]) f = 0;
}
if (!f) break;
}
if (f) {
if (x == lx && y == ly) ans++;
else if (x == lx)
ans += dfs(1, y + 1);
else
ans += dfs(x + 1, y);
}
}
return ans;
}
int res[9][9] =
{3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 9, 66, 390, 1800, 6120, 13680, 15120, 0, 0, 27, 390, 3198, 13176, 27000,
13680, 15120, 0, 0, 81, 1800, 13176, 24336, 4320, 0, 0, 0, 0, 243, 6120, 27000, 4320, 4320, 0, 0, 0, 0, 729, 13680,
13680, 0, 0, 0, 0, 0, 0, 2187, 15120, 15120, 0, 0, 0, 0, 0, 0, 6561, 0, 0, 0, 0, 0, 0, 0, 0, 19683, 0, 0, 0, 0, 0,
0, 0, 0};
void init()
{
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
memset(a, 0, sizeof a);
lx = i, ly = j;
res[i][j] = dfs(1, 1);
}
}
}
void solve()
{
int n, m;
cin >> n >> m;
if (n == 1 || m == 1) {
cout << "0\n";
return;
}
if (n <= 9 && m <= 9) {
cout << (ksm(3, n * m, MOD) - res[n - 1][m - 1] + MOD) % MOD << "\n";
}
else
cout << ksm(3, n * m, MOD) << "\n";
}
signed main()
{
// init();
ios::sync_with_stdio(false), cin.tie(0);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3644kb
input:
3 1 4 2 2 3 3
output:
0 15 16485
result:
ok 3 number(s): "0 15 16485"
Test #2:
score: 0
Accepted
time: 2ms
memory: 3640kb
input:
10000 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 6...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 339 4761 52929 517761 4767849 43046721 387420489 486784380 381059392 429534507 865810542 792294...
result:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 3ms
memory: 3636kb
input:
10000 1705 810 454 699 1749 1057 1177 326 132 74 1657 1927 1688 781 1870 1278 261 681 901 1166 740 1088 1762 344 1519 1027 887 1049 1871 1800 533 173 873 1725 1960 1555 1582 628 1197 453 1668 810 1882 468 1163 1011 1077 627 438 113 1150 480 927 407 1393 1348 784 1650 198 903 939 1930 173 1726 1276 1...
output:
4664542 474135175 453284040 865070735 892936842 930878193 929505944 730204219 878002827 776271982 319486673 354630833 31019262 653603083 316945266 163467758 910052980 502234498 692029941 37337160 838442854 94243481 112835966 363282856 563398619 682187998 379850832 751053515 449670075 419120473 96439...
result:
ok 10000 numbers
Test #4:
score: 0
Accepted
time: 2ms
memory: 3592kb
input:
10000 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 7 3 ...
output:
460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 460338013 ...
result:
ok 10000 numbers