Problem Solution

1 solutions

  • 0
    @ 2024-03-29 11:11:14

    Author Code (Kamonasish Roy):

    #include<bits/stdc++.h>
    using namespace std;
    const long long M=2e5+10,MOD=2000000007;
    typedef long long ll;
    int main()
    {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
    int a,b;
    cin>>a>>b;
    int ans=0;
    while(a<b){
    if(b&1){
    b--;
    ans++;
    }
    else{
    if((b/2)<a){
    ans+=(b-a);
    break;
    }
    else{
    b/=2;
    ans++;
    }
    }
    }
    cout<<ans<<"\n";
    }
    return 0;
    }

    Tester Code (Emon Thakur) :

    #include<bits/stdc++.h>
    using namespace std;
    void solve()
    {
    int a,b; cin>>a>>b;
    int ans=0;
    int n = log2(b)+1, m = log2(a)+1;
    int x=n-m;
    int bb = b>>(x);
    if(bb<a) {bb=b>>(x-1); x--;}
    ans += (bb-a);
    for(int i=0;i<x;i++)
    {
    if((b&(1<<i))==0) ++ans;
    else ans+=2;
    }
    cout<<ans<<endl;
    }
    int main()
    {
    int t; cin>>t; while(t--) solve();
    }

  • 1

Information

ID
1044
Difficulty
7
Category
Beginners | Math Click to Show
Tags
# Submissions
78
Accepted
16
Accepted Ratio
21%
Uploaded By