CodeForces 1883C Raspberries

题目链接:CodeForces 1883C【Raspberries】



思路

       依次枚举,特判k = 4的情况,因为k = 4可以由2个2拼凑起来,这2个2可以不在同一个元素上,如K = 4时,数组a可以为2, 3, 2, 5, 7, 9,此时数组中所有的元素乘积可以被4整除。若k = 4时,此时数组中元素没有可以拆分出2的情况时,所有的数组元素都是奇数,所以取出两个奇数,分别加1,就可以使得数组元素乘积为4的倍数。


代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
void solve() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int ans = 1e7, num = 0;
for (int i = 1; i <= n; i++) {
// 计算结果
ans = min(ans, (a[i] + k - 1) / k * k - a[i]);
if (k == 4) {
while (a[i] % 2 == 0 && num <= 1) {
a[i] /= 2;
num++;
}
}
}
if (k == 4 && num == 2) {
cout << 0 << endl;
} else if (k == 4 && num == 1) {
cout << min(ans, 1) << endl;
} else if (k == 4 && num == 0) {
cout << min(ans, 2) << endl;
} else {
cout << ans << endl;
}
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
posted @   薛定谔的AC  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示