This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP3048.cpp
59 lines (59 loc) · 1.41 KB
/
P3048.cpp
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
57
58
59
#include <iostream>
using namespace std;
int n, k, flag;
int main()
{
scanf("%d%d", &n, &k);
if (k == 1)
{
printf("%c", '1');
for (int i = 2; i <= n; i++)
printf("%c", '0');
}
else if (k == 2)
{
int s = 0, i = 1;
for (i = 1; s < n; i++)
s += i;
i--, s -= i;
putc('1', stdout);
for (int j = 1; j <= i; j++)
printf("%c", (i + 1 - n + s == j) + '0');
}
else if (k == 3)
{
int s = 0, i = 2;
for (i = 2; s < n; i++)
s += i * (i - 1) / 2;
i--, s -= i * (i - 1) / 2;
int nn = n - s, ns = 0, j = 1;
for (j = 1; ns < nn; j++)
ns += j;
j--, ns -= j;
putc('1', stdout);
for (int k = 1; k <= i - j - 1; k++)
printf("%c", '0');
putc('1', stdout);
for (int k = 1; k <= j; k++)
printf("%c", (j + 1 - nn + ns == k) + '0');
}
else
{
unsigned long long x = (1ULL << k) - 1ULL, t;
for (int i = 2; i <= n; i++)
{
t = x + (x & -x);
x = t | ((((t & -t) / (x & -x)) >> 1ULL) - 1ULL);
}
for (int i = 63; i >= 0; i--)
{
if (x & (1ULL << i))
flag = 1;
if (flag)
{
printf("%c", char(((x >> i) & 1ULL) + '0'));
}
}
printf("\n");
}
}