|
| 1 | +package p708; |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +public class CF708B { |
| 7 | + private static final String IMPOSSIBLE = "Impossible"; |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8); |
| 11 | + int a00 = scanner.nextInt(); |
| 12 | + int a01 = scanner.nextInt(); |
| 13 | + int a10 = scanner.nextInt(); |
| 14 | + int a11 = scanner.nextInt(); |
| 15 | + System.out.println(solve(a00, a01, a10, a11)); |
| 16 | + } |
| 17 | + |
| 18 | + private static String solve(int a00, int a01, int a10, int a11) { |
| 19 | + // 根据 f(0,0) 可以求出 0 的个数 c0,f(0,0) = C(c0, 2) |
| 20 | + int cnt0 = f(a00); |
| 21 | + int cnt1 = f(a11); |
| 22 | + if (cnt0 < 0 || cnt1 < 0) { |
| 23 | + return IMPOSSIBLE; |
| 24 | + } |
| 25 | + |
| 26 | + if (a01 == 0 && a10 == 0) { |
| 27 | + if (a00 > 0 && a11 > 0) { |
| 28 | + return IMPOSSIBLE; |
| 29 | + } else if (a11 == 0) { |
| 30 | + return "0".repeat(cnt0); |
| 31 | + } else { |
| 32 | + return "1".repeat(cnt1); |
| 33 | + } |
| 34 | + } |
| 35 | + if (a01 + a10 != cnt0 * cnt1) { |
| 36 | + return IMPOSSIBLE; |
| 37 | + } |
| 38 | + |
| 39 | + int left1 = a10 / cnt0; |
| 40 | + int right0 = a10 % cnt0; |
| 41 | + String res = "1".repeat(left1) + "0".repeat(cnt0 - right0); |
| 42 | + if (right0 > 0) { |
| 43 | + res += "1" + "0".repeat(right0); |
| 44 | + cnt1--; |
| 45 | + } |
| 46 | + res += "1".repeat(cnt1 - left1); |
| 47 | + return res; |
| 48 | + } |
| 49 | + |
| 50 | + // a·x^2 + b·x + c = 0 一元二次方程求根公式 (-b ± sqrt(b^2 - 4ac)) / 2a |
| 51 | + // x(x-1)/2 = a |
| 52 | + // x = (1 + sqrt(1 + 8a)) / 2 |
| 53 | + private static int f(long a) { |
| 54 | + int x = (int) (1 + Math.sqrt(a * 8 + 1.0) / 2); |
| 55 | + if (x * (x - 1L) / 2 == a) { |
| 56 | + return x; |
| 57 | + } |
| 58 | + return -1; |
| 59 | + } |
| 60 | +} |
| 61 | +/* |
| 62 | +B. Recover the String |
| 63 | +https://codeforces.com/contest/708/problem/B |
| 64 | +
|
| 65 | +灵茶の试炼 2023-02-24 |
| 66 | +题目大意: |
| 67 | +对于 01 字符串 s,定义 f(x,y) 表示子序列 [x,y] 在 s 中的出现次数。 |
| 68 | +输入 f(0,0), f(0,1), f(1,0) 和 f(1,1),范围在 [0,1e9]。 |
| 69 | +请构造任意一个满足输入的非空字符串 s。 |
| 70 | +如果不存在,输出 Impossible。 |
| 71 | +注:子序列是从 s 中删除某些元素得到的。 |
| 72 | +
|
| 73 | +constructive algorithms |
| 74 | +https://codeforces.com/contest/708/submission/194669010 |
| 75 | +提示 1:根据 f(0,0) 可以求出 0 的个数 c0,因为 f(0,0) = C(c0,2);同理可求出 1 的个数 c1。 |
| 76 | +提示 2:f(0,1) + f(1,0) = c0 * c1 |
| 77 | +====== |
| 78 | +
|
| 79 | +input |
| 80 | +1 2 3 4 |
| 81 | +output |
| 82 | +Impossible |
| 83 | +
|
| 84 | +input |
| 85 | +1 2 2 1 |
| 86 | +output |
| 87 | +0110 |
| 88 | + */ |
0 commit comments