We are given: n accounts, initial ratings a_i. Killjoy's account is infected with rating x (constant). Infection spreads instantly if an infected account and an uninfected account have equal rating (at any moment). Initially, only Killjoy's account is infected. In each contest, we can select any subset of the n accounts (including infected ones) to participate. Killjoy cannot participate. For participants, we can change their ratings by integer amounts such that the sum of changes is zero. Ratings can become any integer (including possibly huge). After contest, infection can spread if any infected account and uninfected account have equal rating. This can happen after each rating change. Also, infection can happen at the very beginning before any contest: if any a_i equals x, then that account gets infected instantly (since Killjoy's rating is x, and if an uninfected account has rating equal to x, it becomes infected instantly). And then that infected account can infect others if they become equal later.
Goal: infect all n accounts with minimal number of contests. We can plan participation and rating changes arbitrarily.
We need to output minimal contests per test case.
Observations: We can manipulate ratings to make them equal to x (or to each other) by adjusting changes. Since sum of changes among participants must be zero, we can change participants' ratings arbitrarily but with sum zero.
Important: After each contest, infection spreads based on equality of ratings at that moment. Also note that once an account is infected, it remains infected.
We want to minimize number of contests.
First, check if any a_i equals x initially. Then those accounts become infected instantly. Actually, "Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest." So initially, if any a_i == x, those get infected at time 0. So after that initial infection, we have some infected accounts.
If all n accounts already infected? That would happen if all a_i == x? Actually n accounts are the other accounts. If all a_i == x, then all get infected instantly. So contests needed = 0. That's example 2: all 4, x=4 => 0 contests.
Now, if not all are initially infected, we need to use contests.
We can consider that in each contest, we can possibly infect some accounts by making their ratings equal to some infected account's rating (maybe x, or some other infected account's rating). Since the infected accounts can also change their ratings? Yes, any of the n accounts (including infected ones) can participate. So we can change ratings of infected accounts as well. But note: Killjoy's account rating is constant and cannot be changed (since she doesn't participate). So we always have the infected rating x from Killjoy. However, if we have other infected accounts, their ratings can be changed if they participate, but then they might no longer equal x. But they are already infected; they remain infected. Changing their ratings might help to match other uninfected accounts.
We need to think strategically: The infection spreads only when an infected and uninfected have equal rating. So we want to cause equality between some infected account and uninfected account(s). The simplest way: make an uninfected account's rating become equal to x (Killjoy's rating). Since x is constant and infected, if we can set a_i = x for some uninfected i, then that account becomes infected instantly after the contest. So we can try to achieve that.
In a contest, we can change participants' ratings arbitrarily, subject to sum of changes zero. So if we want to set some subset of accounts to have rating x, we need to adjust them appropriately. However, if we set multiple uninfected accounts to x in one contest, we need to change each of them by (x - a_i). But if we include other participants (maybe infected ones or other uninfected) we can offset the sum of changes to zero. For example, if we take all n accounts, we can change each a_i to x? But then sum of changes = Σ (x - a_i) = nx - Σ a_i. That must be zero for the participants. If we include all n accounts, then participants are exactly those n. The sum of changes must be zero, so we require nx = Σ a_i. If that holds, we can set all to x in one contest. That would infect all instantly after that contest (since all become x, equal to Killjoy). So if the average of a_i equals x, then we can do it in one contest (provided we include all accounts). But what if not? We can also include some extra accounts (like infected ones) to adjust the sum. But infected ones might already be x? Actually, infected accounts could have rating not equal to x if they've been changed in previous contests. But initially infected accounts (including those infected initially due to a_i == x) have rating x (unless we changed them). So initially, infected accounts have rating x. But if we want to change uninfected accounts to x, we could also change infected accounts' ratings away from x to help zero sum. But if we change infected accounts away from x, they might no longer equal Killjoy's rating, but they remain infected. However, that might complicate things because we could lose the x anchor? But Killjoy's rating is always x. So we still have the infected rating x from her. So it's okay.
Thus, in a single contest, we might be able to infect some accounts by making them equal to some infected rating.
General approach: This problem is known from Codeforces: "Killjoy" problem from Round #1337? Actually I recall a problem "Killjoy" with similar story. The typical solution:
- If all a_i == x -> 0.
- Else if some a_i == x OR sum of (a_i - x) = 0? Wait let's recall: I think the answer is:
- 0 if all equal x.
- 1 if sum_{i=1}^n (a_i - x) == 0 (i.e., average equals x) or there exists some a_i == x (since then we can use that infected account to zero sum? Actually common solution: If any a_i == x, then answer is 1? Wait check example 1: n=2, x=69, a=[68,70]. Not all equal x; sum of differences = (68-69)+(70-69) = (-1)+(1)=0, average equals x. According to known solution answer is 1. That matches: Output 1. So for that case, answer is 1. But note: There is no a_i == x initially (68,70). So answer 1. So condition: either some a_i == x OR sum of (a_i - x) == 0 gives answer 1? But in that case, sum==0, so answer 1. However, what about a case where there is at least one a_i == x? Example: n=3, x=5, a=[5,1,9]. There is one a_i==5. Can we infect all in 1 contest? Possibly yes: We already have account 1 infected initially (since rating 5 == x). Then we need to infect accounts 2 and 3. In one contest, can we make them infected? Possibly by making them equal to the rating of an infected account. Options: make them equal to x (5) or equal to the infected account's rating (which is 5). So both need to become 5. To set a2=1 ->5, change +4; a3=9 ->5, change -4; sum changes = 0. So we can just have accounts 2 and 3 participate (and optionally account 1? but if we include account 1, it's already infected, we could also change it to help sum, but not needed). The sum of changes for participants (accounts 2 and 3) is +4 + (-4) = 0, so allowed. After contest, a2=5, a3=5, both equal to x and thus become infected. So indeed one contest suffices. So if any a_i == x initially, we can in one contest change all other accounts to x, because we can offset changes among them? Let's check: Suppose there are multiple uninfected accounts. We need to change each uninfected account to x. The sum of changes for those uninfected accounts is Σ (x - a_i) over uninfected i. This sum may not be zero. However, we can also include the already infected account(s) (with rating x) as participants. If we include an infected account, we can change its rating by some amount to make total sum zero. But note: if we change an infected account's rating away from x, it will no longer be equal to x, but that's okay because it's already infected. However, after contest, will there be any issue? The infected account might have rating different from x, but we still have Killjoy's account with rating x, so uninfected accounts that we set to x will become infected because they equal x. So it's fine. So we can always adjust sum to zero by changing an infected account's rating appropriately (since we can choose any integer change). So with at least one infected account (maybe initially infected because a_i == x), we can make all uninfected accounts become x in one contest, by including that infected account as a participant and adjusting its rating to offset the total sum. For example, suppose n=2, x=5, a=[5, 3]. Initially account1 infected. To infect account2: we want to change its rating from 3 to 5 (+2). To make sum zero, we need total change zero among participants. If we include both accounts, we can change account1 by -2 (so its rating becomes 3) and account2 by +2 (becomes 5). After contest, account2=5 infected; account1=3 but still infected (since infected status persists). So yes, one contest works. So condition: if there exists at least one a_i == x initially, answer is 1? But wait: What if all a_i are already infected? Then answer 0. So if any a_i == x but not all, answer 1. So that matches typical solution: answer is 0 if all equal x; 1 if some equal x or sum of differences zero; else 2. Actually there is nuance: if no a_i == x, but the average of a_i equals x, we can infect all in one contest by setting all to x? But we need sum of changes zero when we change all accounts to x. For that, Σ (x - a_i) must be 0, i.e., Σ a_i = nx, answer 1.
If there is at least one a_i == x, answer 1? As argued, we can use that infected account to offset sum for others. But wait: Is it always possible to infect all in one contest if at least one a_i == x? Let's test: n=3, x=0, a=[0, 100, -100]. Here one a_i ==0. The sum of all a_i =0 = 30? Actually nx = 0, Σ a_i = 0, so average condition holds. That also gives answer 1. That's fine. But suppose n=3, x=0, a=[0, 5, 6]. Σ a_i =11, n*x=0, sum not zero. Can we infect all in one contest? We have infected account1 (rating 0). We want to infect accounts 2 and 3. Approach: we could change account2 to 0 (change -5) and account3 to 0 (change -6). Sum changes = -11. To make total sum zero, we need to include an infected account (account1) and give it change +11, making its rating 11. Then after contest: account2=0, account3=0 infected (equal to Killjoy's 0). Account1=11 but remains infected. So all accounts are infected (account1 infected, accounts 2,3 infected). So indeed one contest works. So yes.
What about scenario where no a_i == x and Σ a_i != n*x? Then can we infect all in one contest? Possibly if we don't set all to x but use a different strategy: perhaps infect some accounts first, then use them to help infect others? But we only have one contest. Could we infect all in one contest without setting all to x? For infection to occur, after contest, each uninfected account must have rating equal to some infected account's rating. Initially only infected account is Killjoy (rating x). So after contest, for an uninfected account to be infected, its rating must be x (since only infected rating present is x, unless we infect another account during the same contest? But infection spreads instantly after rating changes. So within the same contest, after we apply changes, infection spreads. Could we arrange such that some accounts become infected because they become equal to x, and then those newly infected accounts, in turn, cause infection to other accounts because they have equal ratings? However, infection happens instantly after contest. But "instantly infects any uninfected account if their ratings are equal. This can happen ... after each contest." The wording suggests that after the contest, we check equality and infection spreads. But does it propagate? It says "any infected account instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest." The term "instantly infects any uninfected account if their ratings are equal" implies that if after a contest, there is an infected account and an uninfected account with same rating, the uninfected becomes infected. But could this happen in a chain? Typically, "instantly" suggests that as soon as the condition is met, infection occurs. But if after changes, we have multiple equalities, infection could spread in a cascade within the same "after contest" phase. For example: we have infected account A with rating r, and uninfected B and C both have rating r (maybe after changes). Then both become infected. Or if we have infected A with rating r, uninfected B with rating s not equal r, but we also have uninfected C with rating r, B gets infected only if equals r? Not chain. But what if we have infected A with rating r, uninfected B with rating s, and uninfected C with rating s. If after contest, B becomes infected because it matches A? Actually B only matches A if s = r. But if s ≠ r, B remains uninfected. But if B and C both have s, and none infected have s, they remain uninfected. However, if we set A to s (change its rating) and also set B to s, then both A and B have s, so B becomes infected because A is infected. But then C also has s, and now B is infected and C has s, so C becomes infected as well. So indeed chain infection can happen instantly as long as there is a connection. So in one contest, we could potentially create a scenario where after rating changes, many accounts have same rating (maybe not x) and at least one of them is already infected (either Killjoy or previously infected). Since initially only Killjoy is infected (rating x). So to cause chain, we need some infected account's rating to equal some uninfected account's rating. The infected account could be Killjoy (rating x), or if we had any initially infected from a_i == x, they also have rating x initially. But we can also change an infected account's rating to something else; it remains infected. So we could change Killjoy? No, Killjoy doesn't participate; her rating is constant x. So we cannot change x. So the only infected rating we can modify is other accounts that become infected (either initially or during contest). But during a contest, we can change the ratings of participants (including any account that is already infected before contest). So suppose we have no initially infected accounts (a_i != x). Then before contest, only infected is Killjoy with rating x. We can choose to change some uninfected accounts' ratings to some value y (maybe not x), and also change some other infected? There is only Killjoy infected, but she cannot be changed. So the only infected rating we can leverage is x. So any account that becomes equal to x after contest will become infected. But what about accounts that are set to a value y different from x, but also we might change some other account to y, and that other account could be infected if we first set it to x? That would require that some account becomes infected (via x) and then its rating (maybe changed) equals y. But that would happen in the same contest? Let's think about the order of events during/after a contest: The process: before contest, certain accounts are infected. We select participants, decide changes. After contest, ratings are changed (simultaneously). Then infection spreads. So after changes, we have a new rating configuration. At that moment, infection spreads: if any infected account (including those that were infected before contest) has the same rating as an uninfected account, that uninfected becomes infected. This infection then may cause further infections if the newly infected account's rating matches other uninfected accounts (since it becomes infected instantly, so we can consider that all such matches are checked simultaneously or iteratively until no more infections). So the final infection set after the contest is the transitive closure of equality matches with any initially infected account.
So, in a single contest, we could potentially set a chain: we could set some account A to rating x (so it becomes infected because it matches Killjoy). Simultaneously, we set account B to rating y (different from x). But for B to become infected, it needs to match some infected rating after changes. If we also set account A (which we just set to x) to something else? But A's rating after changes is whatever we set it to. If we set A to x, it's infected. If we also set A to something else, we have to choose one final rating. So we can't have A both x and y. The rating after changes is a single number. So if we set A to x, it becomes infected and has rating x. Then B's rating y may not equal x. So B remains uninfected. However, we could set A to y, and also set B to y. But for A to be infected before the contest? It wasn't infected initially (since a_i != x). So A would be uninfected before contest, and we set its rating to y. After changes, A's rating is y, but is it infected? No, because it was uninfected before. It could only become infected if after changes its rating equals some infected rating. But the only infected rating before contest is x from Killjoy. So unless y = x, A would not become infected. Therefore, if we want to use a chain where we create a new infected account with some rating and then that infects others with the same rating, we need that the first account becomes infected by matching x. So we need to set at least one account to x. So in any one-contest scenario, at least one account must be set to x (or already be infected) to serve as bridge. Then other accounts could be set to some other value that matches that account's rating, but if that account is set to x, then other accounts must be set to x as well to be infected directly. Alternatively, we could set the infected account (which we set to x) to a different rating after we infect it? But we can't change it after infection within the same contest because changes happen simultaneously. So the rating we assign to that account is final for that contest. So to have an infected account with rating y (not x) after contest, we would need that account to be infected before contest and then we change its rating to y. Since initially only Killjoy is infected (rating x). So if we change Killjoy? No. So we can't have an infected account with rating ≠ x before contest unless we already had an infected account from initial a_i == x (which would have rating x). So essentially, in a single contest without any pre-existing infected account besides Killjoy, the only way to infect any account is to set its rating to x. Because the only infected rating present after changes is x (from Killjoy). Any account that becomes infected must have rating equal to some infected rating. The only infected ratings after changes are: Killjoy's x (constant), and any account that was infected before the contest and we did not change its rating? But if it was infected before, its rating might be something else if we changed it? Wait: An account that was infected before contest could have its rating changed. After changes, it may have a new rating, but it remains infected. So after changes, we could have infected accounts with ratings other than x, if we changed them. However, to have such an infected account with rating y, it must have been infected before the contest. Initially, the only infected accounts are Killjoy (rating x) and any accounts with a_i == x (rating x). So all initially infected accounts have rating x. So before contest, all infected accounts have rating x. In the contest, we can change the ratings of some of these infected accounts to some other values y. After changes, those accounts will have rating y, and remain infected. Then, after contest, any uninfected account with rating y will become infected. So we could potentially infect accounts by setting them to some y, while also changing some initially infected account's rating from x to y. This would require that we have at least one initially infected account besides Killjoy? Actually we can use an initially infected account (which is either Killjoy? Killjoy can't participate, so we cannot change her rating. So we need an infected account among the n that we can change. Initially, if no a_i equals x, then there is no infected account among the n. The only infected is Killjoy (outside the n). Killjoy cannot be changed. So we cannot have an infected account with rating y after contest, because the only infected we could change is not allowed. Therefore, in the case where no a_i equals x initially, we cannot use the trick of changing an infected account's rating to y to infect others with y. The only infected rating after contest will be x (Killjoy). So all accounts we want to infect must end up with rating x. So to infect all in one contest, we must set all accounts to x, which requires Σ (x - a_i) = 0, i.e., Σ a_i = n*x. So condition for one contest when no initial infected is average equals x.
Now, if there is at least one a_i == x initially, we have an infected account among the n that we can change. So we can use that infected account to adjust sum and also potentially set other ratings to something else? But to infect all, we could still just set all uninfected accounts to x, as earlier. That always works because we can offset sum using the infected account. So answer 1. But what about the possibility of using a chain: set infected account to some y, and set all others to y? Could that also work with potentially easier sum constraints? But we already have answer 1 with the simple method. So answer 1 is achievable.
Thus, answer classification:
- 0 if all a_i == x.
- 1 if either there exists some a_i == x OR Σ a_i == n*x.
- Otherwise, answer 2.
But is it always possible in 2 contests if neither condition holds? Let's explore. The typical solution for the known problem is indeed: 0, 1, or 2. Let's verify with sample: Test case 3: n=9, x=38, a = [-21,83,50,-59,-77,15,-71,-78,20]. Check if any a_i == 38? No. Compute sum of a_i: Let's calculate: -21+83=62, +50=112, -59=53, -77=-24, +15=-9, -71=-80, -78=-158, +20=-138. So Σ a_i = -138. nx = 938=342. Σ a_i != n*x. So answer should be 2 according to classification. Output says 2. Good.
So answer pattern matches.
But let's double-check: Are there any edge cases where answer could be more than 2? Typically no. The reasoning: If we cannot do it in 1 contest, we can do it in 2. In the first contest, we can infect at least one account (maybe all but one?) Actually we can always infect all but one? Let's think. Known solution: In the first contest, we can make all ratings become x except one account? Or we can make all but one become x? But we need to satisfy sum zero constraints. There's a known approach: In contest 1, we can infect all but one account by setting them to x, and use the last account to adjust sum. But then after contest 1, we have some infected accounts. Then in contest 2, we can infect the last account easily because we have an infected account we can use to offset. More specifically: If Σ a_i != nx and no a_i == x, then we can do: In first contest, we can infect n-1 accounts by setting them to x, and change the nth account arbitrarily to make total sum zero? Let's see: Suppose we select all n accounts to participate. We want to set n-1 of them to x. Let those be accounts 1..n-1. Then their total change: Σ_{i=1}^{n-1} (x - a_i). Let the change for account n be Δ. We require sum changes = 0 => Δ = - Σ_{i=1}^{n-1} (x - a_i) = Σ_{i=1}^{n-1} a_i - (n-1)x. So we set account n's rating to a_n + Δ = a_n + Σ_{i=1}^{n-1} a_i - (n-1)x = Σ_{i=1}^{n} a_i - (n-1)x. After contest, accounts 1..n-1 have rating x, so they become infected (since Killjoy has rating x). Account n has some rating r = Σ a_i - (n-1)x. Note that r may not equal x (since Σ a_i != nx => r = Σ a_i - (n-1)x = (Σ a_i - n*x) + x = (diff) + x, where diff != 0, so r ≠ x). So after contest 1, we have all accounts infected except account n. Also, we have many infected accounts with rating x. In contest 2, we can now infect account n. Since we have infected accounts, we can include account n and one infected account (say account1) as participants. We want to change account n's rating to x (so it becomes infected). The change for account n is x - r. To offset sum, we can change the infected account by -(x - r). Since infected account's rating can be changed arbitrarily, this is allowed. So after contest 2, account n becomes infected. So total 2 contests. This works provided we can set n-1 accounts to x in contest 1. But is it always allowed? Yes, because we can choose any integer changes; we set n-1 accounts to exactly x, and set the last account to whatever needed to satisfy sum zero. That is always possible. So answer is at most 2.
But wait: What about the case where n=1? But constraints say n>=2. So fine.
Thus minimal contests is:
- 0 if all a_i == x.
- 1 if (any a_i == x) OR (sum of a_i == n*x). Note that if all equal x, both conditions hold, but we already output 0. So the 1 condition is for not all equal.
- else 2.
But let's check if there is any scenario where answer 1 could be possible even if neither condition holds, using a more clever method? For example, using chain infection where we change some infected account to a new rating and set others to that rating. Initially if no a_i == x, we have only Killjoy infected with rating x. To have infected account with rating y ≠ x after contest, we need to change an infected account to y. The only infected we can change is among the n accounts? But there are none initially infected besides Killjoy, and Killjoy can't be changed. So we can't have an infected account with rating y after contest. So all infected after contest must have rating x. Therefore, any uninfected account must be set to x to be infected. So we need to set all accounts to x, requiring sum condition. So no other way. So condition necessary.
What about case where we have some initially infected (a_i == x). Then answer 1 always possible? As argued, yes, by using that infected account to offset sum while setting all uninfected to x. But wait: Is there any scenario where all uninfected can't be set to x in one contest? We can include the infected account (which is already infected) as a participant. We'll change its rating by some amount to make total sum zero. But does the infected account need to be changed? Possibly we can also include other infected accounts, but at least one infected account among participants to adjust sum. But we have at least one infected account (the one with a_i == x). So we can include it. For each uninfected i, we set its rating to x (change = x - a_i). Let S = Σ_{uninfected} (x - a_i). This may be non-zero. We then include the infected account(s) and assign changes to them such that total sum zero. Since we can assign any integer change to the infected account, we can set its change = -S. Then its new rating becomes x + (-S) = x - S. That's fine. So after contest, all uninfected become x and infected, and the originally infected account may have changed rating but remains infected. So all accounts become infected. So yes, one contest suffices. However, what about the possibility that there are multiple infected accounts initially? Same reasoning.
Thus answer 1 condition holds.
One nuance: Could there be a scenario where all a_i == x? Then answer 0. But note: If all a_i == x, then the condition "any a_i == x" is true, but we output 0 because no contest needed. So we must check the 0 case first.
Hence the solution is straightforward.
Now we need to implement for multiple test cases. Input constraints: t up to 100, n up to 1000, values within -4000..4000, but not important.
Implementation: For each test case:
- Read n, x.
- Read array a of length n.
- If all a_i == x: print 0.
- Else if any a_i == x or sum(a) == n * x: print 1.
- Else print 2.
But careful: The condition sum(a) == n*x might be true even if all a_i == x (but then answer 0). However we already handled all equal case first. So it's fine.
We should consider integer overflow? n up to 1000, x and a_i up to 4000 in absolute, so sum up to 4e6, fits in 32-bit int. Use Python anyway no issue.
Now let's double-check with more examples:
Example from known problem: n=3, x=10, a=[5,15,20]. Sum=40, n*x=30 -> not equal. No a_i ==10. So answer 2. Can we do in 2? Yes as described.
Test: n=2, x=0, a=[1, -1]. Sum=0 => average=0 => answer 1? But no a_i ==0. But we can set both to 0? changes: +(-1) for first? Actually to set a1=0 need -1, a2=0 need +1, sum=0, so allowed. So one contest works. Output 1.
Test: n=2, x=0, a=[1,2]. Sum=3, not 0, no a_i==0, answer 2. Strategy: first contest set one to 0, other adjust accordingly. Let's simulate: set account1 to 0 (change -1). To zero sum, include both: change account2 = +1. Then after contest: a1=0 infected, a2=3. Second contest: infect account2 by setting it to 0: change -3, and offset with infected account1 change +3, so account1 becomes 3, but infected already. All infected. So 2 contests.
Edge case: n=2, x=5, a=[5,5] -> all equal? Actually both equal x, so answer 0.
Edge case: n=2, x=5, a=[5,4] -> any a_i==x true, so answer 1.
Edge case: n=2, x=5, a=[6,4] -> sum=10, n*x=10, average equal, so answer 1.
Thus simple.
But wait, there is a nuance: The problem statement says: "Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest." So infection can happen immediately at the beginning. If any a_i == x, they become infected at the beginning. So yes.
Now, is there any case where answer 0 if all a_i == x? But note: There are n accounts except Killjoy's, so if all a_i == x, they all become infected at beginning. So contests needed = 0.
Now check the sample: test case 1: n=2, x=69, a=[68,70]. Sum=138, n*x=138 -> average condition true, answer 1. Output 1.
test case 2: all 4, x=4 -> answer 0.
test case 3: answer 2.
Thus matches.
Thus the solution is trivial.
But we must also consider that in contest, we can also choose not to include all accounts. However, our reasoning for 1 contest conditions is sound.
Now I'll implement solve() accordingly.
But the problem statement says "2 ≤ n ≤ 10^3" but in input example they have n=1? Actually says 2 ≤ n, so okay.
Now code.
We'll read t, loop.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n = int(next(it)); x = int(next(it))
a = [int(next(it)) for __ in range(n)]
if all(val == x for val in a):
out.append('0')
elif any(val == x for val in a) or sum(a) == n * x:
out.append('1')
else:
out.append('2')
print('\n'.join(out))
That should work.
However, I recall that there is a known twist: In the condition for answer 1, sometimes it's "if any a_i == x or sum(a) == n * x". But also there is scenario where after initial infection, we might be able to infect all in one contest even if no a_i == x and sum != n*x? Let's double-check thoroughly using the model.
Suppose no a_i == x and sum != n*x. In one contest, can we infect all? Let's attempt to construct a strategy: We have only Killjoy infected (x). We can include some subset of accounts as participants. Let I be set of participants. For each participant i, we assign a change d_i such that Σ d_i = 0. After changes, the rating of participant i becomes a_i + d_i. Non-participants keep their original rating.
Infection after contest: Any account whose rating equals x becomes infected (since Killjoy's x). Also, if any uninfected participant has rating equal to some infected participant's rating, they get infected. But initially only infected participant is Killjoy (who is not a participant). However, participants can become infected if they end up with rating x. So infection can only spread via x (Killjoy) unless we create an infected participant by making it x. So any participant that does not become x will not be infected unless they match some other infected participant's rating. But the only way to have an infected participant with rating other than x is if that participant was infected before contest. But there are none. So all participants we want to infect must end up with rating x. So we need to choose a set S of participants (could be all accounts) and assign changes such that for each i in S, a_i + d_i = x, and Σ d_i = 0. This implies Σ (x - a_i) over i in S = 0. If S is the set of all accounts, that gives Σ (x - a_i)=0 => Σ a_i = n*x. If S is a proper subset, then after contest, accounts in S become infected (since they become x). Accounts not in S remain uninfected, with their original a_j (which are not x, because no initial a_j == x). So not all infected. To infect those not in S, we would need them to become infected by matching some infected rating after contest. But the only infected ratings after contest would be x (from Killjoy and those S participants). So to infect an account not in S, it must have rating x after contest. But it didn't change, and its original rating is not x, so it won't be infected. So in one contest, we cannot infect all unless we set all accounts to x (or they were already x). So condition is necessary and sufficient.
Now consider the case where some a_i == x initially. Then before contest, there is at least one infected account among the n (call it p). In a contest, we can include that infected account as a participant and change its rating to something else. Could we infect all without setting all uninfected to x? Possibly we could set all accounts (including the infected one) to some other common value y, such that after contest all have rating y, and since the infected account (p) is infected and now has rating y, all others become infected because they match y. Is this possible? We would need to set all accounts to a common value y, not necessarily x. Then after contest, all accounts have rating y, and p (infected) still infected, so all others become infected. This could be done even if sum of a_i is not nx. However, we need to satisfy the zero-sum constraint for participants (all n accounts). The sum of changes to set all accounts to y is Σ (y - a_i). This must be 0, i.e., Σ a_i = ny. So we need to choose y such that y = average of a_i. But then y may not equal x. That's okay because p is infected. But note: p initially had rating x (since a_p = x). For it to become y, we change it by y - x. That's allowed. So if we can find a y such that all accounts become y, then infection spreads. But the zero-sum condition forces y = (Σ a_i)/n. So if the average of a_i equals some integer? But the changes must be integers, and y must be integer because ratings are integers. However, the problem states that new ratings can be any integer; we can choose changes as integers, but then y must be integer. So we require that Σ a_i is divisible by n? Actually we can set each rating to any integer, but we need changes to be integers. If we set rating of account i to y, change = y - a_i, which will be integer if y is integer. So we need to pick y integer. The condition Σ (y - a_i) = 0 => n*y = Σ a_i => y = Σ a_i / n. For y to be integer, Σ a_i must be divisible by n. If Σ a_i is not divisible by n, we cannot set all accounts to exactly the same integer rating because the average would be non-integer, and we cannot make all changes integer with the same y? Wait, we could set different ratings, but we want all to be equal to some common y. For all to be equal, they must be same integer. So that requires Σ a_i divisible by n. But does the problem require all changes to be integer? Yes, each change is an integer. So if Σ a_i is not divisible by n, we cannot set all accounts to the same integer while keeping sum changes zero because that would require y = Σ a_i / n not integer. However, we could set them to different ratings, but still achieve infection through chain? Possibly we can set some to y, some to x, etc.
But is it necessary to use this approach to achieve infection in one contest when we have an initial infected? The simpler method (set all uninfected to x) always works regardless of divisibility. Because we set uninfected to x, and adjust infected account(s) accordingly. That method yields all infected in one contest. So we already have a working 1-contest strategy. So condition for 1 contest includes any a_i == x. So no need to consider average condition for that case, but note: The average condition also gives 1 contest even if no a_i == x. So final condition is as described.
Now, consider edge: Suppose some a_i == x, but also Σ a_i = n*x? Already covered.
Thus solution is correct.
Now, regarding proof: Known problem "Killjoy" from Codeforces Round #1397? Actually it's problem 1419A? Not sure. But answer pattern is established.
Let's also check constraints: t up to 100, n up to 1000, so O(n) per test is fine.
Now we should produce final answer.
But I recall that sometimes there is a nuance: In the condition for answer 1, it's "if any a_i == x OR sum of all a_i == nx". But what about the case where all a_i are equal but not equal to x? For example, n=3, x=10, a=[5,5,5]. Sum=15, nx=30, not equal. No a_i ==10. So answer should be 2. But could we infect all in one contest? Let's see. All a_i equal 5. We have no initial infected. In one contest, we could set all accounts to some rating y. To infect all, we need after contest all accounts to be infected. Since only infected rating initially is x=10, they must become 10 to be infected, unless we create an infected account with rating y and then all become y. But we can't create an infected account with rating y because we have no infected to change. So must set all to 10. That requires sum changes zero: 3*(10-5)=15, not zero. So cannot. Could we do something like set two accounts to 10, and the third to something else such that the third becomes infected via matching some infected rating? After contest, the two accounts become 10, infected. The third might be set to 10 as well? If not, it remains 5 (or something else) and not equal to 10, so not infected. So can't infect all. So 2 contests needed. That matches our classification.
Now what about a scenario where there is an initial infected but sum condition fails? Example: n=2, x=5, a=[5, 6]. Condition any a_i == x true, so answer 1. Works as described: set uninfected (account2) to 5 (change -1), set infected (account1) to 4 (change -1?) Wait we need sum zero: change for account2 = -1 (5-6), change for account1 = +1 (so becomes 6) gives sum 0? Actually we want total zero: change2 + change1 = 0 => change1 = +1, so account1 becomes 6. Then after contest, account2=5 infected, account1=6 but still infected. All infected. So yes 1 contest. So the method works.
Now we need to be careful: In the method, we set all uninfected to x. That might require changing the infected account by -Σ (x - a_i). That sum could be large, but that's fine.
Thus answer is as above.
Now let's produce final solution.
But the problem says: "Find out the minimal number of contests needed to infect all accounts." The solution is minimal.
Thus code is straightforward.
I'll include comments.
Potential pitfalls: The sum condition may cause overflow in C++ but not in Python. Use integers.
Now, check if any test case could have answer more than 2? Could there be scenario requiring more? The construction shows at most 2. So answer is either 0,1,2.
Thus final.
I'll now produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
x = int(next(it))
a = [int(next(it)) for _ in range(n)]
# Check if all already infected
if all(val == x for val in a):
out_lines.append('0')
continue
# Conditions for exactly one contest
if any(val == x for val in a) or sum(a) == n * x:
out_lines.append('1')
else:
out_lines.append('2')
sys.stdout.write('\n'.join(out_lines))
if name == 'main':
solve()