[Codeforces]- Help needed in Double Cola problem.

Problem Code -Double Cola

I have understood the pattern that we have there. But, I am having some difficulties in implementing it. Please help me in devising the solution to this problem.

THANKS IN ADVANCE

There is probably a more efficient way, but in this case you can simply simulate it. Start by subtracting 51, then 52, 54, 52^3 … while n does not become negative. Divide the result by the current power of two and you have the position in the initial query (0-based)

1 Like

Thanks but I figured it out and now I have done it :smiley:

Actually I was able to figure out the solution to this question. I felt like I should be sharing it here, so here it is.

//JUST LIKE ANIMALS !!!!

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
ll n,i;cin>>n;
ll c=1;
while(c*5<n){
	n-=c*5;
	c*=2;
}	
//cout<<--n/c;
ll tmp=(n-1)/c;
switch(tmp){
	case 0:cout<<"Sheldon";break;
	case 1:cout<<"Leonard";break;
	case 2:cout<<"Penny";break;
	case 3:cout<<"Rajesh";break;
	case 4:cout<<"Howard";break;
}
return 0;
}

#include <stdio.h>

int main()
{
   int N,a,i,j;
   char SName[5][10]={"Sheldon","Leonard","Penny","Rajesh","Howard"};
   while(scanf("%d",&N)!=EOF)
   {
       for(a=1,i=0;i<30;++i)
       {
            if(N>5*a)
            {
                N-=5*a;
            }
            else
            {
                for(j=0;j<5;++j)
                {
                    if(N>a)
                    {
                        N-=a;
                    }
                    else
                    {
                        puts(SName[j]);
                        break;
                    }
                }
                break;
            }
            a*=2;
        }
    }
    return 0;
}
1 Like

Nicely done @melotfi :slight_smile:

//