/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 2ms 580.0 KiB
#5 Accepted 5ms 532.0 KiB
#6 Accepted 13ms 532.0 KiB
#7 Accepted 29ms 1.754 MiB
#8 Accepted 31ms 1.523 MiB
#9 Accepted 29ms 1.77 MiB
#10 Accepted 28ms 2.27 MiB
#11 Accepted 28ms 2.02 MiB

Code

#include <bits/stdc++.h> 
using namespace std;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef vector<ll> vi;
typedef long double ld;
#define ff          first
#define ss          second
#define all(a)      a.begin(),a.end()
#define rall(a)     a.rbegin(),a.rend()
#define pb          push_back
#define mp          make_pair
#define bits(x) __builtin_popcountll(x)
#define endl "\n"
#define M 998244353
#define mod 1000000007

ll binToDec(string s) { return bitset<64>(s).to_ullong(); }
string decToBin(ll a) { return bitset<64>(a).to_string(); }

void solve() {
  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;
            }
            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";
        }
}

int main(){

    auto begin = std::chrono::high_resolution_clock::now();
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll test=1;
    // cin>>test;

    for(int i=1;i<=test;i++) {
        //cout << "Case #" << i << ": ";
        
        solve();
    }
    
	  auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";

}

Information

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