Problem Statement
N cells are arranged around a circle. The cells are numbered from 0 to N-1. For each i between 0 and N-2, inclusive, the i-th cell and the (i+1)-th cell are adjacent to each other. The (N-1)-th cell and the 0-th cell are adjacent to each other. Each cell has exactly two adjacent cells. Each cell has a state: "live" or "die".
Taro and Hanako can decide the states of the cells at time 0. For time t > 0, the states are determined as follows:
- Consider three cells: the i-th cell and the two cells that are adjacent to the i-th cell.
- If at least two of the three cells are "live" at time t-1, the state of the i-th cell at time t will be "live".
- If at least two of the three cells are "die" at time t-1, the state of the i-th cell at time t will be "die".
Definition
- Class:
- GameOfLifeDivOne
- Method:
- theCount
- Parameters:
- String, int, int
- Returns:
- long
- Method signature:
- long theCount(String init, int T, int K)
- (be sure your method is public)
Constraints
- init will contain between 3 and 50 characters, inclusive.
- Each character in init will be '0' (zero), '1' (one) or '?'.
- T will be between 0 and 1,000, inclusive.
- K will be between 0 and the number of characters in init, inclusive.
Examples
"0?1"
1
1
Returns: 1
There are two ways to replace '?' with '0' and '1': "001" and "011". If the state is "001" at time 0, the state will be "000" at time 1. No cell is in the "live" state. If the state is "011" at time 0, the state will be "111" at time 1. 3 cells are in the "live" state. Only the second one satisfies the condition, so the answer is 1.
"?????????"
0
1
Returns: 511
There are 512 ways to replace '?' cells with '0' or '1'. All of them except "000000000" satisfy the condition.
"??0???????"
58
6
Returns: 151
"?????????1"
47
3
Returns: 453
"??01??110?"
100
3
Returns: 29
"??????????????????????????????????????????????????"
1000
0
Returns: 1125899906842624
"010101"
10
4
Returns: 0
"10101"
2
5
Returns: 1
"?????????1???0???1??1??????0??????0??0????1???????"
12
40
Returns: 31577324313
"???"
1
0
Returns: 8
"??????????"
99
3
Returns: 823
"???"
0
2
Returns: 4
"???"
0
1
Returns: 7
"???"
1000
3
Returns: 4
"???"
1000
3
Returns: 4
"???"
607
1
Returns: 4
"???"
298
0
Returns: 8
"???"
17
0
Returns: 8
"???"
8
3
Returns: 4
"???"
1
3
Returns: 4
"???"
7
3
Returns: 4
"?????????????????????????????????????????????????"
0
23
Returns: 403023636746956
"?????????????????????????????????????????????????"
0
5
Returns: 562949953189786
"?????????????????????????????????????????????????"
1000
26
Returns: 247145497139164
"?????????????????????????????????????????????????"
1000
8
Returns: 561082903169202
"?????????????????????????????????????????????????"
862
13
Returns: 545777839940362
"?????????????????????????????????????????????????"
895
37
Returns: 17172113480950
"?????????????????????????????????????????????????"
2
22
Returns: 385287847999258
"?????????????????????????????????????????????????"
20
28
Returns: 181542024838938
"?????????????????????????????????????????????????"
17
6
Returns: 562358668473190
"?????????????????????????????????????????????????"
16
45
Returns: 303377597195
"??????????????????????????????????????????????????"
0
3
Returns: 1125899906841348
"??????????????????????????????????????????????????"
0
15
Returns: 1124435014542268
"??????????????????????????????????????????????????"
1000
5
Returns: 1125396592633103
"??????????????????????????????????????????????????"
1000
31
Returns: 226224770913436
"??????????????????????????????????????????????????"
786
0
Returns: 1125899906842624
"??????????????????????????????????????????????????"
236
6
Returns: 1124912962780553
"??????????????????????????????????????????????????"
2
22
Returns: 799951559419648
"??????????????????????????????????????????????????"
21
21
Returns: 848229259540648
"??????????????????????????????????????????????????"
9
37
Returns: 42829121309326
"??????????????????????????????????????????????????"
11
32
Returns: 180812122173236
"?????1??111???????11??????????11?????1???????????1"
0
10
Returns: 1099511627776
"?11?111111111111??1?111?1111?1111111?11111?1111?11"
0
18
Returns: 1024
"???0??0???0??0??????????0??????????????0??????0???"
160
13
Returns: 7601054837275
"?1?111111??1??11????11111?11?1?11?111?11111111???1"
273
28
Returns: 262144
"??1???111??11??11??1??1?1???11??1??11????11??1????"
16
27
Returns: 2141616800
"??????????11?11?1?111??1??1?1?1???1???111?1?????11"
21
23
Returns: 2147154784
"??0000?????0??0000000?00???00?000000?000?00?0000?0"
5
20
Returns: 0
"0000000000000000?00?000?00000000000000000000000000"
15
7
Returns: 0
"???????????????????????????????1??????0???????0???"
0
32
Returns: 2813768603466
"??????1???????????10???????0??????????????1???1??0"
0
9
Returns: 8796092885510
"0??1010?01?10?0?01?10?01??010?01?1010?01?1???10101"
408
36
Returns: 10519
"0?0?0???0???0??1??????????010??1???10?0????101????"
677
47
Returns: 943319
"0?????01?101?10?0?010??101??010?0?0?????0?01???101"
11
46
Returns: 7097
"?1????0??1????????0???01????01??0101??0?0??1?1???1"
24
1
Returns: 8589662593
"?????10?0?0?0101???1???1?1?1??0??101?1?????1??01??"
10
40
Returns: 45611576
"??0??????1???1??????????????010???0??1????????????"
3
29
Returns: 1172949674850
"?111001010??100???10????00?11??1?010?011??0??10??0"
0
13
Returns: 2097152
"0101010101001?101010???1010010101010101010101?1011"
0
40
Returns: 0
"????1???10?0?1????1????1??????1????01????????0?0??"
740
40
Returns: 4873914135
"0100010110101?0101001010110101101010?010?010101?11"
499
42
Returns: 0
"?000?0??000???0??1?1110?0??????????0?00???0?000?0?"
15
31
Returns: 40153
"???????????10??01????01???00???0??????????????0??0"
5
18
Returns: 381708923992
"111111111111000111111011?1111100111111111100000011"
21
39
Returns: 2
"????????????????????????????????1?????????????????"
20
25
Returns: 327634393915761
"????11?????1??0?????0????????????0?00???00????????"
0
9
Returns: 1099510867677
"????0????????0????????????1???????????????????????"
0
48
Returns: 1
"1??01?01?1010010100101?1010???01?1?1?1?10??1?10?01"
619
36
Returns: 3616
"00?0?101??11?0100?0?101011100111??1?0???0000?110?0"
231
18
Returns: 31436
"11111000000011011001011110010001101001001111110111"
2
3
Returns: 1
"010?01??0?00???010????10??0101?1?1?100?010??101010"
19
16
Returns: 296602
"????????????????????????????????????????1?????????"
2
15
Returns: 544202858929478
"01??11111??1??0?110110011??011?01001?1??1?010?????"
3
45
Returns: 432
"???101???0?0?1??111?0?0???????1111?0?0?1100110??00"
0
30
Returns: 1271626
"?????????0???0?0??0?????10??????????00????????????"
0
28
Returns: 194458630304
"??001?01??1?1??0????0?????010??1??1?1???110?01?1?1"
572
40
Returns: 7097773
"101??1010?101101?001?111001?1?0?01011??00?01??0?10"
486
45
Returns: 0
"1011010010101011110?101101010101010101010101001010"
13
5
Returns: 2
"01???1?01?10?0010010?101?1010100?00101?0?010?010?1"
5
39
Returns: 0
"001?100??111?0101?101?1011??001??11?10???0??001?0?"
25
3
Returns: 262144
"?01?110??1?01010101010?0101010?010?1?10??1010??110"
11
28
Returns: 2460
"???????1???"
0
6
Returns: 638
"?0?0??0?00????00???????00?0????????"
0
5
Returns: 33539156
"?????1?111?11????1???111?1?11????1?11??1??????11?"
385
26
Returns: 1073260960
"1111111111111111?111111111111111?111111"
867
22
Returns: 4
"1?1?11111?1??1??1?????"
11
1
Returns: 4096
"?00?0?00000000?000000?0000?0000000??00"
5
24
Returns: 0
"1?1111?1111?111?111111111"
6
1
Returns: 16
"?111111111111?11111111111"
12
0
Returns: 4
"01????01010?0??1"
0
2
Returns: 128
"010?0??1????0?0101010?01?10101?1"
0
13
Returns: 1981
"1010101010101010?010?010101010101010101010101"
589
39
Returns: 1
"?101?1?101??????01??01?????10???????0???????0??"
24
12
Returns: 4227743764
"0?01010?0?01?1?101010101?101?10"
15
27
Returns: 0
"????0???0?0"
2
4
Returns: 101
"??01??0?0101?1?1??01010?0??101010?010?01?1?1"
16
28
Returns: 11285
"????????????0?????????0????????????"
4
14
Returns: 5714341674
"00?0101?101010????10101010??11?1?100?0101010101010"
0
19
Returns: 2048
"?????0?0??0?????0?11??0?"
0
5
Returns: 130918
"??00"
146
0
Returns: 4
"????0??1?"
87
9
Returns: 16
"1?1?11?11?1?0?1?1?1??11?????0??0?0????0?1???1"
5
21
Returns: 66180976
"?????????0"
3
10
Returns: 29
"0??0?000?00000000???0?00?100????0?0000?00?1110??00"
6
48
Returns: 0
"0?0?????1??10???0?00?10??00??10??0??001?0101???"
7
15
Returns: 22159325
"010101?10101?01010101010101010"
0
1
Returns: 4
"????0??????1??0"
0
10
Returns: 299
"1??11111?11???11?11?1?1110?111?111110???1??1???11"
811
25
Returns: 524288
"??????????1?????1???????????????????"
890
4
Returns: 17160197364
"001"
0
0
Returns: 1
"?01?11?011000?0?01?0111?0????1111101?0?1?0000000"
3
13
Returns: 16384
"1??0?0?0?0??0???1???????10???????01?0?"
4
17
Returns: 27382912
"1?1111?01"
0
9
Returns: 0
"10?????111010"
0
12
Returns: 0
"00?0?1?111001??000?001010??01??111??1?0?1110????"
0
4
Returns: 262144
"0010100010101010?00?0101?100?0"
339
29
Returns: 0
"111000??1??001?00?00000?1?"
263
25
Returns: 0
"010101?1?1??010"
1
10
Returns: 2
"?1010?10?01010101010101010100101011010101010100100"
19
20
Returns: 2
"?1????????"
4
0
Returns: 512
"10100?????10??????10"
0
16
Returns: 0