Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
4
Indexable
#include <iostream>
#include <algorithm>
#include <vector>
#define ll long long
#define sorted(x) sort(x.begin(),x.end())
#define fastIO ios::sync_with_stdio(0); cin.tie(0)
using namespace std;
 
int main(){
    fastIO;
    int t; cin>>t;
    while(t--){
        int n, k;
        cin>>n>>k;
        int ans= 1e9;
        if(k!=4){
            for(int i=0,x; i<n; i++){
                cin>>x;
                if(x%k) ans= min(ans, k-x%k);
                else ans= 0;
            }
        }else{
            vector<int> vec;
            for(int i=0,x; i<n; i++){
                cin>>x; vec.push_back(x);
            }
            int even= 0, odd= 0, minDiff= 1e9;
            for(auto i: vec){
                if(i%4==0){ans= 0; break;}
                if(i%2==0) even++;
                else{
                    minDiff= min(minDiff, 4-i%4);
                    odd++;
                }
            }
            if(ans!=0){
                if(even>=2) ans= 0;
                else if(even==1 && odd>0) ans= 1;
                else ans= min(minDiff, 2);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}