candies between them. Each candy should be given to exactly one of them or be thrown away.
The people are numbered from 1
) will be thrown away.
Arkady can't choose x
greater than M as it is considered greedy. Also, he can't choose such a small x that some person will receive candies more than Dtimes, 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 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 x
.
20 4 5 2
8
30 9 4 1
4
In the first example Arkady should choose x=4
candies in total.
Note that if Arkady chooses x=5
, he will receive only 5 candies, and if he chooses x=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 2times.
In the second example Arkady has to choose x=4
, because any smaller value leads to him receiving candies more than 1time
.
题意:k个人分n个糖果,第一个人先拿x个,然后每个人轮流拿x个,直至不到x个糖果(可以循环轮流)。
问第一个人可分的的最多的糖果的个数。
一开始想的用二分,但是精度不好控制,条件不好写。后纯思维,采取暴力。第一个人要想分最多,其他人分
i-1次,第一个人就分i次,才能够保证糖数最多。
代码:
#include<stdio.h>
#include<string.h>
#define ll long long int
int main()
{
ll n,k,m,d,i,x,countt;
scanf("%lld %lld %lld %lld",&n,&k,&m,&d);
countt=0;
for(i=1;i<=d;i++){
x=n/(k*(i-1)+1); //其他人都是i-1次,只有第一个是i次,所以总共的是k*(i-1)+1次;
if(x==0)
break;
if(x>m)
x=m;
countt=countt>i*x?countt:i*x;
}
printf("%lld\n",countt);
return 0;
}