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 @ 2024-07-28 11:14  薛定谔的AC  阅读(3)  评论(0编辑  收藏  举报