QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#281831#6425. Harmonious RectanglejrjyyAC ✓4ms3556kbC++203.9kb2023-12-10 21:56:042023-12-10 21:56:04

Judging History

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

  • [2023-12-10 21:56:04]
  • 评测
  • 测评结果:AC
  • 用时:4ms
  • 内存:3556kb
  • [2023-12-10 21:56:04]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

template <typename T>
constexpr T power(T a, i64 b) {
    T c{1};
    while (b) {
        if (b % 2) {
            c *= a;
        }
        a *= a;
        b /= 2;
    }
    return c;
}

template <int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x_) : x{up(x_ % getMod())} {}
    constexpr static MInt fromNormed(int x) {
        MInt v{};
        v.x = x;
        return v;
    }

    static int Mod;
    constexpr static int getMod() {
        return P > 0 ? P : Mod;
    }
    constexpr static void setMod(int Mod_) {
        Mod = Mod_;
    }

    inline constexpr static int up(int x) {
        if (x < 0) {
            x += getMod();
        }
        return x;
    }
    inline constexpr static int down(int x) {
        if (x >= getMod()) {
            x -= getMod();
        }
        return x;
    }
    inline constexpr static int norm(int x) {
        return up(down(x));
    }

    inline constexpr int val() const {
        return x;
    }
    inline explicit constexpr operator int() const {
        return val();
    }
    inline constexpr MInt operator-() const {
        MInt res;
        res.x = norm(getMod() - x);
        return res;
    }
    inline constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, getMod() - 2);
    }
    inline constexpr MInt &operator+=(MInt rhs) {
        x = down(x + rhs.x);
        return *this;
    }
    inline constexpr MInt &operator-=(MInt rhs) {
        x = up(x - rhs.x);
        return *this;
    }
    inline constexpr MInt &operator*=(MInt rhs) {
        x = 1ll * x * rhs.x % getMod();
        return *this;
    }
    inline constexpr MInt &operator/=(MInt rhs) {
        return *this *= rhs.inv();
    }
    friend inline constexpr MInt operator+(MInt lhs, MInt rhs) {
        return lhs += rhs;
    }
    friend inline constexpr MInt operator-(MInt lhs, MInt rhs) {
        return lhs -= rhs;
    }
    friend inline constexpr MInt operator*(MInt lhs, MInt rhs) {
        return lhs *= rhs;
    }
    friend inline constexpr MInt operator/(MInt lhs, MInt rhs) {
        return lhs /= rhs;
    }
    friend inline constexpr bool operator==(MInt lhs, MInt rhs) {
        return lhs.val() == rhs.val();
    }
    friend inline constexpr bool operator!=(MInt lhs, MInt rhs) {
        return lhs.val() != rhs.val();
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 x = 0;
        is >> x;
        a = MInt(x);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
};

template <int P>
int MInt<P>::Mod = P;
template <>
int MInt<0>::Mod = 998244353;

template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 1000000007;
using Z = MInt<P>;

const int f[10][10] = {
    {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683},
    {0, 9, 66, 390, 1800, 6120, 13680, 15120, 0, 0},
    {0, 27, 390, 3198, 13176, 27000, 13680, 15120, 0, 0},
    {0, 81, 1800, 13176, 24336, 4320, 0, 0, 0, 0},
    {0, 243, 6120, 27000, 4320, 4320, 0, 0, 0, 0},
    {0, 729, 13680, 13680, 0, 0, 0, 0, 0, 0},
    {0, 2187, 15120, 15120, 0, 0, 0, 0, 0, 0},
    {0, 6561, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 19683, 0, 0, 0, 0, 0, 0, 0, 0},
};

void solve() {
    int n, m;
    std::cin >> n >> m;

    if (std::min(n, m) == 1) {
        std::cout << "0\n";
        return;
    }

    Z ans = power(Z(3), n * m);
    if (std::max(n, m) <= 9) {
        ans -= f[n][m];
    }
    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3440kb

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: 3556kb

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: 4ms
memory: 3452kb

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: 3504kb

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