-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathP375.java
58 lines (49 loc) · 1.76 KB
/
P375.java
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
/*
375. Amplifiers
Time limit per test: 1.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard
Scientist Shurik needs voltage that is N times more that the standard voltage
in the wall outlet for power supply for his time machine. The standard voltage
is equal to one Bervolt. Shurik decided to use voltage amplifiers. In the nearby
shop he found the amplifiers of two types, the first type creates voltage 2X-1
Bervolt from X Bervolt, the second one creates voltage 2X+1 Bervolt from X
Bervolt. The number of amplifiers in the shop is unlimited. Shurik wants to
build a sequence of amplifiers from the outlet to the time machine. Of course
he wants to minimize the number of amplifiers. Help him.
Input
A single integer 1 ¡Ü N¡Ü 2¡¤ 109.
Output
If it is possible to make such scheme, output in the first line the minimal
possible number of amplifiers. The second line in this case is to contain the
sequence of amplifiers from the outlet to the time machine. Use number 1 for
the first-type amplifiers and number 2 for second-type amplifiers.
If there is no solution, output "No solution" (without quotes).
Example(s)
sample input
sample output
5
2
2 1
*
*/
import java.util.*;
public class P375 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n % 2 == 0)
System.out.println("No solution");
else {
String res = "";
while(n > 1) {
res += (n / 2 % 2 == 1) ? "2" : "1";
n = (n / 2 % 2 == 1) ? n / 2 : (n / 2 + 1);
}
System.out.println(res.length());
for(int i = res.length() - 1; i >= 0; i--)
System.out.print(res.charAt(i) + " ");
}
}
}