-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheChildandSet.cpp
More file actions
56 lines (52 loc) · 1001 Bytes
/
TheChildandSet.cpp
File metadata and controls
56 lines (52 loc) · 1001 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
AUTHOR: Antarikshya Mitra
TIME:
PROBLEM NO:- The Child and Set
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
void solve(int sum,int limit)
{
// Calculate evens and odds in limit
int evens = limit/2;
int odds = (limit%2==1)?limit/2+1:limit/2;
vector<int> ans;
vector<int> lt;
for(int i=1;i<=limit;i=i*2)
lt.push_back(i);
reverse(lt.begin(),lt.end());
int target = 0;
for(int i=0;i<lt.size();i++)
{
// Push Possible elements and check target<=sum
for(int j=lt[i];j<=limit;j+=lt[i]*2)
{
if(target+lt[i]>sum) break;
ans.push_back(j);
target+=lt[i];
}
}
if(target!=sum)
{
cout<<-1<<endl;
return;
}
cout<<ans.size()<<endl;
for(int x:ans)
cout<<x<<" ";
cout<<endl;
return;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int sum,limit;
cin>>sum>>limit;
solve(sum,limit);
return 0;
}