QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#635655 | #9451. Expected Waiting Time | ucup-team3670# | WA | 997ms | 49408kb | C++17 | 3.1kb | 2024-10-12 20:30:43 | 2024-10-12 20:30:44 |
Judging History
answer
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
using namespace std;
const int N = int(2e7) + 15;
int n, p, b0, A, B;
bool read(){
if (!(cin >> n >> p >> b0 >> A >> B))
return false;
return true;
}
int add(int a, int b){
a += b;
if (a >= p) a -= p;
if (a < 0) a += p;
return a;
}
int mul(int a, int b){
return a * 1ll * b % p;
}
int binpow(int a, int b){
int res = 1;
while (b){
if (b & 1)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
vector<int> a;
int ans, cnt;
void brute(int i, int bal, int cur){
if (i == 2 * n){
if (bal == 0){
cnt = add(cnt, 1);
ans = add(ans, cur);
}
return;
}
brute(i + 1, bal + 1, add(cur, -a[i]));
if (bal > 0) brute(i + 1, bal - 1, add(cur, a[i]));
}
vector<int> fact, rfact;
void init(){
fact.resize(2 * n + 1);
fact[0] = 1;
fore(i, 1, 2 * n + 1)
fact[i] = mul(fact[i - 1], i);
rfact.resize(2 * n + 1);
rfact[2 * n] = binpow(fact[2 * n], p - 2);
for (int i = 2 * n - 1; i >= 0; --i)
rfact[i] = mul(rfact[i + 1], i + 1);
}
int cnk(int n, int k){
if (k < 0 || n < 0 || k > n) return 0;
return mul(fact[n], mul(rfact[k], rfact[n - k]));
}
int cat(int n){
if (n < 0) return 0;
return mul(fact[2 * n], mul(rfact[n], rfact[n + 1]));
}
void solve(){
init();
int b = b0;
a.assign(2 * n + 1, 0);
fore(i, 1, 2 * n + 1){
b = add(mul(A, b), B);
a[i] = add(a[i - 1], add(b, 1));
}
a.erase(a.begin());
vector<int> na;
//int ans = 0;
int sum = 0;
forn(i, n){
sum = add(sum, mul(cat(i), cat(n - 1 - i)));
na.push_back(sum);
na.push_back(sum);
}
na.pop_back();
reverse(na.begin(), na.end());
na.resize(n);
//for (int x : na) cerr << x << " ";
//cerr << endl;
forn(i, n){
int cl = add(cat(n), -na[i]);
na[i] = add(cl, -na[i]);
}
//for (int &x : na) x = add(0, -x);
forn(i, n) na.push_back(add(0, -na[n - 1 - i]));
//for (int x : na) cerr << x << " ";
//cerr << endl;
// int cl = add(cat(n), -op);
// ans = add(ans, )
//}
/*for (int i = 0; i < n; i += 2){
int sum = 0;//cat(n);
for (int len = 0; i + 2 + 2 * len <= 2 * n; ++len){
//sum = add(sum, -mul(cat(i / 2), mul(cat((2 * n - 2 * len - 2 - i) / 2), cat(len))));
//sum = add(sum, -mul(cat(len), cat(n - len - 1)));
sum = add(sum, mul(cat(i / 2), mul(cat((2 * n - 2 * len - 2 - i) / 2), cat(len))));
}
cerr << sum << " ";
}
cerr << endl;*/
/*vector<vector<int>> dp(2 * n + 1, vector<int>(2 * n + 1, 0));
dp[0][0] = 1;
forn(i, 2 * n){
forn(j, i + 1){
dp[i + 1][j + 1] = add(dp[i + 1][j + 1], dp[i][j]);
if (j > 0) dp[i + 1][j - 1] = add(dp[i + 1][j - 1], dp[i][j]);
}
}
forn(i, n * 2){
//cerr << add(cat(n), -dp[2 * n - i][i]) << " ";
cerr << dp[2 * n - i][i] << " ";
}
cerr << endl;*/
int ans = 0;
forn(i, 2 * n) ans = add(ans, mul(a[i], na[i]));
ans = mul(ans, binpow(cat(n), p - 2));
cout << ans << "\n";
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--){
read();
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3604kb
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: 893ms
memory: 3820kb
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: -100
Wrong Answer
time: 997ms
memory: 49408kb
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:
1628689726 1197334321 917675848 827110887 341753876 1070654806 521431574 1802075295 773724845 1371534043 143561165 950959997 754325596 -1514756658 1657330039 -231117104 1142225228 519739450 1114760032 404345174 593241326 1453367438 217539829 1477308847 558752169 1050819260 33991434 844160548 2283891...
result:
wrong answer 1st numbers differ - expected: '1532578211', found: '1628689726'