QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#110421#6518. Not Another Linear Algebra Problemhos_lyricAC ✓819ms277128kbC++147.3kb2023-06-02 07:23:592023-06-02 07:24:03

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-02 07:24:03]
  • 评测
  • 测评结果:AC
  • 用时:819ms
  • 内存:277128kb
  • [2023-06-02 07:23:59]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
  static unsigned M;
  static unsigned long long NEG_INV_M;
  static void setM(unsigned long long m) { M = m; NEG_INV_M = -1ULL / M; }
  unsigned x;
  ModInt() : x(0U) {}
  ModInt(unsigned x_) : x(x_ % M) {}
  ModInt(unsigned long long x_) : x(x_ % M) {}
  ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) {
    const unsigned long long y = static_cast<unsigned long long>(x) * a.x;
    const unsigned long long q = static_cast<unsigned long long>((static_cast<unsigned __int128>(NEG_INV_M) * y) >> 64);
    const unsigned long long r = y - M * q;
    x = r - M * (r >= M);
    return *this;
  }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
unsigned ModInt::M;
unsigned long long ModInt::NEG_INV_M;
// !!!Use ModInt::setM!!!
////////////////////////////////////////////////////////////////////////////////

using Mint = ModInt;


struct Power {
  static constexpr int E = 16;
  vector<Mint> baby, giant;
  Power() {}
  Power(Mint a) : baby((1 << E) + 1), giant(1 << E) {
    baby[0] = 1;
    for (int i = 1; i <= 1 << E; ++i) baby[i] = baby[i - 1] * a;
    giant[0] = 1;
    for (int i = 1; i < 1 << E; ++i) giant[i] = giant[i - 1] * baby[1 << E];
  }
  Mint operator()(unsigned e) const {
    return giant[e >> E] * baby[e & ((1 << E) - 1)];
  }
};

/*
  U \subseteq F^N, subspace
  a(U) := #{ p \in Aut(F^N) | Ker(p - id) = U }
  b(U) := #{ p \in Aut(F^N) | p|_U = id }
  b(U) = \sum[V \supseteq U] a(V)
  
  a[dim(U)] := a(U)
  b[dim(U)] := b(U)
  b[i] = \sum[j>=i] qbinom(N-i, N-j) a[j]
  reverse a, b
  b[i] = \sum[j<=i] qbinom(i, j) a[j]
  
  b[i] = (q^N-q^(N-i)) ... (q^N-q^(N-1))
  
  A(x) := \sum[i] a[i] x^i / (q;q)_i
  B(x) := \sum[i] b[i] x^i / (q;q)_i
  B(x) = A(x) / (x;q)_INF
  A(x) = B(x) (x;q)_INF
  
  B(x) = \sum[i] (-1)^i q^(binom(N,2)-binom(N-i,2)) x^i
  x B(x) = \sum[i>=0] (-1)^i q^(binom(N,2)-binom(N-i,2)) x^i
         = \sum[i>=1] (-1)^(i-1) q^(binom(N,2)-binom(N-i+1,2)) x^i
         = \sum[i>=1] (-1)^i q^(binom(N,2)-binom(N-i,2)) (-1) q^(i-N) x^i
  B(qx) = 1 - q^N x B(x)
  A(qx) = B(qx) (qx;q)_INF
        = (1 - q^N x B(x)) (x;q)_INF / (1-x)
  (1-x) A(qx) = (x;q)_INF - q^N x A(x)
  
  take [x^i/(q;q)_i]
  q^i a[i] - q^(i-1) (1-q^i) a[i-1] = (-1)^i q^(binom(i,2)) - q^N (1-q^i) a[i-1]
*/

