QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#308327 | #8017. 计算 | Xiaohuba | 30 | 3ms | 12584kb | C++23 | 9.2kb | 2024-01-19 22:09:28 | 2024-01-19 22:09:28 |
Judging History
answer
// clang-format off
#include <bits/stdc++.h>
using namespace std;
#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define INLINE_V
#define REGISTER_V register
#define CPP14CONSTEXPR
#define gcd __gcd
#define CPP14ENABLE_IF
#elif __cplusplus < 201700
#define INLINE_V
#define REGISTER_V
#define CPP14CONSTEXPR constexpr
#define gcd __gcd
#define CPP14ENABLE_IF ,enable_if_t<_is_integer<T>, int> = 0
#else
#define INLINE_V inline
#define REGISTER_V
#define CPP14CONSTEXPR constexpr
#define CPP14ENABLE_IF ,enable_if_t<_is_integer<T>, int> = 0
#endif
#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif
#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i,j,k) for(REGISTER_V decltype(k-j) i=(j);i<=(k);++i) // NOLINT
#define ForDown(i,j,k) for(REGISTER_V decltype(k-j) i=(j);i>=(k);--i) // NOLINT
#define pb push_back
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename) freopen(filename".in","r",stdin);freopen(filename".out","w",stdout)
#else
#define FileIO(filename)
#endif
using ll = long long;
// using lll = __int128_t;
using uint = unsigned int;
using ull = unsigned long long;
// using ulll = __uint128_t;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#if __cplusplus >= 201400
template <class T> INLINE_V constexpr bool _is_integer = numeric_limits<T>::is_integer;
// template <> INLINE_V constexpr bool _is_integer<__int128> = true;
// template <> INLINE_V constexpr bool _is_integer<__uint128_t> = true;
template <> INLINE_V constexpr bool _is_integer<bool> = false;
template <> INLINE_V constexpr bool _is_integer<char> = false;
template <class T CPP14ENABLE_IF>
INLINE_V constexpr T INF = numeric_limits<T>::max() >> 1;
#endif
template<typename T> constexpr il T sq(const T & x){return x*x;}
template<typename T> CPP14CONSTEXPR il void cmin(T & x, const T &y){x=min(x,y);}
template<typename T> CPP14CONSTEXPR il void cmax(T & x, const T &y){x=max(x,y);}
template<typename T> CPP14CONSTEXPR il T qpow(T x, ull y, T mod){T ans=1;x%=mod;while(y){if(y&1)(ans*=x)%=mod;(x*=x)%=mod;y>>=1;}return ans;}
template<typename T> CPP14CONSTEXPR il T qpow(T x, ull y){T ans=1;while(y){if(y&1)ans*=x;x*=x;y>>=1;}return ans;}
template<typename T CPP14ENABLE_IF> il void read(T &x){ x=0;int f=1;int c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}x*=f;}
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x);read(y...); }
// File head end
// clang-format on
namespace Modint {
template <const uint Mod> class Modint {
uint _val;
il static unordered_map<uint, uint> _inv;
public:
Modint() : _val(0) {}
constexpr Modint(ll val) : _val((val % Mod + Mod) % Mod) {}
[[nodiscard]] il uint value() const { return _val; }
[[nodiscard]] il uint safe_value() const { return (_val + Mod) % Mod; }
[[nodiscard]] il uint inv() const {
return _inv.count(_val) ? _inv[_val]
: _inv[_val] = qpow(*this, Mod - 2)._val;
}
Modint operator-() const { return (Mod - this->safe_value()) % Mod; }
void operator*=(const Modint &rhs) { _val = ((ull)_val * rhs._val) % Mod; }
void operator/=(const Modint &rhs) { _val = ((ull)_val * rhs.inv()) % Mod; }
void operator+=(const Modint &rhs) {
int(_val += rhs._val - (Mod << 1)) < 0 ? _val += (Mod << 1) : (ull){};
}
void operator-=(const Modint &rhs) {
int(_val -= rhs._val) < 0 ? _val += (Mod << 1) : (ull){};
}
#define setOper(x) \
[[nodiscard]] Modint operator x(const Modint &rhs) const { \
auto lhs = *this; \
lhs x## = rhs; \
return lhs; \
}
setOper(+) setOper(-) setOper(*) setOper(/)
#undef setOper
};
template <const ll Mod> istream &operator>>(istream &istr, Modint<Mod> &x) {
ll _temp;
istr >> _temp, x = _temp;
return istr;
}
template <const ll Mod> ostream &operator<<(ostream &ostr, Modint<Mod> x) {
ostr << x.value();
return ostr;
}
} // namespace Modint
using Z = Modint::Modint<998244353>;
constexpr Z operator""_Z(ull x);
class Poly : public vector<Z> {
il static constexpr Z G = 3;
il static constexpr uint MAXN = 1u << 21;
il static vector<uint> _Rev;
il static uint cur_len = 0;
il static bool gn_table_ok = 0;
il static Z GN[MAXN];
il static void init_rev(uint len) {
cur_len = len;
_Rev.resize(len);
For(i, 0, len - 1) {
_Rev[i] = _Rev[i >> 1] >> 1;
if (i & 1)
_Rev[i] |= len >> 1;
}
}
il static void init_gn_table() {
GN[21] = qpow(G, 998244352 / (1 << 21));
for (uint l = MAXN >> 1, i = 20; l >= 2; l >>= 1, i--) {
GN[i] = sq(GN[i + 1]);
}
gn_table_ok = 1;
}
il void NTT(uint len, bool tp, Poly &res) const {
res = *this;
res.resize(len);
if (len != cur_len)
init_rev(len);
if (!gn_table_ok)
init_gn_table();
for (uint i = 0; i < len; i++)
if (i < _Rev[i])
::swap(res[i], res[_Rev[i]]);
for (uint l = 2, lg = 1; l <= len; l <<= 1, lg++) {
uint m = l >> 1;
Z gn = GN[lg];
for (uint i = 0; i < len; i += l) {
Z g = 1;
for (uint j = 0; j < m; j++, g *= gn) {
Z tmp = res[i + j + m] * g;
res[i + j + m] = res[i + j] - tmp;
res[i + j] += tmp;
}
}
}
if (!tp) {
reverse(res.begin() + 1, res.begin() + len);
auto inv = Z(len).inv();
For(i, 0, len - 1) res[i] *= inv;
}
}
public:
Poly() = default;
Poly(int sz) { this->clear(), this->resize(sz); }
void operator+=(const Poly &rhs) {
int n_sz = max(this->size(), rhs.size()), rhs_sz = rhs.size(); // NOLINT
this->resize(n_sz);
For(i, 0, rhs_sz - 1) this->operator[](i) += rhs[i];
}
void operator-=(const Poly &rhs) {
int n_sz = max(this->size(), rhs.size()), rhs_sz = rhs.size(); // NOLINT
this->resize(n_sz);
For(i, 0, rhs_sz - 1) operator[](i) -= rhs[i];
}
void operator*=(const Poly &rhs) {
int N = 1, NN = this->size() + rhs.size() - 1; // NOLINT
while (N < NN)
N <<= 1;
Poly x, y, ans{N};
this->NTT(N, 1, x), rhs.NTT(N, 1, y);
For(i, 0, N - 1) ans[i] = x[i] * y[i];
ans.NTT(N, 0, *this);
resize(NN);
}
#define setOper(x) \
[[nodiscard]] Poly operator x(const Poly &rhs) const { \
auto lhs = *this; \
lhs x## = rhs; \
return lhs; \
}
setOper(+) setOper(-) setOper(*)
#undef setOper
};
namespace {
constexpr ll MAXN = 1e5 + 5, mod = 998244353;
int T, m, a, b, c, d;
ll L, R;
Poly _qpow(Poly x, ull y) {
Poly ans(1);
ans[0] = 1;
while (y) {
if (y & 1) {
ans *= x;
for (int i = m; i < ans.size(); i++)
ans[i % m] += ans[i];
ans.resize(m);
}
y >>= 1, x *= x;
for (int i = m; i < x.size(); i++)
x[i % m] += x[i];
x.resize(m);
}
// For(i, 1, y) ans *= x;
// for (int i = m; i < ans.size(); i++)
// ans[i % m] += ans[i];
ans.resize(m);
return ans;
}
il void solve() {
read(m, a, b, c, d);
L = (!a || !b) ? 1 : __gcd(qpow(1ll * m, a) - 1, qpow(1ll * m, b) - 1) + 2;
R = (!c || !d) ? 0 : __gcd(qpow(1ll * m, c) - 1, qpow(1ll * m, d) - 1) + 1;
// cerr << L << ' ' << R << '\n';
array<Poly, 2> f;
f[0].resize(m), f[1].resize(m);
if (L / m == R / m) {
f[1][0] = 1;
For(i, L, R) {
int cur = (i - L) & 1, pre = cur ^ 1;
f[cur] = f[pre];
For(j, 0, m - 1) f[cur][(j + i) % m] += f[pre][j];
}
cout << f[R - L & 1][0].safe_value() << '\n';
return;
}
f[1][0] = 1;
For(i, 0, m - 1) {
int cur = i & 1, pre = cur ^ 1;
f[cur] = f[pre];
For(j, 0, m - 1) f[cur][(j + i) % m] += f[pre][j];
if (L == 4 && R == 16) {
for (auto x : f[cur])
cerr << x.safe_value() << ' ';
cerr << '\n';
}
}
if (L == 4 && R == 16) {
auto g = _qpow(f[m - 1 & 1], 3);
for (auto x : g)
cerr << x.safe_value() << ' ';
cerr << '\n';
}
auto g = _qpow(f[m - 1 & 1], R / m - L / m - 1);
assert(g.size() == m);
f[1].swap(g);
ll id = (R / m) * m;
For(i, id, R) {
int cur = (i - id) & 1, pre = cur ^ 1;
f[cur] = f[pre];
For(j, 0, m - 1) { f[cur][(j + i) % m] += f[pre][j]; }
}
if (!(R - id & 1))
f[1].swap(f[0]);
id = (L / m + 1) * m - 1;
For(i, L, id) {
int cur = (i - L) & 1, pre = cur ^ 1;
f[cur] = f[pre];
For(j, 0, m - 1) f[cur][(j + i) % m] += f[pre][j];
}
cout << f[id - L & 1][0].safe_value() << '\n';
}
il void solver_main() {
read(T);
while (T--)
solve();
}
} // namespace
signed main() { return solver_main(), 0; }
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 5
Accepted
time: 0ms
memory: 11688kb
input:
1 2 0 0 1 1
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Time Limit Exceeded
input:
10000 9026580 948 269 622 88 9346507 381 242 826 606 9266080 948 589 28 666 8566088 808 523 402 338 9832014 278 123 146 1000 8000150 613 878 146 740 8296526 404 147 608 398 9062880 494 203 336 382 9375271 823 736 54 676 8465119 505 414 874 772 9535971 784 983 426 802 8258325 817 977 172 862 9656017 ...
output:
result:
Test #3:
score: 0
Time Limit Exceeded
input:
10000 9060194 192 71 24 178 8861291 135 338 24 794 8800613 173 760 704 362 9878509 367 418 58 964 8183946 856 951 406 316 8087229 458 105 770 342 8269502 205 147 614 334 8877019 851 143 206 10 9044859 710 949 210 584 8887395 571 856 550 428 8208494 383 628 74 646 9949585 697 450 794 738 9938335 224 ...
output:
result:
Test #4:
score: 0
Runtime Error
input:
10000 9110615 854 153 494 298 9386442 749 922 386 696 9270958 160 53 630 562 9643022 573 997 226 836 9824638 357 16 34 332 9962202 439 312 40 854 8974913 815 436 710 876 9362731 411 272 998 760 9814831 220 961 962 274 9310979 19 890 782 588 9425378 897 14 212 870 9572172 430 419 334 166 9101141 565 ...
output:
result:
Test #5:
score: 5
Accepted
time: 0ms
memory: 11696kb
input:
4 3 4 5 6 1 2 6 1 8 10 4 1 4 2 2 5 0 10 2 10
output:
1 2 1024 6710912
result:
ok 4 number(s): "1 2 1024 6710912"
Test #6:
score: 5
Accepted
time: 2ms
memory: 11776kb
input:
5 18 10 9 4 6 14 7 4 4 6 18 6 5 8 2 12 5 2 4 10 11 5 6 2 6
output:
8581218 487933114 8581218 919504178 296665006
result:
ok 5 number(s): "8581218 487933114 8581218 919504178 296665006"
Test #7:
score: 5
Accepted
time: 2ms
memory: 11760kb
input:
5 11 9 5 3 3 12 8 9 9 6 10 10 7 3 9 10 5 4 3 6 6 9 6 4 8
output:
336307822 997249957 706112470 706112470 856358531
result:
ok 5 number(s): "336307822 997249957 706112470 706112470 856358531"
Test #8:
score: 5
Accepted
time: 0ms
memory: 11716kb
input:
5 4 5 6 5 10 12 8 5 9 3 10 4 9 9 3 10 5 6 6 3 6 5 7 4 8
output:
336848941 997249957 706112470 706112470 811628181
result:
ok 5 number(s): "336848941 997249957 706112470 706112470 811628181"
Test #9:
score: 0
Wrong Answer
time: 2ms
memory: 11760kb
input:
5 11 80 29 27 84 11 31 27 87 6 4 46 54 95 80 6 45 62 88 92 12 70 34 3 75
output:
1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '336307822', found: '1'
Test #10:
score: 0
Runtime Error
input:
5 58 91 37 75 5 62 99 67 10 75 55 51 92 70 85 55 86 16 50 85 29 54 95 36 78
output:
result:
Test #11:
score: 0
Wrong Answer
time: 2ms
memory: 11968kb
input:
5 10 57 94 99 18 19 97 77 63 98 62 44 4 90 35 61 3 54 25 60 62 57 65 95 70
output:
1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '329246322', found: '1'
Test #12:
score: 5
Accepted
time: 0ms
memory: 11760kb
input:
5 5 0 0 9 5 4 0 0 1 5 2 0 0 7 5 3 0 0 3 7 3 0 0 9 5
output:
8 4 2 4 4
result:
ok 5 number(s): "8 4 2 4 4"
Test #13:
score: 0
Runtime Error
input:
5 1931 633 387 65 265 1334 942 887 115 455 1547 511 57 65 920 1815 983 313 575 430 1497 749 773 25 740
output:
result:
Test #14:
score: 0
Wrong Answer
time: 2ms
memory: 11660kb
input:
5 1676 347 892 10 545 1552 901 243 595 705 1585 181 32 900 215 1043 147 153 250 195 1343 519 863 610 345
output:
1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '733422553', found: '1'
Test #15:
score: 0
Wrong Answer
time: 2ms
memory: 11784kb
input:
5 1507 98 480 995 565 1130 476 464 710 925 1556 518 459 65 445 1153 277 530 485 860 1329 191 777 315 905
output:
1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '919146531', found: '1'
Test #16:
score: 0
Wrong Answer
time: 2ms
memory: 11708kb
input:
5 1260 985 161 855 395 1919 286 557 10 155 1439 586 749 915 730 1297 750 832 245 855 1693 134 289 355 200
output:
1 1 2 1 1
result:
wrong answer 1st numbers differ - expected: '396457042', found: '1'
Test #17:
score: 0
Wrong Answer
time: 0ms
memory: 12312kb
input:
5 95803 976 729 633 237 99669 606 7 489 816 80026 339 914 753 933 97238 844 430 666 537 70169 394 204 411 738
output:
1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '263714877', found: '1'
Test #18:
score: 0
Runtime Error
input:
5 86711 965 851 300 273 97658 466 416 633 603 89583 407 591 411 3 84126 987 514 822 837 79996 9 166 807 15
output:
result:
Test #19:
score: 0
Wrong Answer
time: 0ms
memory: 12080kb
input:
5 90325 2 48 195 528 83446 123 809 897 60 92466 526 768 441 447 95582 319 251 843 447 95424 374 218 162 597
output:
1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '962318822', found: '1'
Test #20:
score: 0
Wrong Answer
time: 3ms
memory: 12584kb
input:
5 80746 952 565 615 192 71949 878 950 357 789 99398 301 646 942 699 94853 670 947 141 276 82409 554 873 375 213
output:
1 1 1 1 1
result:
wrong answer 1st numbers differ - expected: '896071730', found: '1'