2018/7/20 CF#476 C. Greedy Arkady

题目描述:

kk people want to split nn candies between them. Each candy should be given to exactly one of them or be thrown away.

The people are numbered from 1 to k, and Arkady is the first of them. To split the candies, Arkady will choose an integer xx and then give the first xx candies to himself, the next xx candies to the second person, the next xx candies to the third person and so on in a cycle. The leftover (the remainder that is not divisible by x) will be thrown away.

Arkady can't choose x greater than MM as it is considered greedy. Also, he can't choose such a small x that some person will receive candies more than D times, as it is considered a slow splitting.

Please find what is the maximum number of candies Arkady can receive by choosing some valid x.

输入格式

The,only,line,contains,four,integers nn, kk, MM and DD (2≤n≤10^18,2≤k≤n,1≤M≤n, 1≤D≤min(n,1000),M⋅D⋅k≥n) — the number of candies, the number of people, the maximum number of candies given to a person at once, the maximum number of times a person can receive candies.

输出格式

Print a single integer — the maximum possible number of candies Arkady can give to himself.

Note that it is always possible to choose some valid xx.

样例

input

20 4 5 2

output

8

input

30 9 4 1

output

4

Note

In the first example Arkady should choosex=4. He will give 4 candies to himself, 4 candies to the second person, 4 candies to the third person, then 4 candies to the fourth person and then again 4 candies to himself. No person is given candies more than 2 times, and Arkady receives 8 candies in total.

Note that if Arkady choosesx=5, he will receive only 5 candies, and if he choosesx=3, he will receive only 3+3=6 candies as well as the second person, the third and the fourth persons will receive 3 candies, and 2 candies will be thrown away. He can't choose x=1nor x=2 because in these cases he will receive candies more than 2 times.

In the second example Arkady has to choose x=4x=4, because any smaller value leads to him receiving candies more than 11 time.

题目大意:

有n个糖果, 要分给k个人,一次最多只能给一个人分M个糖果,最多分D轮;Arkady站在队伍前面,可以第一个分到糖果,并且由他决定一轮给每个人分几个糖果。(如果一轮给每个人分x个糖果,那么如果分剩下的糖果小于x个,则把这些糖果扔掉而不是给下一个人),Arkady是一个很贪心的人,问每次给一个人分几个糖果,Arkady能分到的糖果最多,输出这个最大值。

解题思路:

一开始推公式,发现似乎有一定的单调性,然后发现其实x<1000,可以模拟遍历一次就行,这道题最坑也是最容易错的点是在于爆long long,当10的18次方拿去乘以一个大于10的数变成10的十九次方时,long long 也就爆了,于是乎出现了一些奇奇怪怪的数。我解决的方法是如果给每个人分的糖果数算出来是0的时候,再加多轮次就直接每个人只分得0个,也就是不再去计算了。

#include <iostream>
#include<algorithm>

using namespace std;

int main()
{
    long long n, k, m, d, ans, part;
    cin >> n >> k >> m >> d;
    int i, flag = 1;
    long long maxx = m;
    for(i = 1; i < d; i++)
    {
            if(flag)
            {
                 part = n/(i*k+1);
                 if(part == 0)
                        flag = 0;
            }
           if(part>m)
           {
                   ans = (n/m)/k;
                   if((n/m)%k>=m)
                        ans = (ans+1)*m;
                   else
                        ans = ans*m;
           }
           else
           {
                   ans = part*(i+1);
           }
           maxx = max(maxx, ans);

    }
    cout << maxx << endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值