Links: https://ac.nowcoder.com/acm/problem/21860
Source: Cattle-off network
Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 32768K, other languages 65536k
64bit the IO the Format: LLD%
space restrictions: C / C ++ 32768K, other languages 65536k
64bit the IO the Format: LLD%
Title Description
J small start working, trying to earn money to buy a gas stove.
The first day, a small wage of n j yuan, a day after his salary more than the day before d yuan.
Known gas stove needs m yuan, seeking small j for at least a few days to buy gas stove.
The first day, a small wage of n j yuan, a day after his salary more than the day before d yuan.
Known gas stove needs m yuan, seeking small j for at least a few days to buy gas stove.
Enter a description:
Four integers n, m, d, x
are respectively the first day of the small pay j, Gas prices, increase the amount of salary per day, the answer is no more than x
Output Description:
A number indicates the answer
Remarks:
0 ≦ n &, d≤1e9, the n-+ d> 0
1≤m≤1e18
1≤x≤1e9
meaning of the questions: can get paid every day, at the beginning is n, every day may be the day before the overpaid d (note d may be zero), and asked how many days the minimum sum you receive salary can exceed m yuan to buy gas stove need, no more than x number of days
thinking: summation problem, and more are likely to increase the value of d day, so this is a beginning is n, the number of arithmetic d tolerance column summing arithmetic sequence summation formula: Sn = a0 * n + ( n * (n-1) * d / 2), m and the maximum is noted 1e18, to let arithmetic sequence sum Sn> = m, the Sn would exceed the range of a long long, so to formula deformation,
n is an initial value, in the number of days, d is the tolerance, m is at least the sum of the required value larger than, get
n*in+(in*(in-1)*d)/2>=m
2*(n*in)+(in(in-1)*d)>=2*m
in*(in-1)*d>=2*(m-(n*in))
While noting that 1 <= in <= x, so I think half of what the answer
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll n,m,d,x; 5 bool check(ll in){ 6 return in*(in-1)*d>=2*(m-(n*in)); 7 } 8 int main(){ 9 cin>>n>>m>>d>>x; 10 ll l=1,r=x,mid=(l+r)>>1; 11 while(l<r){ 12 if(check(mid))r=mid; 13 else l=mid+1; 14 mid=(l+r)>>1; 15 } 16 printf("%lld\n",mid); 17 } 18 /*** 19 n*in+(in*(in-1)*d)/2>=m 20 2*(n*in)+(in(in-1)*d)>=2*m 21 in*(in-1)*d>=2*(m-(n*in)) 22 ***/