QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#89498#5661. Multi-Laddersphtniit#AC ✓0ms3384kbC++113.5kb2023-03-20 10:56:102023-03-20 10:56:12

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-20 10:56:12]
  • 评测
  • 测评结果:AC
  • 用时:0ms
  • 内存:3384kb
  • [2023-03-20 10:56:10]
  • 提交

answer

#include <bits/stdc++.h>

namespace atcoder {

constexpr int P = 1e9+7;
using i64 = long long;

// assume -P <= x < 2P
int norm(int x) {
  if (x < 0) {
    x += P;
  }
  if (x >= P) {
    x -= P;
  }
  return x;
}
template<class T>
T fpower(T a, i64 b) {
  T res = 1;
  for (; b; b /= 2, a *= a) {
    if (b % 2) {
      res *= a;
    }
  }
  return res;
}
struct Z {
  int x;
  Z(int x = 0) : x(norm(x)) {}
  Z(i64 x) : x(norm(x%P)) {}
  int val() const {
    return x;
  }
  Z operator-() const {
    return Z(x == 0 ? 0 : P-x);
    //return Z(norm(P - x));
  }
  Z inv() const {
    assert(x != 0);
    return fpower(*this, P - 2);
  }
  Z &operator*=(const Z &rhs) {
    x = i64(x) * rhs.x % P;
    return *this;
  }
  Z &operator+=(const Z &rhs) {
    x += rhs.x;
    if (x >= P) x -= P;
    //x = norm(x + rhs.x);
    return *this;
  }
  Z &operator-=(const Z &rhs) {
    x -= rhs.x;
    if (x < 0) x += P;
    //x = norm(x - rhs.x);
    return *this;
  }
  Z &operator/=(const Z &rhs) {
    return *this *= rhs.inv();
  }
  friend Z operator*(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res *= rhs;
    return res;
  }
  friend Z operator+(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res += rhs;
    return res;
  }
  friend Z operator-(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res -= rhs;
    return res;
  }
  friend Z operator/(const Z &lhs, const Z &rhs) {
    Z res = lhs;
    res /= rhs;
    return res;
  }
  friend std::istream &operator>>(std::istream &is, Z &a) {
    i64 v;
    is >> v;
    a = Z(v);
    return is;
  }
  friend std::ostream &operator<<(std::ostream &os, const Z &a) {
    return os << a.val();
  }
};

namespace simp {
  std::vector<Z> fac, ifac, invn;
  void check(int x) {
    if (fac.empty()) {
      fac={Z(1), Z(1)};
      ifac={Z(1), Z(1)};
      invn={Z(0), Z(1)};
    }
    while (fac.size()<=x) {
      int n = fac.size(), m = fac.size() * 2;
      fac.resize(m);
      ifac.resize(m);
      invn.resize(m);
      for (int i=n;i<m;i++) {
        fac[i]=fac[i-1]*Z(i);
        invn[i]=Z(P-P/i)*invn[P%i];
        ifac[i]=ifac[i-1]*invn[i];
      }
    }
  }
  Z gfac(int x) {
    check(x); return fac[x];
  }
  Z ginv(int x) {
    check(x); return invn[x];
  }
  Z gifac(int x) {
    check(x); return ifac[x];
  }
  Z binom(int n,int m) {
    if (m < 0 || m > n) return Z(0);
    return gfac(n)*gifac(m)*gifac(n - m);
  }
}

}

inline atcoder::Z C(int n, int m) {
  return atcoder::simp::binom(n, m);
}
inline atcoder::Z F(int n) {
  return atcoder::simp::gfac(n);
}
inline atcoder::Z iF(int n) {
  return atcoder::simp::gifac(n);
}

inline atcoder::Z fpow(long long a, long long b) {
  return atcoder::fpower(atcoder::Z{a}, b);
}

using namespace std;
using i64 = long long;

const int maxn = 500050;


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

  int tes;
  cin >> tes;
  while (tes--) {
    int n, m, k;
    cin >> n >> m >> k;
    if (k < 2) {
      cout << 0 << endl;
      continue;
    }

    atcoder::Z res = fpow(k-1, m);
    if (m & 1) {
      res -= k-1;
    } else {
      res += k-1;
    }
    //cout << res << endl;

    atcoder::Z one = k-1 + atcoder::Z{k-2} * (k-2);
    //cout << one << endl;
    one = fpow(one.x, n-1);
    //cout << one << endl;
    one = fpow(one.x, m);
    //cout << one << endl;

    cout << one * res << endl;
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
2 3 3

output:

162

result:

ok single line: '162'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3368kb

input:

20
2 3 3
1 3 3
10 3 0
10 3 2
1 21 2
1 22 0
2000 15000 2000
12000 30000 200000
1000000000 3 3
2 1000000000 3
2 3 100000000
1000000000 1000000000 10
1000000000 3 100000000
2 1000000000 100000000
1 1000000000 10
1 1000000000 100000000
1 1000 100000000
1000000000 1000000000 0
1000000000 1000000000 1
100...

output:

162
6
0
0
0
0
349400141
243010659
52489881
53690844
176686901
218103365
558243892
991895211
693053429
883715672
80402569
0
0
311752813

result:

ok 20 lines