One thing I noticed is, In Line 17, Newline is missing at the end.
Can someone tell me what is wrong with my code?
I have used long long int and also handled c==0.
Thank you so much
Thank You so much
Just typecast
using namespace std;
#define ll long long int
#define MOD 1000000007
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
void solve()
{
ll c;
cin >> c;
if (c == 0)
{
cout << 2 << endl;
cout << "1 1" << endl;
return;
}
int a[60] = {0};
int i = 1;
while (c > 0)
{
a[60 - i] = c % 2;
c = c / 2;
i++;
}
int prev = 0;
vector<ll> ans;
for (int i = 0; i < 60; i++)
{
if (prev == 0)
{
if (a[i] == 1)
{
ans.pb((unsigned long long int)pow(2, (60 - i)) - 1);
prev = 1;
}
}
else
{
if (a[i] == 0)
{
ans.pb((unsigned long long int)pow(2, (60 - i)) - 1);
prev = 0;
}
}
}
cout << ans.size() << endl;
for (auto &x : ans)
cout << x << " ";
cout << endl;
}
int main()
{
fast;
int t;
//t=1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
Thanks man!
apparently a long long took its toll… can someone explain how different 1ll<<n is to 1<<n . or give reference to any article
Please tell what’s wrong with the code. It is only passing one of the three test cases.
My solution : CodeChef: Practical coding for everyone
Can you please post your code also, that will be a great help for me.
i didnt understand editorial can anyone explain why using this approach any proof?
It is there in the first line
Just typecast your value returned by pow
#include <bits/stdc++.h>
using namespace std;
string binary(long long n)
{
string s = "";
if (n == 0)
{
s += '0';
return s;
}
while (n > 0)
{
long long k = n % 2;
s += to_string(k);
n = n / 2;
// cout << k << s[i] << endl;
}
// s[i] = '\0';
// reverse(s.begin(), s.end());
return s;
}
int main()
{
int t;
cin >> t;
while (t--)
{
long long n;
cin >> n;
string str = binary(n);
// cout << str << endl;
vector<long long> v;
if (str.size() == 1 and str[0] == '0')
{
v.push_back(1);
v.push_back(1);
}
else
{
int f = 1;
for (int i = str.size() - 1; i >= 0; i--)
{
char k = f + '0';
if (str[i] == k)
{
long long y = (long long int)pow(2, i + 1) - 1;
v.push_back(y);
f = 1 - f;
}
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
return 0;
}
I have done exactly the same.
I got the approach but can someone please explain that how is the size of the array minimum using this algo?
can anyone help me with my code ,
I found a pattern , and it works for each case , cant find anyone that fails .
Please help . Here is code
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
// your code goes here
ll t;
cin>>t;
while(t–)
{
ll c;
cin>>c;
ll twos = 2;
ll temp = twos-1;
while(temp<c)
{
twos = twos*2;
temp = twos - 1;
}
// cout<<temp<<"\n";
ll temp2 = temp - c;
if(temp2>0)
{
cout<<2<<"\n";
cout<<temp2<<" "<<temp<<"\n";
}
else
{
cout<<1<<"\n";
cout<<temp<<"\n";
}
}
return 0;
}
Consider the following input.
Input
1
5
Your Output
2
2 7
A_0 viz., 2 doesn’t satisfy the condition A_i = 2^x - 1.
Can someone help, I can’t figure out which case am I missing.
Solution: 52365564 | CodeChef
for _ in range(int(input())):
n = int(input())
if n==0:
ans += "1\n1 1\n"
continue
if (n+1)&n==0:
ans += str(1)+"\n" + str(n)+"\n"
continue
num = bin(n)[2:]
l = len(num)
res = []
cnt = 1
for i in range(len(num)):
if num[i]=="1" and (cnt)%2!=0:
res.append(int('1'*(l-i), 2))
cnt+=1
elif num[i]=="0" and cnt%2==0:
res.append(int('1'*(l-i), 2))
cnt+=1
elif num[i]=='0' and cnt%2!=0:
continue
elif num[i]=='1' and cnt%2==0:
continue
ans += str(len(res)) + "\n"
for i in res:
ans += str(i) + " "
ans += "\n"
print(ans)```