https://codeforces.com/problemset/problem/535/B
please help me in this question, i am not getting the approach, i read the editorial also but didn’t understand anything
https://codeforces.com/problemset/problem/535/B
please help me in this question, i am not getting the approach, i read the editorial also but didn’t understand anything
I’ll explain how I solved it.
If you write down the numbers in order,
4, 7, 44, 47, 74, 77, 444, 447, ...
As there are only 2 numbers, we try to replace them by 1 and 0 and see that they look like binary numbers
0, 1, 00, 01, 10, 11, 000, 001, ...
But this makes no sense, as (0 and 00 and 000 … are all same), similarly (1 and 01 and 001 …).
We find that zero is spoiling it, so we use 1 and 2 instead of 0 and 1.
1, 2, 11, 12, 21, 22, 111, 112, ...
position of 1 = 1 * 1 = 1
position of 2 = 2 * 1 = 2
position of 11 = 1 * 1 + 1 * 2 = 3
position of 12 = 2 * 1 + 1 * 2 = 4
position of 21 = 1 * 1 + 2 * 2 = 5
position of 22 = 2 * 1 + 2 * 2 = 6
position of 111 = 1 * 1 + 1 * 2 + 1 * 4 = 7
position of 112 = 2 * 1 + 1 * 2 + 1 * 4 = 8
Now you can easily see that they are exactly equal to their positions in base 2.
Therefore the code can be just,
int n;
cin >> n;
int ans = 0;
int ctr = 1;
int val;
while (n)
{
int d = n % 10;
n /= 10;
if (d == 4)
val = 1;
else
val = 2;
ans += (ctr * val);
ctr <<= 1;
}
cout << ans;
what is meant by position on rank 2… and in code what is the meaning of cin >> n; and ctr <<= 1;
I don’t get your doubt.
‘cin >> n’ is input in C++.
ctr <<= 1; is the left shift (<<) operator in C++.
ctr <<= 1;
// same as ctr *= 2;