QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#636526 | #9451. Expected Waiting Time | ucup-team008# | AC ✓ | 905ms | 46176kb | C++17 | 8.4kb | 2024-10-13 00:19:07 | 2024-10-13 00:19:08 |
Judging History
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(0) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
template<class T>
bool updmin(T& a, T b) {
if(b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool updmax(T& a, T b) {
if(b > a) {
a = b;
return true;
}
return false;
}
typedef int64_t ll;
typedef long double ld;
struct barrett_reduction {
unsigned mod;
uint64_t div;
barrett_reduction(unsigned m) : mod(m), div(-1LLU / m) {}
unsigned operator()(uint64_t a) const {
#ifdef __SIZEOF_INT128__
uint64_t q = uint64_t(__uint128_t(div) * a >> 64);
uint64_t r = a - q * mod;
return unsigned(r < mod ? r : r - mod);
#endif
return unsigned(a % mod);
}
};
template<const int &MOD, const barrett_reduction &barrett>
struct _b_int {
int val;
_b_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_b_int(uint64_t v) {
if (v >= uint64_t(MOD)) v %= MOD;
val = int(v);
}
_b_int(int v) : _b_int(int64_t(v)) {}
_b_int(unsigned v) : _b_int(uint64_t(v)) {}
static int inv_mod(int a, int m = MOD) {
// https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r; swap(g, r);
x -= q * y; swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_b_int& operator+=(const _b_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_b_int& operator-=(const _b_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x) {
#if !defined(_WIN32) || defined(_WIN64)
return barrett(x);
#endif
// Optimized mod for Codeforces 32-bit machines.
// x must be less than 2^32 * MOD for this to work, so that x / MOD fits in an unsigned 32-bit int.
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n"
: "=a" (quot), "=d" (rem)
: "d" (x_high), "a" (x_low), "r" (MOD));
return rem;
}
_b_int& operator*=(const _b_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_b_int& operator/=(const _b_int &other) {
return *this *= other.inv();
}
friend _b_int operator+(const _b_int &a, const _b_int &b) { return _b_int(a) += b; }
friend _b_int operator-(const _b_int &a, const _b_int &b) { return _b_int(a) -= b; }
friend _b_int operator*(const _b_int &a, const _b_int &b) { return _b_int(a) *= b; }
friend _b_int operator/(const _b_int &a, const _b_int &b) { return _b_int(a) /= b; }
_b_int& operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_b_int& operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_b_int operator++(int) { _b_int before = *this; ++*this; return before; }
_b_int operator--(int) { _b_int before = *this; --*this; return before; }
_b_int operator-() const {
return val == 0 ? 0 : MOD - val;
}
friend bool operator==(const _b_int &a, const _b_int &b) { return a.val == b.val; }
friend bool operator!=(const _b_int &a, const _b_int &b) { return a.val != b.val; }
friend bool operator<(const _b_int &a, const _b_int &b) { return a.val < b.val; }
friend bool operator>(const _b_int &a, const _b_int &b) { return a.val > b.val; }
friend bool operator<=(const _b_int &a, const _b_int &b) { return a.val <= b.val; }
friend bool operator>=(const _b_int &a, const _b_int &b) { return a.val >= b.val; }
_b_int inv() const {
return inv_mod(val);
}
_b_int pow(int64_t p) const {
if (p < 0)
return inv().pow(-p);
_b_int a = *this, result = 1;
while (p > 0) {
if (p & 1)
result *= a;
p >>= 1;
if (p > 0)
a *= a;
}
return result;
}
friend ostream& operator<<(ostream &os, const _b_int &m) {
return os << m.val;
}
friend istream& operator>>(istream &is, _b_int &m) {
int64_t x;
is >> x;
m = x;
return is;
}
};
int MOD = int(1e9) + 7;
barrett_reduction barrett(MOD);
using mnum = _b_int<MOD, barrett>;
void rsolve() {
int n;
cin >> n >> MOD;
vector<ll> av(2*n+1);
vector<ll> bv(2*n+1);
ll A, B;
cin >> bv[0] >> A >> B;
barrett = barrett_reduction(MOD);
for(int i = 1; i <= 2*n; i++) {
bv[i] = (A*bv[i-1] + B) % MOD;
}
for(int i = 1; i <= 2*n; i++) av[i] = av[i-1] + bv[i] + 1;
vector<mnum> catalan(n+1);
catalan[0] = 1;
catalan[1] = 1;
for(int i = 2; i <= n; i++) {
catalan[i] = (catalan[i-1] * (4*i-2)) / (i+1);
}
vector<mnum> dp(2*n+1);
for(int i = 2; i <= 2*n; i++) {
int p = (i/2)-1;
dp[i] = dp[i-2]+catalan[p]*catalan[n-p-1];
}
mnum ret = 0;
for(int i = 1; i <= 2*n; i++) {
if(i > 1) ret += av[i] * dp[i];
if(i < 2*n) ret -= av[i] * dp[2*n-i+1];
}
ret /= catalan[n];
cout << ret << "\n";
}
void solve() {
int t;
cin >> t;
while(t--) rsolve();
}
// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
这程序好像有点Bug,我给组数据试试?
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
5 1 1000000007 0 1 0 2 1000000007 0 1 1 2 7 5 2 3 3 31 15 6 24 20 1000000007 0 1 0
output:
1 12 1 21 879705565
result:
ok 5 number(s): "1 12 1 21 879705565"
Test #2:
score: 0
Accepted
time: 532ms
memory: 3760kb
input:
4400 3954 1000000007 0 1 0 1306 1000000007 0 1 0 3774 1000000007 0 1 0 3345 1000000007 0 1 0 891 1000000007 0 1 0 2462 1000000007 0 1 0 237 1000000007 0 1 0 26 1000000007 0 1 0 2510 1000000007 0 1 0 637 1000000007 0 1 0 3250 1000000007 0 1 0 3447 1000000007 0 1 0 1222 1000000007 0 1 0 133 1000000007...
output:
440618338 378292891 979368645 915766295 343598158 80867595 161627927 517387931 396936703 42785642 945720545 764273281 186237656 635777911 164064906 548455037 991964184 468137124 561243246 118562285 856945294 642467240 23673926 808943705 897417615 462422554 656411244 204288121 997894281 244685651 762...
result:
ok 4400 numbers
Test #3:
score: 0
Accepted
time: 905ms
memory: 46176kb
input:
1019 338 1863833207 1820742817 1507924477 1822273457 770 1386304741 1088481071 1216187083 170973217 597 1604266739 620750027 196415899 456280997 105 1008587891 184044403 24836083 926135599 357 1165127407 440925347 1103369747 912263123 82 1639766993 263045351 631010551 1412721139 928 1715915153 25383...
output:
1532578211 839037587 1047387343 827110887 825754860 1399761197 267796211 1563605211 1628148612 510782452 1009499206 977929696 466163317 246777775 1781337180 700999207 522771237 42312781 172374583 319038379 563256698 1400403161 22552986 1408276343 558752169 1050819260 174447415 844160548 1382940913 1...
result:
ok 1019 numbers
Test #4:
score: 0
Accepted
time: 806ms
memory: 7644kb
input:
217 31752 1623636743 381890923 885513569 842557939 44560 1671300349 1133398261 1076377361 138151151 98395 1887613031 1552853849 1167776639 1748368931 38388 1221893927 524645339 598717199 864504559 46484 1161165839 833729009 348202331 407607601 14134 1500136753 247946861 1029519499 420912461 42361 12...
output:
921943129 1651287678 1204818336 685557670 348324702 1348834532 684106754 1802861733 294146103 1181847835 393402724 771264676 1357541910 336262290 1519052686 965265375 164416232 536332209 664177339 279762508 172270575 296113856 676553568 56580590 1662307723 551032870 849878353 899756098 1043540760 65...
result:
ok 217 numbers
Test #5:
score: 0
Accepted
time: 836ms
memory: 7420kb
input:
209 29771 1072350767 215063557 929759989 884416571 55201 1330151437 375869047 1089916759 1006803043 44446 1255569503 974485139 1100573447 468049237 38112 1088575729 690554509 139043089 318478729 59665 1197676111 958924997 1062562733 504417349 26297 1267535141 800679281 972314209 417253079 19848 1470...
output:
1019773842 530587777 960231793 799694163 777561611 5502176 357632543 758954057 966358573 1023410809 949841520 1495179331 1580320049 173875471 2220116 469298866 1337750009 369625733 747522220 143937247 1286370836 135996013 210044979 1583248565 657653951 1035620126 160616212 193166047 147168296 194451...
result:
ok 209 numbers
Test #6:
score: 0
Accepted
time: 819ms
memory: 7448kb
input:
191 4581 1215490597 542057431 695641117 341312327 76198 1830024701 1063458349 588883499 1260572737 76694 1445111947 1069466003 941483509 268919069 92431 1384691513 15731591 390776461 943271249 63234 1097808793 204272807 857954509 763222181 26427 1746295877 743699191 1671886939 1655403307 91012 19997...
output:
660812054 1707568418 155249600 645552623 671226426 50129037 971925203 1638809868 463571080 143058581 403506184 767222482 666935871 1092771100 416048275 1536775747 475955650 73199758 578511368 51576569 1380151783 515363742 1021989523 892069331 1088408017 1337534689 715624643 1241212077 730954505 1286...
result:
ok 191 numbers
Extra Test:
score: 0
Extra Test Passed