-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1833C.cpp
More file actions
86 lines (72 loc) · 2.13 KB
/
Copy path1833C.cpp
File metadata and controls
86 lines (72 loc) · 2.13 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define umap unordered_map
#define f(i, x, n) for(ll i = x; i < n; i++)
#define endl '\n'
#define optimize() ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int main() {
optimize();
ll t;
cin >> t;
while (t--) {
ll n, k;
cin >> n >> k;
ll arr[n];
bool okay = false;
f(i, 0, n) {
cin >> arr[i];
if (arr[i]%k==0) {
okay = true;
}
}
if (n==1) {
if (k==2||k==3||k==5) {
cout << k-arr[0] << endl;
} else {
if (arr[0] < k) {
cout << k-arr[0] << endl;
} else if (arr[0]==k) {
cout << 0 << endl;
} else {
cout << k-(arr[0]%k) << endl;
}
}
} else {
if (okay) {
cout << 0 << endl;
} else {
if (k==2 || k==3 || k==5) {
ll ans = LLONG_MAX;
f(i, 0, n) {
ll gap = k-(arr[i]%k);
if (gap < ans) {
ans = gap;
}
}
cout << ans << endl;
} else {
bool ev_gaps[n];
int four_gaps[n];
f (i, 0, n) {
if (arr[i]%2==0) {
ev_gaps[i] = false;
} else {
ev_gaps[i] = true;
}
if (arr[i] % 4 == 0) {
four_gaps[i]=0;
} else {
four_gaps[i] = 4-(arr[i]%4);
}
}
sort(four_gaps, four_gaps+n);
sort(ev_gaps, ev_gaps+n);
cout << min(four_gaps[0], ev_gaps[0]+ev_gaps[1]) << endl;
}
}
}
}
}