Problem Statement
You are also given an
The operation looks as follows:
- Ciel chooses four consecutive lamps such that either exactly one of them is on, or exactly one of them is off. (If there is no such group of lamps, Ciel cannot perform any more operations.)
- Ciel divides all lamps into a left part and a right part so that the division point is in the middle of the four lamps chosen in the previous step. In other words, if the four chosen lamps are x, x+1, x+2, x+3, the left part is {0, 1, ..., x+1} and the right part is {x+2, ..., n-2, n-1}.
- Ciel chooses either the left part or the right part.
- Ciel toggles all lamps in the chosen part. (Toggling a lamp means switching it to the opposite state.)
Definition
- Class:
- FourLamps
- Method:
- count
- Parameters:
- String, int
- Returns:
- long
- Method signature:
- long count(String init, int k)
- (be sure your method is public)
Constraints
- init will contain between 4 and 50 characters, inclusive.
- Each character in init will be '0' or '1'.
- k will be between 1 and 1,000,000,000, inclusive.
Examples
"0001"
2
Returns: 4
As there are only four lamps, there is only one group of four consecutive lamps. If these four lamps satisfy the constraint, the left part will be lamps {0,1} and the right part will be lamps {2,3}. If Ciel performs zero operations, the only possible final state is the initial state "0001". If Ciel performs one operation, she can either toggle the left part or the right part. Toggling the left part produces the state "1101", toggling the right part produces the state "0010". Note that both states produced by the first operation still have the property that enables us to do the next operation: exactly one of the four lamps is off in "1101", and exactly one of them is on in "0010". What states can we reach by performing two operations? Toggling the left part and then the left part again will bring us back to "0001". Toggling the right part twice has the same effect. Toggling the left part and then the right part produces the final state "1110". Toggling the right half and then the left half leads to that state as well. In total there are four possible final states: "0001", "1101", "0010", "1110".
"0001"
1000000000
Returns: 4
In the previous scenario increasing k to 10^9 does not change the answer. The following operations won't produce any new states of the lamps.
"1010"
100
Returns: 1
This time Ciel cannot make any operation because there are no four consecutive lamps with the required property. Thus, the only possible final state is the initial state.
"000111"
3
Returns: 12
These are the states we can get: "000111" "110111" "001000" "111011" "000100" "111000" "001111" "110000" "001011" "110100" "000011" "111100"
"00000010000"
5
Returns: 58
"10101100101010001011000100101000100001101"
58
Returns: 100256132848
Watch for overflow.
"0010"
1
Returns: 3
"000111"
1
Returns: 5
"1101111111111101101111111011111111"
459
Returns: 21036600
"0001000011011000001"
31
Returns: 48552
"1111111111"
11
Returns: 1
"1111"
4
Returns: 1
"111010011110101010011"
204
Returns: 151164
"11110011111"
53
Returns: 252
"0000000000000000000010000000000000000000100000"
839
Returns: 271502
"111110111111"
48
Returns: 90
"110010101100"
3
Returns: 36
"1111111111111111110111111111111111101110101"
373654854
Returns: 1498796
"10100"
945101462
Returns: 6
"11111110111111111110101101010111111111011"
13818199
Returns: 123047496
"11111111101111110011110111111111111111111111111"
305
Returns: 431106390
"1111011"
5
Returns: 20
"111110111111111101111111111111111"
1
Returns: 17
"000010011011110000001"
676411108
Returns: 184756
"001000001100000000000010010"
166
Returns: 4085950
"001000"
9
Returns: 12
"01010000010000000000010010110000010000010000110"
279
Returns: 1293252810256
"110110100011110010111110"
58
Returns: 1410314
"111100011"
32
Returns: 70
"110000010010000000000001010"
391762884
Returns: 961400
"1010101000110001101001000010010011"
317
Returns: 1131445440
"000000000000000000"
1
Returns: 1
"0111"
1
Returns: 3
"1110000"
100027819
Returns: 20
"0000000000100000000000000100100000011000000000"
1
Returns: 25
"11010011110"
21
Returns: 252
"000000000000000000000000010000000000000"
552
Returns: 1332
"111111111111111110111"
101
Returns: 342
"010000000010001001000010001000100001011000"
507
Returns: 177464757600
"110111011100011011111111011010100111011101000"
155
Returns: 1919878174380
"1111011111111011111111111111111111111111111111"
233
Returns: 271502
"1111111110111101111111110"
1
Returns: 19
"1001101010001111000000010000011010001101110100110"
946
Returns: 32247603683100
"00000000001000100000001001"
743903527
Returns: 692208
"010100110000010010000000000110"
18
Returns: 4462986
"111011111111111111111110111111111111111"
192
Returns: 132090
"00000000000000000100000000"
141731695
Returns: 552
"000000100"
31
Returns: 42
"001000100100"
7
Returns: 366
"00001000000110"
1
Returns: 11
"1011111111111001110001"
140
Returns: 251940
"0110010001000110000000100001010"
295079411
Returns: 155117520
"001000000100000001001001000"
28
Returns: 4588960
"1110001110011000111111000"
46
Returns: 1591564
"11111111101110111111001101101111111111011111"
690
Returns: 105720458160
"1000100011011100"
916127298
Returns: 4004
"011111010111000001011011010111110101111000011001"
793
Returns: 11216466014292
"0111111111111101"
53
Returns: 182
"1111111"
19
Returns: 1
"0000000000000000100001000000001"
111
Returns: 237510
"11111100111111110110"
160
Returns: 63648
"01100010000000000000000000010000000"
286
Returns: 8544096
"000100000000000000000"
24
Returns: 310
"110011100000011010000"
617487767
Returns: 184756
"111111111111111111111110111111111111111111111111"
1042
Returns: 2070
"11001010000011010101111000111000100111100100"
133
Returns: 1016140151536
"111110111111111111111111111111111111111"
93
Returns: 1332
"0110100000010111000010"
178
Returns: 251940
"11111111"
14
Returns: 1
"111111111111110111111111111111111111111"
122
Returns: 1332
"00010110000000000011"
128
Returns: 37128
"101111110111111011101111111111011111111111001110"
1
Returns: 43
"0100111000101111"
54
Returns: 6864
"1111111111101111111101111111111111111111"
194030428
Returns: 147630
"1100000010000110110000"
54
Returns: 369512
"11111111111111111111111111111111111"
377290146
Returns: 1
"000000101000100001000001001000000000010000001"
893
Returns: 73153696336
"000011110"
6
Returns: 66
"1111111111111011111111111110111111111111111"
474
Returns: 202540
"0000110110000"
12
Returns: 892
"000110011000"
1
Returns: 5
"000010000000000000000100100000000100000"
707
Returns: 77216040
"11111110111111011110"
1
Returns: 19
"1111011101111011111111"
1
Returns: 25
"11111111111111110011100111111111101111101111"
1
Returns: 25
"100000000000001100100000"
131
Returns: 341088
"110111111011111111"
128
Returns: 3640
"0000000000100000010010"
190
Returns: 31008
"1000000001000"
83
Returns: 330
"00000000000001"
42
Returns: 24
"100011111001111000101111110001110101101010100"
1
Returns: 41
"0000110101010110011001001011101010000100000"
907
Returns: 404225281200
"0000000000010001000010000000000010110011101100010"
1
Returns: 45
"101111111111011111111111111111111111111"
563
Returns: 15540
"1001"
1
Returns: 1
"000111000010010100110"
180
Returns: 151164
"0001011010010101010010"
91
Returns: 155040
"100000000000000"
44
Returns: 26
"101100100000101110000010100111001111"
61
Returns: 4425699846
"000110000000000000100111111000101111100010111001"
921
Returns: 8308493343920
"011001101111001111011101111011011100111110100111"
839
Returns: 8308493343920
"111100"
11
Returns: 12
"1111111110111101101111111111111"
1
Returns: 21
"1111111110111110111011111111101"
299
Returns: 3121560
"001100101001010100"
65
Returns: 25740
"0111010101110"
30
Returns: 660
"0000000000000000000000010"
242816223
Returns: 46
"10110111111011111111111101111111111111111011111111"
775
Returns: 3354213280
"11001100110011001100110000000000000000000000000000"
1000000000
Returns: 54771314563296
"11001100110011001100110000000000000000000000000000"
554
Returns: 54771314560872
"11001100110011001100110000000000000000000000000000"
555
Returns: 54771314561466
"00101100101010001011000100101000100001101"
58
Returns: 118691951548
"0001"
1
Returns: 3