QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#506747 | #6168. 异构序列码性态问题 | hellolin | 0 | 0ms | 0kb | C++23 | 5.0kb | 2024-08-05 21:09:22 | 2024-08-05 21:09:23 |
answer
// Hellolin v4.12.1
#include <bits/stdc++.h>
using namespace std;
#ifdef HELLOLIN_OPT
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#endif
namespace hellolin {
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
#ifdef __SIZEOF_INT128__
using i128 = __int128_t;
using u128 = __uint128_t;
#endif
template <class T1, class T2>
bool checkMax(T1 &x, T2 y) { return x < y ? x = y, true : false; }
template <class T1, class T2>
bool checkMin(T1 &x, T2 y) { return x > y ? x = y, true : false; }
template <class T>
struct Vec : vector<T> {
using vector<T>::vector;
using Iter = typename vector<T>::iterator;
using ConstIter = typename vector<T>::const_iterator;
Vec()
: vector<T>() {}
Vec(const vector<T> &x)
: vector<T>(x) {}
void concat(const Vec &x) { this->insert(this->end(), x.begin(), x.end()); }
void sort() { std::sort(this->begin(), this->end()); }
template <class F>
void sort(F f) { std::sort(this->begin(), this->end(), f); }
void reverse() { std::reverse(this->begin(), this->end()); }
T &operator[](size_t x) { return this->at(x); }
const T &operator[](size_t x) const { return this->at(x); }
Iter lbound(const T &x) { return lower_bound(this->begin(), this->end(), x); }
Iter ubound(const T &x) { return upper_bound(this->begin(), this->end(), x); }
ConstIter lbound(const T &x) const { return lower_bound(this->begin(), this->end(), x); }
ConstIter ubound(const T &x) const { return upper_bound(this->begin(), this->end(), x); }
bool bsearch(const T &x) const { return binary_search(this->begin(), this->end(), x); }
Vec slice(size_t l, size_t r) const {
if (l > r) {
return {};
}
return Vec(this->begin(), r < this->size() ? this->begin() + r : this->end());
}
template <class P>
Vec<P> map(function<P(T)> f) const {
Vec<P> res(this->size());
for (size_t i = 0; i < this->size(); ++i) {
res[i] = f(this->at(i));
}
return res;
}
};
class IO {
static constexpr int Buffer = 1 << 19;
char ibuf[Buffer], *ist, *ied;
char obuf[Buffer], *ost, *oed;
FILE *ifile = stdin, *ofile = stdout;
public:
IO() {
ist = ied = ibuf;
ost = obuf, oed = obuf + Buffer - 1;
}
~IO() { flush(); }
void reopen(const char *a, const char *b) {
flush();
ifile = fopen(a, "r");
ofile = fopen(b, "w");
}
char getChar() {
if (ist == ied) {
ied = (ist = ibuf) + fread(ibuf, 1, Buffer, ifile);
if (ist == ied) {
return EOF;
}
}
return *ist++;
}
void putChar(char x) {
*ost++ = x;
if (ost == oed) {
flush();
}
}
void flush() {
fwrite(obuf, 1, ost - obuf, ofile);
ost = obuf;
fflush(ofile);
}
void read(char &x) {
do {
x = getChar();
} while (!isgraph(x));
}
void read(string &x) {
x = "";
char c = getChar();
for (; !isgraph(c); c = getChar())
;
for (; isgraph(c); c = getChar()) {
x += c;
}
}
template <class T>
void read(T &x) {
x = 0;
char c = getChar();
bool neg = false;
for (; !isdigit(c); c = getChar()) {
neg ^= c == '-';
}
for (; isdigit(c); c = getChar()) {
x = x * 10 + c - 48;
}
x = neg ? -x : x;
}
template <class T, class... A>
void read(T &x, A &...a) { read(x), read(a...); }
void write() {}
void write(char x) { putChar(x); }
void write(const string &x) {
for (const char &i : x) {
putChar(i);
}
}
template <class T>
void write(T x) {
write(to_string(x));
}
template <class T, class... A>
void write(T x, A... a) { write(x), write(a...); }
template <class... A>
void writeln(A... a) { write(a...), putChar('\n'); }
} io;
void main();
} // namespace hellolin
int main() {
#ifndef HELLOLIN_STDIO
// hellolin::io.reopen("sort.in", "sort.out");
#endif
hellolin::main();
}
namespace hellolin {
void main() {
for (; ;) {
int n = -1, m = -1;
io.read(n, m);
if(n == -1 && m == -1) {
return;
}
if(n == 1) {
io.writeln(0);
continue;
}
Vec<i64> fac(n + 1), ifc(n + 1);
fac[0] = ifc[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = fac[i - 1] * i % m;
}
auto pow = [&](i64 x, int y) {
i64 res = 1;
for (; y; y >>= 1, (x *= x) %= m) {
if (y & 1) {
(res *= x) %= m;
}
}
return res;
};
ifc[n] = pow(fac[n], m - 2);
for (int i = n; i >= 2; --i) {
ifc[i - 1] = ifc[i] * i % m;
}
auto comb = [&](int x, int y) -> i64 {
if(y < 0 || x < y) return 0;
return fac[x] * ifc[y] % m * ifc[x - y] % m;
};
i64 ans = 0, pre = 1;
for (int i = 0; i < n; ++i) {
ans += pre * comb(n - 1, n - i - 1) % m * comb(n - 1, n - i) % m;
ans %= m;
(pre <<= 1) %= m;
}
ans = (fac[n] - pow(n - 1, m - 2) * ans % m) % m + m;
io.writeln(ans % m);
}
}
} // namespace hellolin
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 0
Time Limit Exceeded
input:
1329 281979259 3946 347158051 3102 613279651 4193 175275013 494 674092373 549 240448331 3924 588857561 1719 782159639 1781 270936499 672 589936439
output:
result:
Test #2:
score: 0
Time Limit Exceeded
input:
2013 239960621 2839 377547413 4864 996482101 3196 204395311 1568 653611141 753 104807119 3006 755688737 3047 697114861 2943 384316589 2413 512454407
output:
result:
Test #3:
score: 0
Time Limit Exceeded
input:
4542 497374921 4375 144420547 3310 175379389 1159 776452463 642 401409289 1214 754051891 1956 962928761 3925 282058753 2664 215149997 2050 912416861
output:
result:
Test #4:
score: 0
Time Limit Exceeded
input:
4808 349909687 4602 991037059 108 14516431 3384 974151743 1250 726146471 4891 585626857 3617 211451333 358 425997931 2891 654329051 4814 64701121
output:
result:
Test #5:
score: 0
Time Limit Exceeded
input:
1107 524640323 1847 933584471 3406 582135349 4007 51397727 80 570372863 4186 193850947 1773 505708747 59 753490921 4244 207441401 723 3432577
output:
result:
Test #6:
score: 0
Time Limit Exceeded
input:
350598 149719567 5656 639718861 182032 562484129 321578 733607449 396666 977259137 289217 738297893 94787 357806747 130763 912856261 129615 332700631 439091 653900939 228198 805737671 421386 35411809
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
71980 730430429 365591 519176123 421874 939796883 442008 891121537 196686 57507407 481211 220812971 351047 410650543 262764 313392899 434798 192402149 113530 421210637 359403 379486343 396967 813975307 193616 995122811
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
412065 473565137 427460 278321429 481215 341626709 248825 235725317 391371 550178417 166694 541821383 3035 184388593 166130 370451243 342592 79808159 98863 640391243 416610 96090131 479715 456938333 407065 630349243 289928 807773149 486842 415008521 300760 618573047
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
401210 376894061 112020 976190981 76624 418721741 308217 29130763 275157 939661747 35829 250363831 178538 424627327 456973 351281803 88075 992733541 281595 311233067 104622 151994753 271593 41245261 319132 947462657 186811 961154149 227249 86441119 165977 701025443 471204 999330173 296567 685415303 ...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
372965 157601299 239698 272457617 40399 596646311 315339 562161443 166279 292784099 224605 629707601 14534 7015139 478528 718703087 129027 957763397 44039 880925107 420271 933815867 323410 164444393 160117 572839451 344095 28487981 473223 648555881 469070 150428897 447149 993828707 214989 982422577 ...