|
| 1 | +/* Code Chef */ |
| 2 | +/* Title - XxOoRr */ |
| 3 | +/* Created By - Akash Modak */ |
| 4 | +/* Date - 12/07/2021 */ |
| 5 | + |
| 6 | +// Given an array A1,A2…AN, find the minimum number of operations (possibly zero) required to convert all integers in A to 0. |
| 7 | + |
| 8 | +// In one operation, you |
| 9 | +// choose a non-negative integer p (p≥0), |
| 10 | +// select at most K indices in the array A, and |
| 11 | +// for each selected index i, replace Ai with Ai⊕2p. Here, ⊕ denotes bitwise XOR. |
| 12 | +// Input |
| 13 | +// The first line contains an integer T - the number of test cases. Then T test cases follow. |
| 14 | +// The first line of each test case contains two integers N, K - the size of the array and the maximum number of elements you can select in an operation. |
| 15 | +// The second line of each test case contains N integers A1,A2…AN. |
| 16 | +// Output |
| 17 | +// For each test case, output the minimum number of operations to make all elements of the array 0. |
| 18 | + |
| 19 | +// Sample Input |
| 20 | +// 1 |
| 21 | +// 3 2 |
| 22 | +// 3 6 10 |
| 23 | +// Sample Output |
| 24 | +// 5 |
| 25 | + |
| 26 | +#include<bits/stdc++.h> |
| 27 | +#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); |
| 28 | +#define F first |
| 29 | +#define S second |
| 30 | +#define pb push_back |
| 31 | +#define MP make_pair |
| 32 | +#define REP(i,a,b) for (int i = a; i <= b; i++) |
| 33 | +#define FLSH fflush(stdout) |
| 34 | +#define count_1(n) __builtin_popcountll(n) |
| 35 | +#define max(x,y) (x>y)?x:y |
| 36 | +#define min(x,y) (x<y)?x:y |
| 37 | +#define mid(s,e) (s+(e-s)/2) |
| 38 | +#define mini INT_MIN |
| 39 | +#define maxi INT_MAX |
| 40 | + |
| 41 | +const int MOD = 1000000007; |
| 42 | +const int FMOD = 998244353; |
| 43 | +using namespace std; |
| 44 | + |
| 45 | +typedef long long int ll; |
| 46 | +typedef vector<int> vi; |
| 47 | +typedef pair<int,int> pi; |
| 48 | +int main() { |
| 49 | + // your code goes here |
| 50 | + fast; |
| 51 | + int t; |
| 52 | + cin>>t; |
| 53 | + while(t--){ |
| 54 | + int n,k, count, term; |
| 55 | + cin>>n>>k; |
| 56 | + vi a(n); |
| 57 | + vi setBit(31); |
| 58 | + for(int i=0;i<n;i++){ |
| 59 | + cin>>a[i]; |
| 60 | + } |
| 61 | + |
| 62 | + for(int i = 0; i <= 30; i++){ |
| 63 | + count = 0; |
| 64 | + for(int j=0;j<n;j++){ |
| 65 | + if(a[j]%2!=0){ |
| 66 | + count++; |
| 67 | + } |
| 68 | + a[j]/=2; |
| 69 | + } |
| 70 | + setBit[i] = count; |
| 71 | + } |
| 72 | + |
| 73 | + int res = 0; |
| 74 | + for(int i=0;i<=30;i++){ |
| 75 | + if(setBit[i]%k==0){ |
| 76 | + res += setBit[i]/k; |
| 77 | + }else{ |
| 78 | + res += setBit[i]/k + 1; |
| 79 | + } |
| 80 | + } |
| 81 | + cout<<res<<"\n"; |
| 82 | + } |
| 83 | + return 0; |
| 84 | +} |
0 commit comments