Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×
Изменения рейтингов за последние раунды временно удалены. Скоро они будут возвращены. ×

Блог пользователя aryan_singhal

Автор aryan_singhal, история, 8 месяцев назад, По-английски

Hello fellow developers! I'm relatively new to programming and currently navigating the challenging terrain of Codeforces. I've encountered a perplexing situation while implementing a binary search-based solution for a problem. Despite having a seemingly correct logic, one implementation gets accepted, while the other faces rejection.

I've distilled the issue to a pair of code snippets that encapsulate the identical logic but differ in syntax. Both attempts are centered around a binary search strategy, aiming to crack the problem at hand.

Problem statement: 689C - Mike and Chocolate Thieves

Rejected submission: 244815151 Accepted submission: 244814697

Snippet Comparison: For clarity, I've condensed the snippets to highlight the differences. Your keen eyes might spot the elusive bug that's eluding my scrutiny.

Rejected Code

bool isPos(ll mid, ll m){
    ll ways = 0;
    for(ll k = 2; k * k * k <= mid; k++){
        ways += (mid/(k*k*k));
    }
 
    if(ways >= m){
        return true;
    }else{
        return false;
    }
}
void build(){
    ll m;cin >> m;
 
    ll low = 1;ll high = 1e18;
 
    ll ans = -1;
    while(low <= high){
        ll mid = low + (high - low)/2;
        if(isPos(mid, m)){
            ans = mid;
            high = mid - 1;
        }else{
            low = mid + 1;
        }
    }
 
    if(!isPos(ans, m)){
        cout << -1 << endl;
        return;
    }
 
    cout << ans << endl;
 
}

Accepted Code

ll isPos(ll mid, ll m){
    ll ways = 0;
    for(ll k = 2; k * k * k <= mid; k++){
        ways += (mid/(k*k*k));
    }
 
    return ways;
}
void build(){
    ll m;cin >> m;
 
    ll low = 1;ll high = 1e18;
 
    ll ans = -1;
    while(low <= high){
        ll mid = low + (high - low)/2;
        if(isPos(mid, m) >= m){
            ans = mid;
            high = mid - 1;
        }else{
            low = mid + 1;
        }
    }
 
    if(isPos(ans, m) != m){
        cout << -1 << endl;
        return;
    }
 
    cout << ans << endl;
 
}

Now, as I'm still in the learning phase, I'm reaching out to the experienced members of our programming community for guidance. Could there be some crucial nuances in binary search that I might be overlooking? Or is there a subtle error in my code that's slipping through the cracks?

Happy coding!

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
8 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Check the case when isPos(ans, m) returns m+1

  • »
    »
    8 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Oh yes, got it. My rejected code was checking ways < m while accepted is checking ways != m. Thankyou so much!

»
8 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

chatgpt

  • »
    »
    8 месяцев назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Ah yes, I have used chatGPT to enhance english but the code and content solely belongs to me. My motive to write this post was to seek help finding bug in my code. Just to attract fellow developers I had to enhance english. Apologies if I have hurt any sentiments.