C. Killjoy SOLUTIONS CODEFORCES

Killjoy SOLUTION CODEFORCES

A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).

Killjoy’s account is already infected and has a rating equal to xx. Its rating is constant. There are nn accounts except hers, numbered from 11 to nn. The ii-th account’s initial rating is aiai. 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. If an account is infected, it can not be healed.

Contests are regularly held on Codeforces. In each contest, any of these nn accounts (including infected ones) can participate. Killjoy can’t participate. After each contest ratings are changed this way: each participant’s rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.

Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.

It can be proven that all accounts can be infected in some finite number of contests.Input

The first line contains a single integer tt (1≤t≤100)(1≤t≤100) — the number of test cases. The next 2t2t lines contain the descriptions of all test cases.

The first line of each test case contains two integers nn and xx (2≤n≤1032≤n≤103, −4000≤x≤4000−4000≤x≤4000) — the number of accounts on Codeforces and the rating of Killjoy’s account.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−4000≤ai≤4000)(−4000≤ai≤4000) — the ratings of other accounts.

Output

For each test case output the minimal number of contests needed to infect all accounts.

Example

input

3
2 69
68 70
6 4
4 4 4 4 4 4
9 38
-21 83 50 -59 -77 15 -71 -78 20

output

1
0
2

Note

In the first test case it’s possible to make all ratings equal to 6969. First account’s rating will increase by 11, and second account’s rating will decrease by 11, so the sum of all changes will be equal to zero.

In the second test case all accounts will be instantly infected, because all ratings (including Killjoy’s account’s rating) are equal to 44.

Solution:

Program C++:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		int n,x;
		cin>>n>>x;
		int sum=0,dem=0;
		for(int i=0;i<n;i++){
			int k;
			cin>>k;
			sum+=k;
			if(k==x) dem++;
		}
		if(dem==n) cout<<0<<endl;
		else if(sum!=x*n && dem==0) cout<<2<<endl;
		else cout<<1<<endl;
	}
}

Program Java:

import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int test_case = sc.nextInt();
		while(test_case-->0) {
			int n = sc.nextInt();
			int x = sc.nextInt();
			int []all = new int[n];
			int sum = 0;
			Set set = new HashSet();
			boolean test = false;
			for(int i=0; i<all.length; i++) {
				all[i] = sc.nextInt();
				if(all[i]==x) {
					test = true;
				}
				sum+=all[i];
				if(all[i]==x) {
					
				}
				set.add(all[i]);
			}
			if(set.contains(x)&&set.size()==1) {
				System.out.println(0);
			}else if(sum == n*x||test) {
				System.out.println(1);
			}else{
				System.out.println(2);
			}
		}
	}
}

Program Python:

for _ in range(int(input())):
	n,x=map(int,input().split())
	l=list(map(int,input().split()))
	if x in l and len(set(l))==1:
		print(0)
	elif x in l or n*x==sum(l):
		print(1)
	else:
		print(2)

Related : 

Leave a Comment