mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
problems(cpp): add “1680. Concatenation of Consecutive Binary Numbers”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
aa2008b326
commit
a48dd4765e
1 changed files with 34 additions and 0 deletions
34
cpp/concatenation-of-consecutive-binary-numbers.cpp
Normal file
34
cpp/concatenation-of-consecutive-binary-numbers.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <cassert>
|
||||
|
||||
class Solution {
|
||||
static const int MOD = 1000000007;
|
||||
|
||||
public:
|
||||
int concatenatedBinary(int n)
|
||||
{
|
||||
long joined_number = 0;
|
||||
|
||||
int padding = 1;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
joined_number = ((joined_number << padding) + i) % MOD;
|
||||
|
||||
if ((i & (i + 1)) == 0) {
|
||||
padding++;
|
||||
}
|
||||
}
|
||||
|
||||
return joined_number;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Solution s;
|
||||
|
||||
assert(s.concatenatedBinary(1) == 1);
|
||||
assert(s.concatenatedBinary(2) == 6);
|
||||
assert(s.concatenatedBinary(3) == 27);
|
||||
assert(s.concatenatedBinary(12) == 505379714);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue