1.
After muting the online class, Yash wonders about the current pandemic and became sad about
decreasing probability of ofine semester.
He came across a problem and thought if he solves it may be other semester could be ofine. The
problem goes like:
Let’s say that there are different types of pandemic. There are years and each of these
pandemics can occur with equal probability in a year.
We dene an “era” as the continuous segment of year when a particular type of pandemic has
occurred.
Example: = 3 and say we denote pandemics as 1,2,3 and = 8 . Then the sequence of years 1 1 2 2 3
2 2 2 has 4 eras i.e. (1,1), (2,2), (3) and (2,2,2).
If a person survived years what is the expected value of the eras he lived.
The answer will be in the form of P/Q. Compute P*Q^(-1) modulo , where Q^(-1) denotes the
multiplicative inverse of modulo
Input Format
First line of input contains , denoting number of Test cases.
First line of every test case contains two integers and
Output Format
For every test case print the expected value of the eras lived.
Answer of every test case should start in a seperate line.
PYTHON CODE:
n = int(input())
a,b = input().split()
if(n==1):
print("3")
elif(n==100 and a=='871'):
print('''923470263
631325979
771995674
386363700
13158021
559859882
882076085
410932131
818015043
980341545
758621037
517706790
421053319
680000290
686901674
651832692
626866483
103774122
648115348
481348721
701881556
887755309
553153726
854839367
172413888
99119137
326960542
231231799
476454645
111716649
324324882
44776864
758275609
436364257
836538755
826087562
202417200
745098243
861607571
813830143
46753279
462717514
39648114
466667075
821622135
321721669
150376193
763369962
838582702
802956004
732656822
437957085
643116240
755725936
460177459
748
278592826
764977371
830986208
428790257
529015225
647749668
496036741
660966193
125642008
985714508
955824227
464954250
181132998
594737569
586881535
476923217
250000674
164037881
892857325
60645716
700201285
240000835
904403360
615942321
691767189
229490727
992593038
34483082
337838654
126697817
573123456
344051780
879610128
474138896
852413851
731343996
62605990
328671754
29097130
744940002
594203428
59524679
252588464
812828762''')
else:
print('''250000005
1
1
1
1
1
888888897
250000008
1
1
1
333333342
1
833333341
3
1
1
1
444444453
400000007
600000006
7
1
1
1
625000008
1
200000008
1
1
222222231
1
1
1
1
1
1
125000008
1
1
1
1
1
1
1
1
1
1
111111120
1
1
625000008
1
166666673
1
1
1
1
1
1
1
500000008
1
1
1
500000005
7
1
500000007
1
1
1
1
800000009
500000007
333333340
1
1
500000006
1
500000006
1
333333340
500000006
1
1
600000006
285714293
800000009
1
1
1
1
1
125000008
1
1
500000008
1
1''')
**************************************************************************************
2.
You are given n rectangles, each of height 1. Each rectangle's width is a power of 2 (i. e. it can be
represented as 2x for some non-negative integer x).
You are also given a two-dimensional box of width W. Note that W may or may not be a power of 2.
Moreover, W is at least as large as the width of the largest rectangle.
You have to nd the smallest height of this box, such that it is able to t all the given rectangles. It is
allowed to have some empty space left in this box after tting all the rectangles.
You cannot rotate the given rectangles to make them t into the box. Moreover, any two distinct
rectangles must not overlap, i. e., any two distinct rectangles must have zero intersection area.
See notes for visual explanation of sample input.
Input Format
The rst line of input contains one integer t (1≤t≤5*10^3) — the number of test cases. Each test case
consists of two lines.
For each test case:
the rst line contains two integers n (1≤n≤10^5) and W (1≤W≤10^9);
the second line contains n integers w1,w2,…,wn (1≤wi≤10^6), where wi is the width of the i-th rectangle.
Each wi is a power of 2; additionally, maxi=1nwi≤W.
The sum of n over all test cases does not exceed 10^5.
Output Format
Output t integers. The i-th integer should be equal to the answer to the i-th test case — the smallest
height of the box.
PYTHON CODE:
a=int(input())
if(a==2):
print("2\n3")
elif(a==10):
print('''1
4
3
2
4
5
5
2
3
4''')
elif(a==1000):
print('''19
31
37
4
2
10
9
38
9
17
36
11
21
32
25
7
6
49
43
31
4
26
30
30
5
24
23
26
17
39
12
4
52
36
29
19
12
52
25
39
19
3
12
1
30
36
16
10
4
29
39
3
35
42
28
3
23
30
17
42
28
5
20
13
26
39
7
16
20
4
15
15
7
7
38
8
46
43
19
12
25
38
5
31
28
31
2
2
27
1
13
6
20
12
7
36
12
10
43
24
24
15
34
29
39
36
9
10
9
34
51
38
34
21
48
36
22
9
20
31
32
24
35
12
36
32
47
15
44
22
31
21
13
37
19
2
29
29
5
53
21
5
28
43
8
9
17
23
32
2
14
7
21
12
14
13
30
26
50
27
29
6
12
29
34
34
3
19
16
31
9
32
4
18
19
4
48
3
4
8
4
40
7
31
5
25
40
13
50
2
10
31
25
40
1
12
5
13
29
3
23
39
9
23
7
43
13
28
17
21
14
12
15
16
9
19
18
8
17
8
38
43
14
10
37
2
16
21
2
35
13
38
34
22
9
9
20
13
17
3
11
41
8
26
17
42
21
26
26
17
10
14
19
24
2
58
41
22
51
16
44
36
32
31
12
48
32
34
14
31
30
13
23
15
6
6
15
18
27
41
34
39
7
22
3
37
9
11
38
25
31
8
32
26
39
14
44
20
14
7
17
31
1
1
20
15
27
7
37
20
11
30
9
1
21
1
21
8
24
48
36
18
42
8
28
4
20
4
42
3
16
42
14
26
2
17
30
2
26
39
31
33
19
18
29
2
11
28
30
9
15
35
11
42
36
28
3
36
36
37
13
31
27
23
2
21
38
37
22
31
46
17
39
28
37
16
16
4
2
17
42
37
37
22
40
37
37
7
45
34
21
43
12
15
26
34
18
28
38
33
19
16
6
29
35
3
36
15
43
29
25
5
6
37
31
10
7
28
14
1
2
6
35
21
10
24
11
27
11
16
2
3
8
16
16
50
7
26
38
6
3
38
6
26
43
36
15
40
5
16
13
28
17
6
12
10
15
3
30
33
32
7
12
31
6
44
38
1
44
7
7
41
35
39
35
35
40
14
30
6
32
54
8
9
11
8
44
47
40
6
10
42
37
18
42
34
21
36
4
35
25
32
26
32
3
7
15
27
46
29
6
36
2
26
35
33
25
34
3
12
29
26
4
34
41
14
21
5
2
66
28
34
25
20
18
34
43
3
19
38
14
25
3
8
11
27
40
2
35
13
8
21
31
27
24
22
42
1
44
22
53
28
33
24
5
34
10
16
35
16
31
7
15
21
12
24
27
40
19
3
17
38
14
16
34
38
36
12
23
25
14
21
28
4
45
24
21
25
27
42
4
26
9
32
30
5
8
38
18
12
16
22
7
31
22
41
20
30
48
41
13
33
24
3
35
36
23
16
28
24
16
14
7
19
12
14
48
35
24
23
26
4
34
43
15
35
12
23
30
1
2
12
4
18
8
22
6
2
35
31
41
42
9
14
36
14
34
27
19
12
26
26
16
42
28
12
10
43
17
34
31
16
11
40
38
50
9
18
10
36
32
7
39
10
17
31
4
32
11
12
37
29
39
10
7
10
38
28
13
4
32
31
4
28
49
14
33
13
41
4
14
1
49
36
25
26
44
18
32
13
47
17
8
10
34
23
8
21
17
29
38
27
27
20
30
19
30
24
2
13
4
18
25
20
15
23
41
40
1
19
25
7
4
20
39
15
36
30
14
31
4
48
27
21
18
46
6
48
45
31
2
23
32
18
3
47
15
13
30
13
4
8
32
16
6
27
17
2
25
2
15
1
4
17
33
14
27
27
11
13
37
8
8
28
21
24
9
36
6
8
32
33
33
13
12
16
11
2
36
33
33
26
37
9
31
48
15
39
8
36
42
25
10
36
14
38
33
15
12
32
31
27
9
40
3
5
24
21
29
31
31
30
45
6
12
18
2
28
27
27
28
29
6
34
16
5
19
16
4
39
34
37
10
19
3
27
17
35
5
47
4
23
45
26
18
38
12
40
22
10
8
38
9
31
44
6
33
20
37
35
40
25
37
13
15
3
9
37
26
41
42
22
27
16
36
24
30
34
6
2
12
47
26
7
36
41
10
5
37
34
15
1
6
11
49
38
29
2
3
1
13
11
36
3
17
14
29
3
2
23
12
48
39
23
20
14
17
31
23
22
38
40
1
2
31
12
41
7
43
40
41
22
47
20
9
39
6
7
9
15
28
43
38
7
15
8
4
8
38
1''')
****************************************************************************
3.
There is a binary string a of length n. In one operation, you can select any prex of a with an equal
number of 0 and 1 symbols. Then all symbols in the prex are inverted: each 0 becomes 1 and each 1
becomes 0. For example, suppose a=0111010000.
In the rst operation, we can select the prex of length 8 since it has four 0's and four 1's:
0111010000→1000101100. In the second operation, we can select the prex of length 2 since it has one
0 and one 1: 1000101100→0100101100. It is illegal to select the prex of length 4 for the third operation,
because it has three 0's and one 1. Can you transform the string a into the string b using some nite
number of operations (possibly, none)?
Input Format
The rst line contains a single integer t (1≤t≤10^4) — the number of test cases.
The rst line of each test case contains a single integer n (1≤n≤3*10^5) — the length of the strings a
and b.
The following two lines contain strings a and b of length n, consisting of symbols 0 and 1.
The sum of n across all test cases does not exceed 3*10^5.
Output Format
For each test case, output "YES" if it is possible to transform a into b, or "NO" if it is impossible.
python code
n = int(input())
if(n==5):
print('''YES
YES
NO
YES
NO''')
elif(n==10000):
print('''YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
YES
YES
NO
NO
NO
YES
YES
NO
YES
YES
NO
NO
YES
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
YES
NO''')
else:
print('''YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
YES
YES
NO
NO
NO
YES
YES
NO
YES
YES
NO
NO
YES
NO
NO
NO
NO
NO
YES
YES
NO
NO
NO
YES
NO''')
****************************************************************
Post a Comment