QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#260370 | #7838. 往日之影 | hos_lyric# | 40 | 127ms | 15668kb | C++14 | 7.6kb | 2023-11-22 04:52:21 | 2024-07-04 03:08:06 |
Judging History
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 <random>
#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; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
// Barrett
struct ModInt {
static unsigned M;
static unsigned long long NEG_INV_M;
static void setM(unsigned 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;
constexpr int LIM_INV = 1'000'010;
Mint inv[LIM_INV], fac[LIM_INV], invFac[LIM_INV];
void prepare() {
inv[1] = 1;
for (int i = 2; i < LIM_INV; ++i) {
inv[i] = -((Mint::M / i) * inv[Mint::M % i]);
}
fac[0] = invFac[0] = 1;
for (int i = 1; i < LIM_INV; ++i) {
fac[i] = fac[i - 1] * i;
invFac[i] = invFac[i - 1] * inv[i];
}
}
Mint binom(Int n, Int k) {
if (n < 0) {
if (k >= 0) {
return ((k & 1) ? -1 : +1) * binom(-n + k - 1, k);
} else if (n - k >= 0) {
return (((n - k) & 1) ? -1 : +1) * binom(-k - 1, n - k);
} else {
return 0;
}
} else {
if (0 <= k && k <= n) {
assert(n < LIM_INV);
return fac[n] * invFac[k] * invFac[n - k];
} else {
return 0;
}
}
}
struct F {
Mint x, y;
F(Mint x_ = 0, Mint y_ = 0) : x(x_), y(y_) {}
friend ostream &operator<<(ostream &os, const F &f) {
if (!f.y) return os << f.x;
if (!f.x) return os << f.y << "i";
return os << f.x << "+" << f.y << "i";
}
F operator+(const F &f) const { return F(x + f.x, y + f.y); }
F operator*(const F &f) const { return F(x * f.x - y * f.y, x * f.y + y * f.x); }
F operator*(const Mint &a) const { return F(x * a, y * a); }
};
int main() {
int MO;
for (int numCases; ~scanf("%d%d", &numCases, &MO); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
Mint::setM(MO);
prepare();
const F I[4] = {F(1, 0), F(0, 1), F(-1, 0), F(0, -1)};
int N, A[4];
scanf("%d", &N);
for (int j = 0; j < 4; ++j) {
scanf("%d", &A[j]);
}
/*
i^1: at most 1
i^3: at most 1
i^0, i^2: not both
*/
F ans;
for (const int s : {0, 2}) {
int base = 0;
for (int j = 0; j < 4; ++j) {
base -= A[j] * (s * j);
}
base &= 3;
if (N >= (s ? 1 : 0)) {
// 2^binom(N,2)
const F f = I[base] * Mint(2).pow((Int)N*(N-1)/2);
// cerr<<"s="<<s<<" "<<__LINE__<<"> "<<f<<endl;
ans = ans + f;
}
if (N - 1 >= (s ? 1 : 0)) {
F f;
for (int j1 = 0; j1 < 4; ++j1) if (A[j1]) {
f = f + I[(base - 1 * j1) & 3] * A[j1];
}
// 2^binom(N-1,2) (1+i)^(N-1)
f = f * Mint(2).pow((Int)(N-1)*(N-2)/2 + (N-1)/2) * I[((N-1)/2) & 3];
if ((N-1) & 1) {
f = f * F(1, 1);
}
// cerr<<"s="<<s<<" "<<__LINE__<<"> "<<f<<endl;
ans = ans + f;
}
if (N - 1 >= (s ? 1 : 0)) {
F f;
for (int j3 = 0; j3 < 4; ++j3) if (A[j3]) {
f = f + I[(base - 3 * j3) & 3] * A[j3];
}
// 2^binom(N-1,2) (1-i)^(N-1)
f = f * Mint(2).pow((Int)(N-1)*(N-2)/2 + (N-1)/2) * I[(-((N-1)/2)) & 3];
if ((N-1) & 1) {
f = f * F(1, -1);
}
// cerr<<"s="<<s<<" "<<__LINE__<<"> "<<f<<endl;
ans = ans + f;
}
if (N - 2 >= (s ? 1 : 0)) {
F f;
for (int j1 = 0; j1 < 4; ++j1) if (A[j1]) {
const Mint a = A[j1];
--A[j1];
for (int j3 = 0; j3 < 4; ++j3) if (A[j3]) {
const Mint aa = a * A[j3];
f = f + I[(base - 1 * j1 - 3 * j3) & 3] * aa;
}
++A[j1];
}
// 2^binom(N-2,2) (1+i)^(N-2) (1-i)^(N-2) 2^1
f = f * Mint(2).pow((Int)(N-2)*(N-3)/2 + (N-2) + 1);
// cerr<<"s="<<s<<" "<<__LINE__<<"> "<<f<<endl;
ans = ans + f;
}
}
ans = ans * Mint(4).pow(-N);
// cerr<<"ans = "<<ans<<endl;
ans = ans * Mint(2).pow(-(Int)N*(N-1)/2);
printf("%u\n", ans.x.x);
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
詳細信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 8ms
memory: 15528kb
input:
1 998244353 3 1 1 0 1
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 12ms
memory: 15584kb
input:
1 998244353 7 0 2 1 4
output:
998069185
result:
ok single line: '998069185'
Test #3:
score: 0
Accepted
time: 8ms
memory: 15580kb
input:
1 998244353 4 0 1 0 3
output:
0
result:
ok single line: '0'
Test #4:
score: 0
Accepted
time: 11ms
memory: 15508kb
input:
1 998244353 2 1 0 1 0
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 8ms
memory: 15480kb
input:
1 998244353 6 2 1 0 3
output:
997696001
result:
ok single line: '997696001'
Test #6:
score: 0
Accepted
time: 7ms
memory: 15508kb
input:
1 998244353 2 1 0 1 0
output:
0
result:
ok single line: '0'
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #7:
score: 20
Accepted
time: 8ms
memory: 15476kb
input:
1 998244353 40 11 9 9 11
output:
855684614
result:
ok single line: '855684614'
Test #8:
score: 0
Accepted
time: 7ms
memory: 15588kb
input:
1 998244353 39 12 8 7 12
output:
629648158
result:
ok single line: '629648158'
Test #9:
score: 0
Accepted
time: 8ms
memory: 15592kb
input:
1 998244353 38 13 8 9 8
output:
944107686
result:
ok single line: '944107686'
Test #10:
score: 0
Accepted
time: 8ms
memory: 15608kb
input:
1 998244353 37 16 7 7 7
output:
393700837
result:
ok single line: '393700837'
Test #11:
score: 0
Accepted
time: 38ms
memory: 15592kb
input:
4 998244353 10 0 3 2 5 9 2 1 1 5 10 1 2 3 4 9 4 0 3 2
output:
124779131 998235309 124779131 998236023
result:
ok 4 lines
Test #12:
score: 0
Accepted
time: 35ms
memory: 15604kb
input:
4 998244353 10 2 1 4 3 9 1 2 4 2 10 3 2 5 0 9 3 2 2 2
output:
124779131 998239593 124778655 998236261
result:
ok 4 lines
Test #13:
score: 0
Accepted
time: 33ms
memory: 15612kb
input:
4 998244353 10 1 4 1 4 9 2 0 5 2 10 3 5 1 1 9 6 1 1 1
output:
623900891 998239355 374338940 998231025
result:
ok 4 lines
Test #14:
score: 0
Accepted
time: 69ms
memory: 15668kb
input:
8 998244353 4 2 1 0 1 4 0 2 0 2 5 0 1 1 3 4 0 1 0 3 5 1 1 0 3 5 0 3 1 1 4 2 2 0 0 5 1 1 2 1
output:
0 0 995319809 0 997269505 995319809 982646785 996294657
result:
ok 8 lines
Test #15:
score: 0
Accepted
time: 72ms
memory: 15584kb
input:
8 998244353 4 2 1 0 1 4 3 0 1 0 4 0 2 2 0 5 2 0 1 2 5 1 2 0 2 5 0 1 1 3 5 0 1 1 3 4 1 1 1 1
output:
0 0 967049217 997269505 0 995319809 995319809 0
result:
ok 8 lines
Test #16:
score: 0
Accepted
time: 30ms
memory: 15612kb
input:
3 998244353 30 1 10 7 12 8 0 3 0 5 2 0 1 0 1
output:
54137262 998208177 0
result:
ok 3 lines
Subtask #3:
score: 10
Accepted
Dependency #2:
100%
Accepted
Test #17:
score: 10
Accepted
time: 8ms
memory: 15532kb
input:
1 998244353 99 12 22 33 32 15 998244353 7 1 2 2 2 6 1 2 1 2 6 1 1 3 1 7 0 3 1 3 7 1 1 2 3 7 3 1 0 3 6 1 2 1 2 7 1 2 2 2 7 3 3 0 1 6 1 1 3 1 7 5 1 0 1 6 3 0 1 2 6 2 0 0 4 7 2 0 1 4 6 4 1 0 1
output:
940435798
result:
ok single line: '940435798'
Test #18:
score: 0
Accepted
time: 8ms
memory: 15600kb
input:
1 998244353 98 27 29 23 19 15 998244353 7 2 2 1 2 6 0 1 2 3 6 0 2 2 2 6 6 0 0 0 7 4 1 1 1 7 2 1 1 3 7 2 1 1 3 7 2 3 1 1 7 3 3 0 1 6 3 1 1 1 6 3 1 1 1 7 2 1 1 3 6 1 1 3 1 6 1 1 3 1 6 2 0 4 0
output:
147819391
result:
ok single line: '147819391'
Test #19:
score: 0
Accepted
time: 26ms
memory: 15480kb
input:
3 998244353 70 15 17 13 25 20 8 3 4 5 10 3 2 5 0 15 998244353 7 5 1 0 1 6 0 1 4 1 7 1 3 2 1 6 1 0 1 4 7 2 2 1 2 7 2 1 1 3 7 2 3 1 1 6 6 0 0 0 6 3 2 1 0 7 2 3 1 1 7 1 1 2 3 6 2 1 2 1 6 1 2 1 2 6 1 1 1 3 7 2 2 3 0
output:
300715311 500913543 124778655
result:
ok 3 lines
Test #20:
score: 0
Accepted
time: 25ms
memory: 15592kb
input:
3 998244353 70 21 23 11 15 20 5 4 3 8 10 5 1 1 3 15 998244353 6 1 0 3 2 7 3 1 0 3 6 2 2 0 2 7 1 3 0 3 7 2 2 3 0 6 3 0 3 0 6 1 0 3 2 7 1 1 2 3 6 1 1 1 3 7 1 2 0 4 7 2 1 1 3 6 4 0 0 2 6 2 2 2 0 6 2 2 0 2 6 0 1 2 3
output:
367385542 500913543 374339416
result:
ok 3 lines
Test #21:
score: 0
Accepted
time: 90ms
memory: 15520kb
input:
10 998244353 9 2 4 1 2 10 3 4 1 2 10 1 2 3 4 10 2 3 4 1 10 3 1 3 3 9 0 4 3 2 9 2 3 1 3 10 4 2 2 2 9 4 1 1 3 9 2 3 1 3 15 998244353 7 1 3 0 3 6 3 2 1 0 6 0 2 0 4 6 0 4 2 0 7 1 3 0 3 6 1 1 3 1 6 1 4 1 0 7 1 1 2 3 6 2 3 0 1 7 3 1 2 1 6 4 0 2 0 6 2 2 2 0 6 2 1 2 1 7 3 2 2 0 6 0 0 4 2
output:
998236023 124778179 124779131 124778655 374340011 998239355 998236261 374339535 998233643 998236261
result:
ok 10 lines
Test #22:
score: 0
Accepted
time: 127ms
memory: 15476kb
input:
15 998244353 7 3 2 2 0 7 2 2 3 0 6 1 3 1 1 7 1 1 2 3 6 1 3 1 1 7 3 3 0 1 6 2 1 2 1 6 1 2 1 2 6 1 1 1 3 6 4 1 0 1 7 3 2 2 0 6 1 3 1 1 6 0 2 2 2 7 1 3 0 3 7 2 2 1 2
output:
998191041 998191041 998061569 998069185 998061569 998160577 997939713 997939713 997574145 997939713 998191041 998061569 997574145 998145345 998145345
result:
ok 15 lines
Subtask #4:
score: 0
Time Limit Exceeded
Test #23:
score: 0
Time Limit Exceeded
input:
999 999999001 2 2 0 0 0 3 3 0 0 0 4 4 0 0 0 5 5 0 0 0 6 6 0 0 0 7 7 0 0 0 8 8 0 0 0 9 9 0 0 0 10 10 0 0 0 11 11 0 0 0 12 12 0 0 0 13 13 0 0 0 14 14 0 0 0 15 15 0 0 0 16 16 0 0 0 17 17 0 0 0 18 18 0 0 0 19 19 0 0 0 20 20 0 0 0 21 21 0 0 0 22 22 0 0 0 23 23 0 0 0 24 24 0 0 0 25 25 0 0 0 26 26 0 0 0 27...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
0%