C. Tavas and Karafs
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <string>
7 #include <vector>
8 #include <set>
9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF = 0x4fffffff;
17 const double EXP = 1e-5;
18 const int MS = 40005;
19 const int SIZE = 100005;
20
21 int main()
22 {
23 LL A,B,n;
24 LL L,T,M;
25 cin>>A>>B>>n;
26 for(LL i=0;i<n;i++)
27 {
28 cin>>L>>T>>M;
29 LL minv=(L-1)*B+A;
30 if(T<minv)
31 {
32 cout<<"-1"<<endl;
33 continue;
34 }
35 LL R=(T-minv)/B;
36
37 int tl=L;
38 int tr=L+R+1;
39 while((tr-tl)>1) //二分查找用[a,b)使用方便,又不易出错。
40 {
41 LL mid=(tr+tl)>>1;
42 LL sum=(mid-L+1)*A+(mid+L-2)*(mid-L+1)/2*B;
43 LL maxv=(mid-1)*B+A;
44
45 LL time=sum/M;
46 if(sum%M!=0)
47 time++;
48 time=max(time,maxv);
49 if(time<=T)
50 tl=mid;
51 else
52 tr=mid;
53 }
54 cout<<tl<<endl;
55 }
56 return 0;
57 }