/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 588.0 KiB
#2 Accepted 1ms 540.0 KiB
#3 Accepted 1ms 588.0 KiB
#4 Accepted 1ms 540.0 KiB
#5 Accepted 2ms 540.0 KiB
#6 Accepted 4ms 540.0 KiB
#7 Accepted 30ms 1.91 MiB
#8 Accepted 29ms 1.582 MiB
#9 Accepted 30ms 1.859 MiB
#10 Accepted 27ms 2.27 MiB
#11 Accepted 28ms 2.02 MiB

Code

#include <iostream>
#include <algorithm>
using namespace std;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int T;
    cin >> T;
    while(T--){
        // Read N and K for the test case.
        long long N, K;
        cin >> N >> K;
        
        // If we can't even give each element one increment,
        // the AND remains 0, so answer is 0.
        if(K < N){
            cout << 0 << "\n";
            continue;
        }
        
        // Let v be the common value we want for each A[i].
        // To get every element to at least v, we need N*v operations.
        // The remaining operations: X = K - N*v.
        // And the AND becomes v.
        // So product = f(v) = v * (K - N*v).
        // We need to choose 1 <= v <= floor(K/N).
        long long max_v = K / N;
        
        // The function f(v) = K*v - N*v*v is concave and
        // its continuous maximum is at v = K / (2*N).
        long long candidate1 = K / (2 * N);
        if(candidate1 < 1) candidate1 = 1;  // ensure at least 1.
        long long candidate2 = candidate1 + 1;
        
        auto f = [&](long long v) -> long long {
            return v * (K - N * v);
        };
        
        long long ans = f(candidate1);
        if(candidate2 <= max_v){
            ans = max(ans, f(candidate2));
        }
        
        cout << ans << "\n";
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1092 Bitwise AND
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 18:49:36
Judged At
2025-02-17 18:49:36
Judged By
Score
100
Total Time
30ms
Peak Memory
2.27 MiB