题目:
Farmer John owes Bessie N gallons of milk (1≤N≤1012). He has to give her the milk within K days. However, he doesn’t want to give the milk away too quickly. On the other hand, he has to make forward progress on the loan, so he must give Bessie at least M gallons of milk each day (1≤M≤1012).
Here is how Farmer John decides to pay back Bessie. He first picks a positive integer X. He then repeats the following procedure every day:
Assuming that Farmer John has already given Bessie G gallons, compute N−GX rounded down. Call this number Y. If Y is less than M, set Y to M. Give Bessie Y gallons of milk. Determine the largest X such that if Farmer John follows the above procedure, Farmer John gives Bessie at least N gallons of milk after K days (1≤K≤1012).
Input
The only line of input contains three space-separated positive integers N, K, and M satisfying K⋅M<N.
Output
Output the largest positive integer X such that Farmer John will give Bessie at least N gallons using the above procedure.
Example
inputCopy
10 3 3
outputCopy
2
Note
For the first test case, when X=2 Farmer John gives Bessie 5 gallons on the first day and M=3 gallons on each of the next two days.
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a “long long” in C/C++).
这个题目看起来就是模拟,但是很多人可能难以处理的点就是其中一个问题,模拟每天给同样数目的牛奶的时候,如何删去这些重复的步骤。
我们看一下这个东西:
bool check(LL num)
{
/*LL G=0;
LL day=0;
while(1)
{
if((n-G)%m==0&&(n-G)/m+day<=k)return true;
else if((n-G)%m!=0&&(n-G)/m+day+1<=k)return true;
if((n-G)/num<=m)return false;
G+=(n-G)/num;
day++;
}*/
LL kk=k,G=0;
while(G<n&&kk>0)
{
LL y=(n-G)/num;
if(y<m)
{
LL o=(n-G+m-1)/m;
return o<=kk;
}
LL ch=n-num*y;
LL d=(ch-G)/y+1;
d=min(d,kk);
G+=y*d;
kk-=d;
}
return G>=n;
}
我们看看如何跳过了那段时间:注意看这一步:
LL ch=n-num*y;
LL d=(ch-G)/y+1;
d=min(d,kk);
G+=y*d;
kk-=d
这就是利用整数除法把所有相同的牛奶给省出去了,这样就是省下来一大堆时间,然后就能过了:
完整代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <queue>
#include <stack>
#include <map>
//鬼畜头文件
using namespace std;
const int INF = 0x3f3f3f3f;
//1.06e9大小
const int mod = 1e9+7;
typedef unsigned long long ULL;
typedef long long LL;
//鬼畜define
// typedef struct note{};
LL n,k,m;
bool check(LL num)
{
/*LL G=0;
LL day=0;
while(1)
{
if((n-G)%m==0&&(n-G)/m+day<=k)return true;
else if((n-G)%m!=0&&(n-G)/m+day+1<=k)return true;
if((n-G)/num<=m)return false;
G+=(n-G)/num;
day++;
}*/
LL kk=k,G=0;
while(G<n&&kk>0)
{
LL y=(n-G)/num;
if(y<m)
{
LL o=(n-G+m-1)/m;
return o<=kk;
}
LL ch=n-num*y;
LL d=(ch-G)/y+1;
d=min(d,kk);
G+=y*d;
kk-=d;
}
return G>=n;
}
int main()
{
scanf("%lld %lld %lld",&n,&k,&m);
LL left=1;
LL right=min(n/m,k);
while(left+1<right)
{
LL mid=(left+right)/2;
if(check(mid))
{
left=mid;
}
else right=mid;
}
printf("%lld\n",left);
return 0;
}