贪心 + 特判
2025-07-27 20:30:27
发布于:北京
1阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
#define V vector
#define all0(x) (x).begin(),(x).end()
#define all1(x) (x).begin()+1,(x).end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
typedef long long LL;
typedef pair<int, int> pi;
typedef pair<LL, LL> pl;
const int N = 3e5 + 20;
const int INF = 0x3f3f3f3f;
const LL INFLL =0x3f3f3f3f3f3f3f3f;
int a[N];
int n , k;
void so() {
for(int i = 1; i <= n ; i++ ) cin >> a[i];
int ans = 1e9;
int res;
if(k == 4){
int p = 0;
for(int i = 1; i <= n ; i++ ) {
int s = 0;
if(a[i] % 2 == 0) p++;
while(a[i] % k != 0){
a[i]++;
s++;
}
ans = min(ans , s);
}
if(p >=2){
ans = 0;
}
else ans = min(ans , 2 - p);
}
else{
for(int i = 1; i <= n ; i++ ) {
res = 0;
while(a[i] % k != 0){
a[i]++;
res++;
}
ans = min(ans , res);
}
}
cout <<ans<<endl;
}
int main() {
IOS
int tt=1;
cin >> tt;
while (tt--){
cin >> n >> k;
so();
}
}
这里空空如也
有帮助,赞一个