Codechef September long challenge 2021 | Covaxin vs Covishield Solution
There are two types of vaccines available: Covaxin and Covishield.
A black marketeer has XX coins and wants to buy as many vaccines as possible. Due to the black marketing concerns, government has enforced the following policy:
ithith dose of Covaxin costs a+(i−1)⋅ba+(i−1)⋅b coins for every i≥1i≥1.
ithith dose of Covishield costs c+(i−1)⋅dc+(i−1)⋅d coins for every i≥1i≥1.
The values of the four parameters a,b,ca,b,c and dd, however isn’t constant and might vary from query to query. In general the value of these four parameters for ithith query will be Ai,Bi,CiAi,Bi,Ci and DiDi respectively. Let ansiansi be the maximum total quantity of vaccines the black marketeer can buy corresponding to the ithith query. For each query, you have to find the value of ansiansi.
You will be given integers A1,B1,C1,D1,P,Q,R,S,TA1,B1,C1,D1,P,Q,R,S,T and MM which will define the queries to be followed.
For i≥1i≥1 and i≤I−1i≤I−1:
- Ai+1=(Ai+ansi⋅T)modM+PAi+1=(Ai+ansi⋅T)modM+P
- Bi+1=(Bi+ansi⋅T)modM+QBi+1=(Bi+ansi⋅T)modM+Q
- Ci+1=(Ci+ansi⋅T)modM+RCi+1=(Ci+ansi⋅T)modM+R
- Di+1=(Di+ansi⋅T)modM+SDi+1=(Di+ansi⋅T)modM+S .
Note: Since the output is large, prefer using fast input-output methods.
Input Format
- First line contains of input contains an integer II denoting the number of queries.
- Second line of input contains five integers X,A1,B1,C1,D1X,A1,B1,C1,D1.
- Third line of input contains six integers P,Q,R,S,T,MP,Q,R,S,T,M.
Output Format
For each query output the maximum quantity of vaccines the black marketeer can buy.
Constraints
- 1≤I≤5⋅1051≤I≤5⋅105
- 1≤X,A1,B1,C1,D1≤10181≤X,A1,B1,C1,D1≤1018
- 1≤P,Q,R,S,M≤10181≤P,Q,R,S,M≤1018
- 1≤T≤1091≤T≤109
Subtasks
Subtask #1 (10 points):
- 1≤I≤1031≤I≤103
- 1≤X≤1091≤X≤109
- Time limit: 11 sec
Subtask #2 (30 points):
- 1≤I≤1031≤I≤103
- 1≤X≤10151≤X≤1015
- 109≤A1≤1018109≤A1≤1018
- 109≤B1≤1018109≤B1≤1018
- 109≤P≤1018109≤P≤1018
- 109≤Q≤1018109≤Q≤1018
- Time limit: 11 sec
Subtask #3 (60 points):
- Original constraints
- Time limit: 33 sec
Sample Input 1
3
20 2 3 4 1
3 7 8 4 11 20
Sample Output 1
4
1
2