int main() {
  int N, Q, M;
  for (; ~scanf("%d%d%d", &N, &Q, &M); ) {
    Mint::setM(M);
    
    const Mint q = Q;
    const Mint invQ = q.inv();
    vector<Mint> qq(N + 1), qq2(N + 1);
    qq[0] = 1;
    qq2[0] = 1;
    for (int i = 1; i <= N; ++i) {
      qq[i] = qq[i-1] * q;
      qq2[i] = qq2[i-1] * qq[i-1];
    }
    int ord = N + 1;
    for (int i = 1; i <= N; ++i) if (qq[i] == 1) {
      ord = i;
      break;
    }
    vector<Mint> fac(N + 1), invFac(N + 1);
    fac[0] = 1;
    for (int i = 1; i <= N; ++i) {
      fac[i] = fac[i-1] * i;
    }
    invFac[N] = fac[N].inv();
    for (int i = N; i >= 1; --i) {
      invFac[i-1] = i * invFac[i];
    }
    vector<Mint> fs(ord), invFs(ord);
    fs[0] = 1;
    for (int i = 1; i < ord; ++i) {
      fs[i] = fs[i-1] * (1 - qq[i]);
    }
    invFs[ord-1] = fs[ord-1].inv();
    for (int i = ord; --i >= 1; ) {
      invFs[i-1] = (1 - qq[i]) * invFs[i];
    }
    auto qb = [&](int n, int k) -> Mint {
      const int nq = n / ord, nr = n % ord;
      const int kq = k / ord, kr = k % ord;
      if (kr > nr) return 0;
      return (fac[nq] * invFac[kq] * invFac[nq - kq]) * (fs[nr] * invFs[kr] * invFs[nr - kr]);
    };
    
    vector<Mint> as(N + 1);
    as[0] = 1;
    {
      Mint iqq = 1;
      for (int i = 1; i <= N; ++i) {
        iqq *= invQ;
        as[i] = iqq * (((i&1)?-1:+1) * qq2[i] + (qq[i-1] - qq[N]) * (1 - qq[i]) * as[i - 1]);
      }
    }
// cerr<<"as = "<<as<<endl;
    for (int i = 0; i <= N; ++i) {
      as[i] *= qb(N, i);
    }
// cerr<<"as = "<<as<<endl;
    
    Int qn = 1;
    for (int i = 0; i < N; ++i) {
      (qn *= Q) %= (M - 1);
    }
    if (qn == 0) qn = M - 1;
    
    const Power three(3);
    Mint ans = 0;
    Int e = 1;
    for (int i = 0; i <= N; ++i) {
      ans += three(e) * as[N - i];
      (e *= qn) %= (M - 1);
    }
    printf("%u\n", ans.x);
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 3664kb

input:

2 2 1000000007

output:

43046970

result:

ok 1 number(s): "43046970"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3616kb

input:

100 127 998244353

output:

881381862

result:

ok 1 number(s): "881381862"

Test #3:

score: 0
Accepted
time: 2ms
memory: 3588kb

input:

1 2 540053233

output:

9

result:

ok 1 number(s): "9"

Test #4:

score: 0
Accepted
time: 3ms
memory: 3636kb

input:

2 2 156542707

output:

43046970

result:

ok 1 number(s): "43046970"

Test #5:

score: 0
Accepted
time: 3ms
memory: 3688kb

input:

1 2 186225229

output:

9

result:

ok 1 number(s): "9"

Test #6:

score: 0
Accepted
time: 3ms
memory: 3632kb

input:

3 3 109884329

output:

100602209

result:

ok 1 number(s): "100602209"

Test #7:

score: 0
Accepted
time: 3ms
memory: 3688kb

input:

1 2 144802297

output:

9

result:

ok 1 number(s): "9"

Test #8:

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

input:

20 21992843 328859143

output:

110137213

result:

ok 1 number(s): "110137213"

Test #9:

score: 0
Accepted
time: 3ms
memory: 3636kb

input:

22 332524739 654888401

output:

410922781

result:

ok 1 number(s): "410922781"

Test #10:

score: 0
Accepted
time: 1ms
memory: 3656kb

input:

26 302215049 566649113

output:

221720840

result:

ok 1 number(s): "221720840"

Test #11:

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

input:

15 111009527 722130737

output:

648834664

result:

ok 1 number(s): "648834664"

Test #12:

score: 0
Accepted
time: 3ms
memory: 3644kb

input:

82 110032063 394529383

output:

111730592

result:

ok 1 number(s): "111730592"

Test #13:

score: 0
Accepted
time: 3ms
memory: 3688kb

input:

9 11172911 259650437

output:

68381774

result:

ok 1 number(s): "68381774"

Test #14:

score: 0
Accepted
time: 3ms
memory: 3668kb

input:

86 12016027 354886243

output:

263687778

result:

ok 1 number(s): "263687778"

Test #15:

score: 0
Accepted
time: 3ms
memory: 3692kb

input:

91 273689959 454097881

output:

114436127

result:

ok 1 number(s): "114436127"

Test #16:

score: 0
Accepted
time: 3ms
memory: 3636kb

input:

73 148878967 694206977

output:

176215101

result:

ok 1 number(s): "176215101"

Test #17:

score: 0
Accepted
time: 2ms
memory: 3644kb

input:

45 205982233 227598247

output:

156769598

result:

ok 1 number(s): "156769598"

Test #18:

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

input:

2778 122825869 147297463

output:

43419574

result:

ok 1 number(s): "43419574"

Test #19:

score: 0
Accepted
time: 1ms
memory: 3644kb

input:

289 7729669 589652893

output:

552952137

result:

ok 1 number(s): "552952137"

Test #20:

score: 0
Accepted
time: 3ms
memory: 3716kb

input:

2281 35651417 203950963

output:

21659018

result:

ok 1 number(s): "21659018"

Test #21:

score: 0
Accepted
time: 1ms
memory: 3708kb

input:

1684 258745639 373223677

output:

355596229

result:

ok 1 number(s): "355596229"

Test #22:

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

input:

2107 86850989 455823859

output:

245960059

result:

ok 1 number(s): "245960059"

Test #23:

score: 0
Accepted
time: 3ms
memory: 3652kb

input:

1323 43290799 791120419

output:

509649562

result:

ok 1 number(s): "509649562"

Test #24:

score: 0
Accepted
time: 3ms
memory: 3736kb

input:

2401 34064903 185314627

output:

70571452

result:

ok 1 number(s): "70571452"

Test #25:

score: 0
Accepted
time: 3ms
memory: 3688kb

input:

1073 82288187 564447959

output:

168200843

result:

ok 1 number(s): "168200843"

Test #26:

score: 0
Accepted
time: 3ms
memory: 3724kb

input:

1926 29995039 129122281

output:

60921463

result:

ok 1 number(s): "60921463"

Test #27:

score: 0
Accepted
time: 3ms
memory: 3776kb

input:

3000 66915659 765705179

output:

222619979

result:

ok 1 number(s): "222619979"

Test #28:

score: 0
Accepted
time: 87ms
memory: 31088kb

input:

998818 198334853 998244353

output:

153251445

result:

ok 1 number(s): "153251445"

Test #29:

score: 0
Accepted
time: 76ms
memory: 28752kb

input:

914379 128814383 998244353

output:

477606145

result:

ok 1 number(s): "477606145"

Test #30:

score: 0
Accepted
time: 79ms
memory: 29532kb

input:

944474 478445339 998244353

output:

174204073

result:

ok 1 number(s): "174204073"

Test #31:

score: 0
Accepted
time: 83ms
memory: 29888kb

input:

948637 711592127 998244353

output:

178256114

result:

ok 1 number(s): "178256114"

Test #32:

score: 0
Accepted
time: 74ms
memory: 29004kb

input:

927564 14465663 998244353

output:

315244613

result:

ok 1 number(s): "315244613"

Test #33:

score: 0
Accepted
time: 81ms
memory: 29236kb

input:

934615 392799073 998244353

output:

892700270

result:

ok 1 number(s): "892700270"

Test #34:

score: 0
Accepted
time: 73ms
memory: 28724kb

input:

917196 124972031 998244353

output:

782017412

result:

ok 1 number(s): "782017412"

Test #35:

score: 0
Accepted
time: 80ms
memory: 29852kb

input:

957149 392606173 998244353

output:

159348443

result:

ok 1 number(s): "159348443"

Test #36:

score: 0
Accepted
time: 83ms
memory: 30908kb

input:

997042 184649453 998244353

output:

464643024

result:

ok 1 number(s): "464643024"

Test #37:

score: 0
Accepted
time: 77ms
memory: 29764kb

input:

953353 14071961 998244353

output:

391688875

result:

ok 1 number(s): "391688875"

Test #38:

score: 0
Accepted
time: 787ms
memory: 274712kb

input:

9909956 720431399 720431401

output:

86883659

result:

ok 1 number(s): "86883659"

Test #39:

score: 0
Accepted
time: 810ms
memory: 275020kb

input:

9924163 267052829 267052831

output:

75754681

result:

ok 1 number(s): "75754681"

Test #40:

score: 0
Accepted
time: 781ms
memory: 276244kb

input:

9967885 197873129 197873131

output:

16653739

result:

ok 1 number(s): "16653739"

Test #41:

score: 0
Accepted
time: 683ms
memory: 220144kb

input:

9952642 101872151 101872153

output:

0

result:

ok 1 number(s): "0"

Test #42:

score: 0
Accepted
time: 796ms
memory: 275880kb

input:

9955909 167874431 167874433

output:

130012020

result:

ok 1 number(s): "130012020"

Test #43:

score: 0
Accepted
time: 811ms
memory: 276936kb

input:

9994785 399509567 399509569

output:

153324498

result:

ok 1 number(s): "153324498"

Test #44:

score: 0
Accepted
time: 781ms
memory: 275836kb

input:

9954011 108819131 108819133

output:

101671540

result:

ok 1 number(s): "101671540"

Test #45:

score: 0
Accepted
time: 770ms
memory: 277084kb

input:

9997570 213315827 213315829

output:

57441081

result:

ok 1 number(s): "57441081"

Test #46:

score: 0
Accepted
time: 803ms
memory: 277128kb

input:

9995867 113028299 113028301

output:

67837072

result:

ok 1 number(s): "67837072"

Test #47:

score: 0
Accepted
time: 799ms
memory: 274756kb

input:

9909335 247275617 247275619

output:

202966817

result:

ok 1 number(s): "202966817"

Test #48:

score: 0
Accepted
time: 808ms
memory: 274960kb

input:

9921815 38466881 725310841

output:

601117286

result:

ok 1 number(s): "601117286"

Test #49:

score: 0
Accepted
time: 798ms
memory: 274928kb

input:

9919464 4830599 747345523

output:

168521454

result:

ok 1 number(s): "168521454"

Test #50:

score: 0
Accepted
time: 805ms
memory: 276576kb

input:

9981374 3616373 154722097

output:

2696288

result:

ok 1 number(s): "2696288"

Test #51:

score: 0
Accepted
time: 788ms
memory: 274488kb

input:

9906664 12433457 558159149

output:

538699014

result:

ok 1 number(s): "538699014"

Test #52:

score: 0
Accepted
time: 796ms
memory: 276684kb

input:

9985736 46853 410275823

output:

258567756

result:

ok 1 number(s): "258567756"

Test #53:

score: 0
Accepted
time: 805ms
memory: 276092kb

input:

9962926 33790087 203505083

output:

40932778

result:

ok 1 number(s): "40932778"

Test #54:

score: 0
Accepted
time: 780ms
memory: 274404kb

input:

9903735 146658401 157137433

output:

154493145

result:

ok 1 number(s): "154493145"

Test #55:

score: 0
Accepted
time: 805ms
memory: 274736kb

input:

9913516 105010771 110717611

output:

67979325

result:

ok 1 number(s): "67979325"

Test #56:

score: 0
Accepted
time: 789ms
memory: 275832kb

input:

9953517 268142489 675913921

output:

523115756

result:

ok 1 number(s): "523115756"

Test #57:

score: 0
Accepted
time: 802ms
memory: 276552kb

input:

9981005 11993207 114120883

output:

7261617

result:

ok 1 number(s): "7261617"

Test #58:

score: 0
Accepted
time: 799ms
memory: 275540kb

input:

9945956 36522077 168104303

output:

82398556

result:

ok 1 number(s): "82398556"

Test #59:

score: 0
Accepted
time: 804ms
memory: 276252kb

input:

9967933 15301477 352827883

output:

242773007

result:

ok 1 number(s): "242773007"

Test #60:

score: 0
Accepted
time: 793ms
memory: 274744kb

input:

9911781 83845891 360130933

output:

158254305

result:

ok 1 number(s): "158254305"

Test #61:

score: 0
Accepted
time: 801ms
memory: 274844kb

input:

9916390 100404191 108138473

output:

103346432

result:

ok 1 number(s): "103346432"

Test #62:

score: 0
Accepted
time: 819ms
memory: 276380kb

input:

9974438 7828049 430399297

output:

76675277

result:

ok 1 number(s): "76675277"