-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCANDY3.cs
44 lines (38 loc) · 1.27 KB
/
CANDY3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
// https://www.spoj.com/problems/CANDY3/ #division #mod-math
// Determines if N bags full of candies can have their contents
// redistributed equally amongst N children.
public static class CANDY3
{
// I guess the numbers will be too big if summed directly, so need to use
// property of modular arithmetic: (a + b) mod m == (a mod m + b mod m) mod m
public static bool Solve(ulong[] backpackCandyCounts)
{
uint N = (uint)backpackCandyCounts.Length;
ulong modSum = 0;
for (int i = 0; i < N; ++i)
{
modSum = (modSum + (backpackCandyCounts[i] % N)) % N;
}
return modSum == 0;
}
}
public static class Program
{
private static void Main()
{
int remainingTestCases = int.Parse(Console.ReadLine());
while (remainingTestCases-- > 0)
{
Console.ReadLine();
int backpackCount = int.Parse(Console.ReadLine());
ulong[] backpackCandyCounts = new ulong[backpackCount];
for (int i = 0; i < backpackCount; ++i)
{
backpackCandyCounts[i] = ulong.Parse(Console.ReadLine());
}
Console.WriteLine(
CANDY3.Solve(backpackCandyCounts) ? "YES" : "NO");
}
}
}