[Kick Start 2022 #Session 1] Sample Problem

Titu·2022년 2월 15일
0

Algorithm

목록 보기
27/28

Problem

You have gathered N bags of candy and you want to distribute the candy amongst M kids. The i-th bag contains Ci pieces of candy. You want to make sure that every kid get the same amount of candy and that the number of pieces of candy they receive is the greatest possible. You can open each bag and mix all pieces of candy before distributing them to the kids.

How many pieces of candy will remain after you share the candy amongst kids, based on the rules described above?

Input

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case consists of two lines. The first line of each test case contains two integers: integer NN, the number of candy bags, and MM, the number of kids.

The next line contains NN non-negative integers C1,C2,…,CNC1,C2,…,CN representing array CC, where the ii-th integer represents the number of candies in the ii-th bag.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 11) and y is the number of candies that will remain if you divide candies between kids according to the rules described above.

Limits

Time limit: 40 seconds.
Memory limit: 1 GB.

/* Sample Input
2
7 3
1 2 3 4 5 6 7
5 10
7 7 7 7 7
*/

/* Sample Output
Case #1: 1
Case #2: 5
*/

import java.util.*;
import java.io.*;

public class SampleProblem {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int t = in.nextInt(); // Scanner has functions to read ints, longs, strings, chars, etc.
        for (int i = 1; i <= t; ++i) {
            int n = in.nextInt();
            int m = in.nextInt();
            int candies = 0;

            for(int j = 1; j <= n; j++) {
                candies += in.nextInt();
            }

            int leftCandies = candies % m;
            
            System.out.println("Case #" + i + ": " + leftCandies);
        }
    }
}
profile
This is titu

1개의 댓글

comment-user-thumbnail
2023년 9월 6일

I am impressed by your ability to only up simplify complex concepts related to this theme, making it accessible to a wide range of readers without compromising on the depth of the subject matter.

답글 달기