題意(Codeforces 965C)
k人分n個糖果,每個糖果至多屬於1個人。A某人是第一個拿糖果的。(這點很重要!!) 他x個x個的發糖果,從第一個(他自己)到最後一個,然後再到第一個;多餘的糖果丟掉。x不能大於M,單個人最多分糖果輪到他D次。問A某人最多能拿多少糖果。
分析
先說一句話:這條題目極度坑爹。
先考慮題意,可以發現,A某人不論如何,一定會吃到 個糖果,這裏的 指該次分發中的最多次數。因此,我們只需要求這個值即可。
然後觀察數據規模,會發現n,k,m,d中只有d的規模最小(不到1000)。因此對d進行枚舉,然後選擇最大x,並計算/更新值即可。
那麼這題坑在哪裏呢?首先是前面的數據枚舉,選錯了那就是對 操作,那就很坑了;第二,看下面的數據:
23925738098196565 23925738098196565 23925738098196565 1000
輸出是:
23925738098196565
這個數據坑爹在哪裏呢?這個數據 的值恰好是unsigned long long的上限。。。然後+1就喜溢出了,變成0了。。。。然後,然後就RE了啊!!!!然後,如果你用double是不是以爲你自己贏定了?除法誤差2333(就是會恰好讓你 ,然後你如果用浮點數比較會產生浮點數誤差)
這個數據估計比賽的時候是一坑一個準,專坑C/C++選手,死不瞑目啊……
代碼
爲了排錯我一個一個的加註釋解讀啊。。。
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define PB push_back
#define MP make_pair
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
#define pr(x) cout << #x << " = " << x << " ";
#define prl(x) cout << #x << " = " << x << endl;
#define ZERO(X) memset((X),0,sizeof(X))
#define ALL(X) X.begin(),X.end()
#define SZ(x) (int)x.size()
using namespace std;
typedef pair<double ,double > PI; //specially defined here.
typedef pair<pair<int,int>, int> PII;
typedef pair<pair<pair<int,int>, int>, int> PIII;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define quickio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
/* debug("Precalc: %.3f\n", (double)(clock()) / CLOCKS_PER_SEC);
clock_t z = clock();
solve();
//debug("Test: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
*/
template<typename T = int>
inline T read() {
T val=0, sign=1;
char ch;
for (ch=getchar();ch<'0'||ch>'9';ch=getchar())
if (ch=='-') sign=-1;
for (;ch>='0'&&ch<='9';ch=getchar())
val=val*10+ch-'0';
return sign*val;
}
int main()
{
ll n,k,m,d;
cin>>n>>k>>m>>d;
ll ans=0;
for(ll i=1;i<=d;++i) // i should be ll as well.
{
// no matter which value d or x has, Arakdy always has x*d candies. so we only need to have a larger x :)
ll maxx;
ld tmp=(i-1)*k+1;
// 坑坑坑!
if(n<tmp) maxx=min(m,ll(0));
else if(n-1==(i-1)*k) maxx=min(m,ll(1)); //....
else maxx=min(m,ll(n/tmp));
tmp=(n/maxx+k-1);
if(maxx==0 || ll(tmp/k)!=i) // add k-1 means make A always be able to get his candy in d times.
continue;
ans=max(ans,maxx*i);
}
cout<<ans<<endl;
return 0;
}