A. Berland Poker

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The game of Berland poker is played with a deck of nn cards, mm of which are jokers. kk players play this game (nn is divisible by kk).

At the beginning of the game, each player takes nknk cards from the deck (so each card is taken by exactly one player). The player who has the maximum number of jokers is the winner, and he gets the number of points equal to x−yx−y, where xx is the number of jokers in the winner's hand, and yy is the maximum number of jokers among all other players. If there are two or more players with maximum number of jokers, all of them are winners and they get 00 points.

Here are some examples:

  • n=8n=8, m=3m=3, k=2k=2. If one player gets 33 jokers and 11 plain card, and another player gets 00 jokers and 44 plain cards, then the first player is the winner and gets 3−0=33−0=3 points;
  • n=4n=4, m=2m=2, k=4k=4. Two players get plain cards, and the other two players get jokers, so both of them are winners and get 00 points;
  • n=9n=9, m=6m=6, k=3k=3. If the first player gets 33 jokers, the second player gets 11 joker and 22 plain cards, and the third player gets 22 jokers and 11 plain card, then the first player is the winner, and he gets 3−2=13−2=1 point;
  • n=42n=42, m=0m=0, k=7k=7. Since there are no jokers, everyone gets 00 jokers, everyone is a winner, and everyone gets 00 points.

Given nn, mm and kk, calculate the maximum number of points a player can get for winning the game.

Input

The first line of the input contains one integer tt (1≤t≤5001≤t≤500) — the number of test cases.

Then the test cases follow. Each test case contains three integers nn, mm and kk (2≤n≤502≤n≤50, 0≤m≤n0≤m≤n, 2≤k≤n2≤k≤n, kk is a divisors of nn).

Output

For each test case, print one integer — the maximum number of points a player can get for winning the game.

Example

input

Copy

4
8 3 2
4 2 4
9 6 3
42 0 7

output

Copy

3
0
1
0

Note

Test cases of the example are described in the statement.

解題說明:此題是一道模擬題,先判斷joker的數目與每個人分到的牌數目之間的關係,最多爲m,然後再判斷剩下的joker能否被剩下的選手平分。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>

using namespace std;

int main()
{
	int a, n, m, k, c;
	scanf("%d", &a);
	while (a--) 
	{
		scanf("%d %d %d", &n, &m, &k);
		if (m <= n / k)
		{
			printf("%d\n", m);
		}
		else 
		{
			c = m - n / k;
			if (c % (k - 1) == 0)
			{
				printf("%d\n", (n / k) - c / (k - 1));
			}
			else
			{
				printf("%d\n", (n / k) - c / (k - 1) - 1);
			}
		}
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章