QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#694406#6425. Harmonious Rectangleatzk#TL 1ms3872kbC++202.4kb2024-10-31 17:53:182024-10-31 17:53:25

Judging History

你现在查看的是最新测评结果

  • [2024-10-31 17:53:25]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3872kb
  • [2024-10-31 17:53:18]
  • 提交

answer

# include <bits/stdc++.h>
using namespace std;

# ifndef ONLINE_JUDGE
# include "debug.h"
# else
# define debug(...) 0721
# define ps 0721
# endif

using i32 = signed;
using i64 = long long;

const int mod = 1e9 + 7;

int power(int a,int n) {
    int res = 1;
    while(n) {
        if(n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

vector<vector<int>> res(10,vector<int> (10));
void solve() {
    int n,m;
    cin >> n >> m;

    vector<vector<int>> a(n + 1,vector<int>(m + 1, 0));
    vector<vector<int>> b(n + 1,vector<int>(4,0));
    auto check = [&] (int x,int y,int val) -> bool {
        a[x][y] = val;
        if(b[x][0] <=3  && b[x][1] <= 3 && b[x][2] <= 3) {
            for (int i = 1; i < x; i++) {
                for (int j = 1; j < y; j++) {
                    
                    if(a[i][j] == a[i][y] && a[x][y] == a[x][j]) {
                        return false;
                    }
                    if(a[i][j] == a[x][j] && a[i][y] == a[x][y]) {
                        return false;
                    }
                } 
            }
            return true;
        }

        return false;
    };
    int ans = 0;

    auto dfs = [&] (auto self,int x,int y) -> void {
        if(x > n) {
            ans++;
            return;
        }
        if(y > m) {
            self(self,x + 1,1);
            return;
        }
        for (int i = 1; i <=3 ; i++) {
            b[x][i]++;
            if(check(x,y,i)) {
                self(self,x,y + 1);
            }
            b[x][i]--;
        }
    };
    if(n == 1 || m == 1) {
        cout << 0 << '\n';
        return;
    }
    if(n > 7 || n > 7) {
        cout << power(3,n * m) << '\n';
    }
    if(res[n][m] != -1) {
        cout << res[n][m] << '\n';
    }
    else {
         dfs(dfs,1,1);
        res[n][m] = power(3, n * m) - ans;
        res[n][m] = (res[n][m] + mod) % mod;
        cout << res[n][m] << '\n';
    }

}

i32 main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

# ifndef ONLINE_JUDGE
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
    freopen("Error.txt", "w", stderr);
# endif

    for (int i = 1; i < 9;i++) {
        for (int j = 1; j < 9;j++) {
            res[i][j] = -1;
        }
    }
    i32 t = 1;
    cin >> t;
    while (t --) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3872kb

input:

3
1 4
2 2
3 3

output:

0
15
16485

result:

ok 3 number(s): "0 15 16485"

Test #2:

score: -100
Time Limit Exceeded

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:


result: