#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define lld long long int
//-4%3=-1
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lld arr[61],power[61];
arr[0]=0; power[0]=1LL;
arr[1]=3; power[1]=2 ;
for(int i=2;i<=61;i++)
{
arr[i]=2*arr[i-1]+1LL;
power[i]=2*power[i-1];
}
arr[0]=1LL;
power[0]=1LL;
lld t;
cin>>t;
while(t--)
{
lld n;
cin>>n;
lld val=log2(n);
//cout<<"VAL "<<val<<"\n";
lld res=0;
res+=arr[val];
n-=power[val];
//cout<<res<<"a\n";
while(n!=0)
{
val=log2(n);
//cout<<val<<" "<<arr[val]<<" "<<power[val]<<"c\n";
res+=arr[val];
n-=power[val];
}
cout<<res<<"\n";
}
}
I have wasted a lot of time in this queestion (during and after the contest). This code fails on n=576460752303423487 When I find its log2 then it gives 59. But power[59] is giving value greater than 1. Can someone tell me my mistake?
Also do ide of cc and cf work differently?
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define lld long long int
//-4%3=-1
int main() {
// your code goes here
lld arr[61],power[61];
arr[0]=1, power[0]=1;
arr[1]=3, power[1]=2 ;
for(int i=2;i<=61;i++)
{
arr[i]=2*arr[i-1]+1;
power[i]=2*power[i-1];
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lld t;
cin>>t;
while(t--)
{
lld n;
cin>>n;
lld val=log2(n);
lld res=0;
res+=arr[val];
n-=power[val];
while(n!=0)
{
val=log2(n);
res+=arr[val];
n-=power[val];
}
cout<<res<<"\n";
}
}
This code gives correct output for sample TC in cc ide but in cf ide it gives WA. Garbage value is stored in first index of power[] and arr[]. Can someone help me?
@ssjgz @galencolin @everule1

