|
| 1 | +#include<bits/stdc++.h> |
| 2 | +#define ms(x,v) memset((x),v,sizeof(x)) |
| 3 | +#define INF 0x3f3f3f3f |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +const int MAXN = 100+10; |
| 7 | +typedef long long LL; |
| 8 | + |
| 9 | +bool ok(LL x,int k,const vector<int>&L,const vector<int>&R,const vector<LL>&preL,const vector<LL>& preR){ |
| 10 | + // cout << "\nok " << x << " " << k << "\n"; |
| 11 | + auto iR = lower_bound(R.begin(),R.end(),x,greater_equal<int>()) - R.begin(); |
| 12 | + auto iL = lower_bound(L.begin(),L.end(),x,greater_equal<int>()) - L.begin(); |
| 13 | + // cout << iR << " " << iL << "\n"; |
| 14 | + LL rank = preR[iR] - iR *x+iR; |
| 15 | + rank -= preL[iL] - iL * x; |
| 16 | + // cout << x << "rank " <<rank << "\n"; |
| 17 | + return rank <k; |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +LL solve(vector<int >& L,vector<int>& R,const vector<int>&Q){ |
| 22 | + LL ret =0; |
| 23 | + vector<LL> preSumR(L.size()+1),preSumL(L.size()+1); |
| 24 | + sort(L.begin(),L.end(),[](int a,int b){return a>b;}); |
| 25 | + sort(R.begin(),R.end(),[](int a,int b){return a > b;}); |
| 26 | + for(int i=0 ;i<L.size() ; ++i) |
| 27 | + { |
| 28 | + preSumL[i+1] = preSumL[i]+L[i]; |
| 29 | + preSumR[i+1] = preSumR[i] + R[i]; |
| 30 | + } |
| 31 | + |
| 32 | + for(int i=0 ; i<Q.size(); ++i){ |
| 33 | + auto q = Q[i]; |
| 34 | + int l =0,r = R[0]+1; |
| 35 | + while (l <r) |
| 36 | + { |
| 37 | + int mid = (l +r) >> 1; |
| 38 | + if(ok(mid,q,L,R,preSumL,preSumR)) r = mid -1; |
| 39 | + else l = mid; |
| 40 | + if(l + 1 >=r){ |
| 41 | + if(!ok(r,q,L,R,preSumL,preSumR))l = r; |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + LL ans = l; |
| 46 | + if(ans <0 || ans > R[0])ans =0; |
| 47 | + ret += ans * (i+1); |
| 48 | + } |
| 49 | + return ret; |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +void solve(){ |
| 54 | + int N,Q; |
| 55 | + cin >> N >> Q; |
| 56 | + |
| 57 | + LL x1,x2,a1,b1,c1,m1,y1,y2,a2,b2,c2,m2,z1,z2,a3,b3,c3,m3; |
| 58 | + cin >> x1 >> x2 >> a1 >> b1 >> c1 >> m1; |
| 59 | + cin >> y1 >> y2 >> a2 >> b2 >> c2 >> m2; |
| 60 | + cin >> z1 >> z2 >> a3 >> b3 >> c3 >> m3; |
| 61 | + vector<int> L(N),R(N),K(Q); |
| 62 | + L[0] = min(x1,y1) + 1,L[1] = min(x2,y2) + 1,K[0] = z1 + 1; |
| 63 | + R[0] = max(x1,y1) + 1,R[1] = max(x2,y2) + 1,K[1] = z2 + 1; |
| 64 | + for(int i=2 ; i<N ; ++i){ |
| 65 | + |
| 66 | + LL x3 = (a1 * x2 +b1*x1 + c1) % m1; |
| 67 | + LL y3 = (a2 * y2 + b2 * y1 + c2) % m2; |
| 68 | + x1 = x2,x2 = x3; |
| 69 | + y1 = y2, y2 = y3; |
| 70 | + if(x3 > y3)swap(x3,y3); |
| 71 | + L[i] = x3 + 1; |
| 72 | + R[i] = y3 + 1; |
| 73 | + } |
| 74 | + for(int i=2 ; i< Q; ++i){ |
| 75 | + |
| 76 | + LL z3 = (a3 * z2 + b3 * z1 + c3) % m3; |
| 77 | + K[i] = z3 +1; |
| 78 | + z1 = z2,z2 = z3; |
| 79 | + } |
| 80 | + cout << solve(L,R,K) << "\n"; |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +int main(){ |
| 87 | + ios :: sync_with_stdio(0); |
| 88 | + cin.tie(0); |
| 89 | + // cout.tie(0); |
| 90 | + std::cout.precision(10); |
| 91 | + std::cout.setf( std::ios::fixed, std:: ios::floatfield ); |
| 92 | + |
| 93 | + int T; |
| 94 | + cin >> T; |
| 95 | + for(int tt =1 ; tt <=T ; ++tt) |
| 96 | + { |
| 97 | + cout << "Case #" << tt << ": "; |
| 98 | + solve(); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
0 commit comments