AshToady's blog

By AshToady, history, 4 months ago, In English

here's my code to the most annoying problem i've ever encountered(its a recent one, so you probably know it),

i'm getting 2147483647 runtime error.

Having submitted over 15/20 codes over a period of a week, and researching on it, i'm sure my algorithm is correct, have checked it innumerable times.

To simplify the logic-> its grouping numbers with matching largest bits and feeding it to calc function for further recursion till required.

what is this error 2147483647? i've faced it many times, but never really understood it, it always magically somehow gets resolved, except this time.

need help!

and if you can spot the mistake, kindly let me know.

 
 
 
 
  • Vote: I like it
  • -20
  • Vote: I do not like it

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Your code is impossible to read. If you ask for help, please clean it up.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    i hope its better now

    #include<bits/stdc++.h>
    #define ll long long
    #define fi first
    
    #define se second
    using namespace std;
    vector<ll> ans;ll kpos,k,n;
    
    void calc(vector<pair<ll,ll>>v,ll pos)
    {
    
         for(ll i=pos;i>kpos;i--)
         {   
             vector<pair<ll,ll>>v2;
            for(ll j=0;j<v.size();j++)
             {   
                 if((v[j].fi)==-1)continue;
                 
                 if((v[j].fi)&(1<<i))
                 
                 v2.push_back(v[j]),v[j].fi=-1;
             }
                if(v2.size()==1)
               {
                 
                 ans.push_back(v2[0].se);
               }
               else if(v2.size())
               {
                 
                 calc(v2,i-1);
               }
         }
         
         ll cnt=0;
     
           vector<pair<ll,ll>>pp,pp1;
            
            for(ll j=0;j<v.size();j++)
            {
                if(v[j].fi!=-1)
                {
                    if(v[j].fi!=0&&((v[j].fi)&(1ll<<kpos)))
                    pp1.push_back(v[j]);
                    else
                    pp.push_back(v[j]);
                }
            }
        
        if(pp.size())
        sort(pp.begin(),pp.end());
       
        if(pp1.size())
        sort(pp1.begin(),pp1.end());
         ll stat=1;
        for(ll i=pp.size()-1;i>-1&&stat;i--)
        {   
          ll x;
            
            if(pp[i].fi!=0)
             x=(pp[i].fi>>kpos<<kpos)+(1ll<<kpos);
            else
             x=(1ll<<kpos);
            for(ll j=kpos-1;j>-1;j--)
            {
                if((k&(1ll<<j))==0)
                {   
                    ll l,r;
                    if(pp[i].fi&(1ll<<j))
                    l=x,r=x|(1ll<<j);
                    else
                    l=x|(1ll<<j),r=x+(1ll<<(j+1));
                    ll m=lower_bound(pp1.begin(),pp1.end(),make_pair(l,-1ll))-pp1.begin();
                    
                    if(m<pp1.size()&&pp1[m].fi<r)
                    {
                        ans.push_back(pp[i].se),ans.push_back(pp1[m].se);stat=0;break;
                    }
                    
                    if((pp[i].fi&(1ll<<j))==1)
                     x|=(1ll<<j);
                }
                else
                {
                    if((pp[i].fi&(1ll<<j))==0)
                    x|=(1ll<<j);
                }
                
            }
            if(stat)
            {
                ll p=k^pp[i].fi;
                ll m=lower_bound(pp1.begin(),pp1.end(),make_pair(p,-1ll))-pp1.begin();
                if(m<pp1.size()&&pp1[m].fi==p)
                {
                    ans.push_back(pp[i].se),ans.push_back(pp1[m].se);
                    stat=0;
                    break;
                }
            }
        }
        if(stat)
        {
            if(pp1.size())
            ans.push_back(pp1[0].se);
            else if(pp.size())
            ans.push_back(pp[0].se);
        }
    }
     main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL); 
        ll t;
    
        cin>>n>>k;
        ll a[n];
        ll y=k;kpos=0;
        vector<pair<ll,ll>>v(n);
        
    while(y>0&&y!=1)
        y>>=1ll,kpos++;
        
        for(ll i=0;i<n;i++)
        {
            cin>>v[i].fi;
            v[i].se=i+1;
        }
       
        if(k)
        {   calc(v,30);
            if(ans.size()>1)
            {
            cout<<ans.size()<<endl;
            for(auto x:ans)cout<<x<<" ";
            }
            else
            cout<<-1;
        }
        else
        {   cout<<v.size()<<endl;
             for(auto y:v)cout<<y.se<<" ";
        }
        
    
        
    }