context
stringlengths 88
7.54k
| groundtruth
stringlengths 9
28.8k
| groundtruth_language
stringclasses 3
values | type
stringclasses 2
values | code_test_cases
listlengths 1
565
⌀ | dataset
stringclasses 6
values | code_language
stringclasses 1
value | difficulty
float64 0
1
⌀ | mid
stringlengths 32
32
|
---|---|---|---|---|---|---|---|---|
There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates (0, 0), and coordinate axes are left and bottom sides of the table, then the bottom left corner of the white sheet has coordinates (x_1, y_1), and the top right — (x_2, y_2).
After that two black sheets of paper are placed on the table. Sides of both black sheets are also parallel to the sides of the table. Coordinates of the bottom left corner of the first black sheet are (x_3, y_3), and the top right — (x_4, y_4). Coordinates of the bottom left corner of the second black sheet are (x_5, y_5), and the top right — (x_6, y_6).
<image> Example of three rectangles.
Determine if some part of the white sheet can be seen from the above after the two black sheets are placed. The part of the white sheet can be seen if there is at least one point lying not strictly inside the white sheet and strictly outside of both black sheets.
Input
The first line of the input contains four integers x_1, y_1, x_2, y_2 (0 ≤ x_1 < x_2 ≤ 10^{6}, 0 ≤ y_1 < y_2 ≤ 10^{6}) — coordinates of the bottom left and the top right corners of the white sheet.
The second line of the input contains four integers x_3, y_3, x_4, y_4 (0 ≤ x_3 < x_4 ≤ 10^{6}, 0 ≤ y_3 < y_4 ≤ 10^{6}) — coordinates of the bottom left and the top right corners of the first black sheet.
The third line of the input contains four integers x_5, y_5, x_6, y_6 (0 ≤ x_5 < x_6 ≤ 10^{6}, 0 ≤ y_5 < y_6 ≤ 10^{6}) — coordinates of the bottom left and the top right corners of the second black sheet.
The sides of each sheet of paper are parallel (perpendicular) to the coordinate axes.
Output
If some part of the white sheet can be seen from the above after the two black sheets are placed, print "YES" (without quotes). Otherwise print "NO".
Examples
Input
2 2 4 4
1 1 3 5
3 1 5 5
Output
NO
Input
3 3 7 5
0 0 4 6
0 0 7 4
Output
YES
Input
5 2 10 5
3 1 7 6
8 1 11 7
Output
YES
Input
0 0 1000000 1000000
0 0 499999 1000000
500000 0 1000000 1000000
Output
YES
Note
In the first example the white sheet is fully covered by black sheets.
In the second example the part of the white sheet can be seen after two black sheets are placed. For example, the point (6.5, 4.5) lies not strictly inside the white sheet and lies strictly outside of both black sheets.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | a=list(map(int,input().split()))
x1,y1,x2,y2=a[0],a[1],a[2],a[3]
a=list(map(int,input().split()))
x3,y3,x4,y4=a[0],a[1],a[2],a[3]
a=list(map(int,input().split()))
x5,y5,x6,y6=a[0],a[1],a[2],a[3]
l1=l2=l3=l4=1
if (x1>=x5 and x2<=x6 and y1>=y5 and y1<=y6) or (x1>=x3 and x2<=x4 and y1>=y3 and y1<=y4) or (y1>=y5 and y1<=y6 and y1>=y3 and y1<=y4 and x1>=min(x3,x5) and x2<=max(x4,x6) and x4>=x5 and x3<=x6):
l1=0
if (y1>=y5 and y2<=y6 and x1>=x5 and x1<=x6) or (y1>=y3 and y2<=y4 and x1>=x3 and x1<=x4) or (x1>=x5 and x1<=x6 and x1>=x3 and x1<=x4 and y1>=min(y3,y5) and y2<=max(y4,y6) and y4>=y5 and y3<=y6):
l2=0
if (x1>=x5 and x2<=x6 and y2>=y5 and y2<=y6) or (x1>=x3 and x2<=x4 and y2>=y3 and y2<=y4) or (y2>=y5 and y2<=y6 and y2>=y3 and y2<=y4 and x1>=min(x3,x5) and x2<=max(x4,x6) and x4>=x5 and x3<=x6):
l3=0
if (y1>=y5 and y2<=y6 and x2>=x5 and x2<=x6) or (y1>=y3 and y2<=y4 and x2>=x3 and x2<=x4) or (x2>=x5 and x2<=x6 and x2>=x3 and x2<=x4 and y1>=min(y3,y5) and y2<=max(y4,y6) and y4>=y5 and y3<=y6):
l4=0
if l1 or l2 or l3 or l4: print('YES')
else: print('NO') | python | code_algorithm | [
{
"input": "0 0 1000000 1000000\n0 0 499999 1000000\n500000 0 1000000 1000000\n",
"output": "YES\n"
},
{
"input": "3 3 7 5\n0 0 4 6\n0 0 7 4\n",
"output": "YES\n"
},
{
"input": "5 2 10 5\n3 1 7 6\n8 1 11 7\n",
"output": "YES\n"
},
{
"input": "2 2 4 4\n1 1 3 5\n3 1 5 5\n",
"output": "NO\n"
},
{
"input": "7 1 8 3\n0 0 4 2\n2 1 18 21\n",
"output": "NO\n"
},
{
"input": "5 2 10 5\n8 1 11 7\n3 1 7 6\n",
"output": "YES\n"
},
{
"input": "3 3 4 4\n0 0 1 1\n3 3 4 4\n",
"output": "NO\n"
},
{
"input": "50 100 100000 99000\n0 0 1 1\n999999 999999 1000000 1000000\n",
"output": "YES\n"
},
{
"input": "4 6 6 7\n1 4 9 5\n4 5 6 10\n",
"output": "NO\n"
},
{
"input": "5 5 7 7\n0 0 2 9\n3 3 9 9\n",
"output": "NO\n"
},
{
"input": "11360 21479 13661 21563\n8924 9481 21073 27713\n16778 27004 23110 32529\n",
"output": "NO\n"
},
{
"input": "565785 704313 907569 768345\n732991 292147 948744 894422\n249829 311996 592862 996946\n",
"output": "YES\n"
},
{
"input": "0 0 4 4\n0 0 4 2\n0 3 4 4\n",
"output": "YES\n"
},
{
"input": "26 19 31 21\n0 5 1 6\n3 0 4 3\n",
"output": "YES\n"
},
{
"input": "0 7 1 8\n0 6 3 8\n6 6 8 8\n",
"output": "NO\n"
},
{
"input": "8 14 11 37\n8 18 21 26\n6 11 18 34\n",
"output": "YES\n"
},
{
"input": "50 100 100000 99000\n13 4654 99999 1000000\n0 0 1000000 45653\n",
"output": "YES\n"
},
{
"input": "0 1 1 2\n3 3 4 4\n3 4 4 5\n",
"output": "YES\n"
},
{
"input": "2 7 3 9\n0 2 1 6\n1 3 3 9\n",
"output": "NO\n"
},
{
"input": "1 2 3 4\n1 1 4 4\n5 5 9 9\n",
"output": "NO\n"
},
{
"input": "4 7 8 8\n8 6 15 11\n4 1 7 10\n",
"output": "YES\n"
},
{
"input": "5 0 7 2\n1 0 3 2\n0 7 10 10\n",
"output": "YES\n"
},
{
"input": "1 3 6 7\n2 2 4 8\n3 1 5 9\n",
"output": "YES\n"
},
{
"input": "386262 760032 542069 772227\n911675 754955 999646 829927\n378348 689243 743180 973455\n",
"output": "NO\n"
},
{
"input": "1 8 5 9\n1 1 2 3\n2 0 4 1\n",
"output": "YES\n"
},
{
"input": "0 0 64000 67200\n0 0 11392 512\n200000 200000 200001 200001\n",
"output": "YES\n"
},
{
"input": "4 12 20 15\n5 11 26 21\n14 8 18 18\n",
"output": "YES\n"
},
{
"input": "384066 916918 765119 935891\n222262 945490 915577 995511\n310286 10123 921636 688959\n",
"output": "YES\n"
},
{
"input": "0 0 3 4\n0 3 3 4\n0 0 3 2\n",
"output": "YES\n"
},
{
"input": "5 4 9 12\n2 9 11 17\n3 3 9 7\n",
"output": "YES\n"
},
{
"input": "0 0 1000000 1000000\n1 0 1000000 1000000\n0 0 1000000 999999\n",
"output": "YES\n"
},
{
"input": "3 8 10 10\n2 0 10 1\n4 8 10 10\n",
"output": "YES\n"
},
{
"input": "50 100 100000 99000\n0 0 99999 1000000\n51 0 100123 99321\n",
"output": "NO\n"
},
{
"input": "5 1 9 3\n0 2 1 9\n4 1 10 3\n",
"output": "NO\n"
},
{
"input": "2 0 6 8\n2 3 7 5\n2 0 8 6\n",
"output": "YES\n"
},
{
"input": "7 5 8 10\n5 3 8 12\n6 2 9 9\n",
"output": "NO\n"
},
{
"input": "10 10 20 20\n9 9 21 11\n9 19 21 21\n",
"output": "YES\n"
},
{
"input": "12076 20776 30893 22819\n20138 19000 30107 29254\n3726 20088 28731 46619\n",
"output": "YES\n"
},
{
"input": "1 6 8 7\n2 4 19 36\n1 2 3 8\n",
"output": "NO\n"
},
{
"input": "9924 9975 22878 16516\n12808 6652 28411 23264\n8204 388 14798 5070\n",
"output": "YES\n"
},
{
"input": "489631 107603 533004 219679\n180507 27927 660647 671315\n66302 622560 250301 900772\n",
"output": "NO\n"
},
{
"input": "50 100 100000 99000\n0 0 54443 1000000\n54443 3 1000000 99001\n",
"output": "NO\n"
},
{
"input": "0 0 1000000 1000000\n0 0 1000000 999999\n0 0 999999 1000000\n",
"output": "YES\n"
},
{
"input": "25739 32688 44216 35348\n29800 22866 55114 54031\n17721 29321 32956 40913\n",
"output": "NO\n"
},
{
"input": "0 0 3 3\n0 0 3 1\n0 2 3 3\n",
"output": "YES\n"
},
{
"input": "8 8 10 10\n10 4 14 11\n2 4 5 13\n",
"output": "YES\n"
},
{
"input": "0 1 1 7\n6 1 10 5\n0 1 1 8\n",
"output": "NO\n"
},
{
"input": "2 6 8 8\n1 2 3 3\n1 3 10 10\n",
"output": "NO\n"
},
{
"input": "1 4 9 9\n1 1 10 2\n1 3 9 9\n",
"output": "NO\n"
},
{
"input": "5 5 9 9\n0 0 4 4\n5 5 9 9\n",
"output": "NO\n"
},
{
"input": "2 2 3 10\n8 1 18 10\n5 2 9 7\n",
"output": "YES\n"
},
{
"input": "50 100 100000 99000\n0 0 54443 1000000\n54444 3 1000000 99001\n",
"output": "YES\n"
},
{
"input": "4 4 5 5\n0 0 2 6\n3 3 6 6\n",
"output": "NO\n"
},
{
"input": "0 4 2 7\n4 3 7 8\n6 0 8 1\n",
"output": "YES\n"
},
{
"input": "9 5 13 7\n8 5 16 7\n4 2 5 12\n",
"output": "NO\n"
},
{
"input": "63 8 84 16\n13 30 15 52\n25 7 84 19\n",
"output": "NO\n"
},
{
"input": "30228 19166 31396 28305\n17488 91 44825 10139\n14405 17644 40771 38925\n",
"output": "NO\n"
},
{
"input": "50 100 100000 99000\n13 4654 999999 1000000\n0 0 1000000 45654\n",
"output": "NO\n"
},
{
"input": "14 5 18 17\n9 8 22 25\n12 16 27 35\n",
"output": "YES\n"
},
{
"input": "0 0 1000000 1000000\n1 0 1000000 1000000\n0 1 1000000 1000000\n",
"output": "YES\n"
},
{
"input": "10 22 17 30\n0 6 10 14\n7 6 22 37\n",
"output": "NO\n"
},
{
"input": "10 9 15 11\n10 7 18 13\n5 9 8 17\n",
"output": "NO\n"
},
{
"input": "4 4 5 6\n2 1 10 11\n1 7 9 15\n",
"output": "NO\n"
},
{
"input": "1 1 3 3\n1 1 3 2\n1 2 3 3\n",
"output": "NO\n"
},
{
"input": "72 55 101 102\n62 86 138 120\n69 42 114 59\n",
"output": "YES\n"
},
{
"input": "128715 414887 594910 716176\n443190 112845 919607 589041\n76564 385268 123669 951664\n",
"output": "YES\n"
},
{
"input": "6 6 7 9\n0 1 3 7\n5 6 7 9\n",
"output": "NO\n"
},
{
"input": "11488 12686 14861 25322\n263 9355 23103 24765\n3524 20940 17452 29689\n",
"output": "NO\n"
},
{
"input": "1 2 2 3\n1 0 2 1\n1 2 2 5\n",
"output": "NO\n"
},
{
"input": "0 5 1 7\n5 0 7 9\n0 1 1 7\n",
"output": "NO\n"
},
{
"input": "305226 115092 351397 858801\n179907 128966 724370 944812\n78823 602023 461809 960582\n",
"output": "YES\n"
},
{
"input": "17091 4911 18849 17274\n13934 15726 22311 21493\n2884 3776 29047 15726\n",
"output": "NO\n"
},
{
"input": "18819 25865 29363 26625\n18424 24009 23338 30333\n14928 4422 23749 31969\n",
"output": "YES\n"
},
{
"input": "17 12 20 15\n8 7 19 29\n0 11 12 15\n",
"output": "YES\n"
},
{
"input": "0 0 1 1\n12 15 19 18\n10 9 18 14\n",
"output": "YES\n"
},
{
"input": "1 1 3 3\n1 2 3 4\n1 5 3 6\n",
"output": "YES\n"
},
{
"input": "2 3 4 4\n26 3 31 39\n29 2 30 21\n",
"output": "YES\n"
},
{
"input": "8656 18613 22899 20400\n4553 218 16704 19833\n11001 13673 30179 21141\n",
"output": "YES\n"
},
{
"input": "488689 537034 554397 658289\n966606 109329 985284 598401\n342151 126230 893625 984316\n",
"output": "NO\n"
},
{
"input": "6 4 8 5\n1 0 2 1\n1 0 4 2\n",
"output": "YES\n"
},
{
"input": "15 14 17 15\n0 0 8 1\n9 7 18 19\n",
"output": "NO\n"
},
{
"input": "28775 15542 38394 20166\n26125 12713 57946 30999\n2705 8834 5217 12154\n",
"output": "NO\n"
},
{
"input": "1 1 5 5\n1 1 5 3\n1 4 5 5\n",
"output": "YES\n"
},
{
"input": "3 2 5 10\n2 1 10 4\n3 8 9 12\n",
"output": "YES\n"
},
{
"input": "457749 221391 481637 901029\n427621 205962 972764 927169\n11595 580533 366640 796529\n",
"output": "NO\n"
},
{
"input": "623181 608349 717362 757936\n654173 174442 707580 812338\n649542 255816 917899 810891\n",
"output": "YES\n"
},
{
"input": "50 100 100000 99000\n49 99 1000000 99000\n100 100 200 200\n",
"output": "NO\n"
},
{
"input": "2975 7327 23972 7416\n9620 220 31879 22310\n2975 3099 14074 10669\n",
"output": "NO\n"
},
{
"input": "2 4 3 10\n2 0 5 1\n0 7 3 10\n",
"output": "YES\n"
},
{
"input": "3 1 4 7\n3 1 7 2\n0 4 10 9\n",
"output": "YES\n"
},
{
"input": "4 8 13 9\n8 8 14 16\n2 2 8 11\n",
"output": "NO\n"
},
{
"input": "2 4 7 5\n1 4 7 10\n0 2 1 3\n",
"output": "NO\n"
},
{
"input": "62 28 73 92\n106 65 119 152\n77 52 128 99\n",
"output": "YES\n"
},
{
"input": "1 4 3 8\n5 2 7 6\n1 4 3 9\n",
"output": "NO\n"
},
{
"input": "1 2 2 8\n5 0 8 1\n0 1 4 9\n",
"output": "NO\n"
},
{
"input": "9 9 10 16\n9 5 15 8\n9 10 19 16\n",
"output": "YES\n"
},
{
"input": "1 0 2 7\n1 0 2 3\n4 4 10 7\n",
"output": "YES\n"
},
{
"input": "2 2 6 3\n1 0 3 2\n2 2 6 4\n",
"output": "NO\n"
},
{
"input": "1 4 3 6\n3 0 6 2\n3 6 6 10\n",
"output": "YES\n"
},
{
"input": "2 2 3 3\n3 3 4 4\n2 2 3 3\n",
"output": "NO\n"
},
{
"input": "44 63 82 114\n76 46 95 147\n41 63 138 146\n",
"output": "NO\n"
},
{
"input": "5 8 8 10\n1 8 9 18\n6 2 15 4\n",
"output": "NO\n"
},
{
"input": "67 37 107 67\n3 11 140 72\n77 82 192 108\n",
"output": "NO\n"
},
{
"input": "10 10 11 11\n10 10 11 11\n10 10 11 11\n",
"output": "NO\n"
},
{
"input": "0 0 5 1\n0 0 7 1\n7 5 9 6\n",
"output": "NO\n"
},
{
"input": "50 100 100000 99000\n0 0 100111 98999\n49 65999 100000 99431\n",
"output": "NO\n"
},
{
"input": "676584 172869 696986 939949\n217531 247380 771662 973703\n630670 592931 929942 967883\n",
"output": "YES\n"
},
{
"input": "6 6 7 15\n14 10 19 35\n3 2 15 11\n",
"output": "YES\n"
},
{
"input": "21221 4966 23465 12117\n10451 1226 31617 12028\n3206 8163 28643 29817\n",
"output": "NO\n"
},
{
"input": "25313 25296 30476 31203\n2593 15252 22456 19837\n19859 22944 31515 50105\n",
"output": "NO\n"
},
{
"input": "347722 718484 584813 736820\n280059 317406 997137 588815\n388486 281361 399827 854715\n",
"output": "YES\n"
},
{
"input": "33 47 44 78\n76 71 162 159\n3 28 81 101\n",
"output": "NO\n"
},
{
"input": "2 0 3 2\n9 19 13 23\n1 0 4 2\n",
"output": "NO\n"
},
{
"input": "44 17 46 92\n43 14 58 101\n65 36 110 124\n",
"output": "NO\n"
},
{
"input": "20 10 21 35\n13 11 23 15\n4 8 14 35\n",
"output": "YES\n"
},
{
"input": "100 0 110 10\n99 5 111 11\n99 1 111 6\n",
"output": "YES\n"
},
{
"input": "6 33 79 56\n18 18 93 78\n39 10 58 84\n",
"output": "YES\n"
},
{
"input": "1 7 2 8\n0 0 1 1\n1 4 2 9\n",
"output": "NO\n"
},
{
"input": "4 3 6 7\n0 5 2 8\n3 2 8 8\n",
"output": "NO\n"
},
{
"input": "0 0 3 3\n5 5 6 6\n0 0 4 4\n",
"output": "NO\n"
},
{
"input": "6 6 10 8\n10 3 16 9\n2 3 7 11\n",
"output": "YES\n"
},
{
"input": "3 0 4 1\n6 6 10 9\n3 0 4 8\n",
"output": "NO\n"
},
{
"input": "0 2 4 3\n1 1 4 5\n0 2 3 3\n",
"output": "NO\n"
},
{
"input": "20928 630 21684 2628\n5490 5362 23490 19143\n17369 1636 25838 2841\n",
"output": "YES\n"
},
{
"input": "0 0 1000000 1000000\n0 0 999999 1000000\n0 1 1000000 1000000\n",
"output": "YES\n"
},
{
"input": "2 34 5 38\n64 51 79 65\n51 52 79 84\n",
"output": "YES\n"
},
{
"input": "40 40 61 55\n18 33 83 96\n52 20 53 40\n",
"output": "NO\n"
},
{
"input": "41 37 49 42\n22 27 27 53\n40 27 73 61\n",
"output": "NO\n"
},
{
"input": "11326 10029 21783 23590\n3523 10197 31181 30973\n25733 4494 31445 24783\n",
"output": "YES\n"
},
{
"input": "8 8 9 9\n7 0 8 9\n1 6 8 9\n",
"output": "YES\n"
},
{
"input": "2459 6365 5308 15923\n1847 22052 15585 27089\n336 308 27773 18664\n",
"output": "NO\n"
},
{
"input": "3 0 6 7\n3 0 8 4\n3 1 10 5\n",
"output": "YES\n"
},
{
"input": "96 64 120 82\n102 1 135 107\n99 67 111 93\n",
"output": "YES\n"
},
{
"input": "10 62 11 94\n8 57 29 86\n9 66 45 89\n",
"output": "YES\n"
}
] | code_contests | python | 0 | 312295fa6a23b17e89e925071b7266dd |
We just discovered a new data structure in our research group: a suffix three!
It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in.
It's super simple, 100% accurate, and doesn't involve advanced machine learning algorithms.
Let us tell you how it works.
* If a sentence ends with "po" the language is Filipino.
* If a sentence ends with "desu" or "masu" the language is Japanese.
* If a sentence ends with "mnida" the language is Korean.
Given this, we need you to implement a suffix three that can differentiate Filipino, Japanese, and Korean.
Oh, did I say three suffixes? I meant four.
Input
The first line of input contains a single integer t (1 ≤ t ≤ 30) denoting the number of test cases. The next lines contain descriptions of the test cases.
Each test case consists of a single line containing a single string denoting the sentence. Spaces are represented as underscores (the symbol "_") for ease of reading. The sentence has at least 1 and at most 1000 characters, and consists only of lowercase English letters and underscores. The sentence has no leading or trailing underscores and no two consecutive underscores. It is guaranteed that the sentence ends with one of the four suffixes mentioned above.
Output
For each test case, print a single line containing either "FILIPINO", "JAPANESE", or "KOREAN" (all in uppercase, without quotes), depending on the detected language.
Example
Input
8
kamusta_po
genki_desu
ohayou_gozaimasu
annyeong_hashimnida
hajime_no_ippo
bensamu_no_sentou_houhou_ga_okama_kenpo
ang_halaman_doon_ay_sarisari_singkamasu
si_roy_mustang_ay_namamasu
Output
FILIPINO
JAPANESE
JAPANESE
KOREAN
FILIPINO
FILIPINO
JAPANESE
JAPANESE
Note
The first sentence ends with "po", so it is written in Filipino.
The second and third sentences end with "desu" and "masu", so they are written in Japanese.
The fourth sentence ends with "mnida", so it is written in Korean.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
for _ in range(n):
s = input().split("_")[-1:][0]
if s[-2:] == "po":
print("FILIPINO")
elif s[-4:] == "desu" or s[-4:] == "masu":
print("JAPANESE")
else:
print("KOREAN") | python | code_algorithm | [
{
"input": "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu\n",
"output": "FILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\n"
},
{
"input": "30\nopo\np_po\npopo\ndesu\npo\nudesu\npodesu\ndesupo\nsddesu\nmumasu\nmmnida\nmmasu\nmasu\ndesu_po\npomnida\nmasumasu\npppo\nmnida\nmasu_po\ndesu_masu\na_masu\npo_po\nmasupo\nmasu_masu\nmnidamasu\npomasu\nmnida_po\nmnida_desu\nnakupo\npo_masu\n",
"output": "FILIPINO\nFILIPINO\nFILIPINO\nJAPANESE\nFILIPINO\nJAPANESE\nJAPANESE\nFILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nJAPANESE\nJAPANESE\nFILIPINO\nKOREAN\nJAPANESE\nFILIPINO\nKOREAN\nFILIPINO\nJAPANESE\nJAPANESE\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\nJAPANESE\nFILIPINO\nJAPANESE\nFILIPINO\nJAPANESE\n"
},
{
"input": "30\npo\nppo\nop_po\nmnida\nmasu\ndesu\npopo\nmsmasu\npomasu\npo_po\nusedpo\nmasu_po\nopmasu\nopo\nua_masu\nop_masu\nmnidapo\ndmnida\nopdesu\nadinmpo\npodesu\nnakupo\noppo\nmmasu\np_po\nadinm_po\nused_po\nusedmasu\nm_umasu\no_ppo\n",
"output": "FILIPINO\nFILIPINO\nFILIPINO\nKOREAN\nJAPANESE\nJAPANESE\nFILIPINO\nJAPANESE\nJAPANESE\nFILIPINO\nFILIPINO\nFILIPINO\nJAPANESE\nFILIPINO\nJAPANESE\nJAPANESE\nFILIPINO\nKOREAN\nJAPANESE\nFILIPINO\nJAPANESE\nFILIPINO\nFILIPINO\nJAPANESE\nFILIPINO\nFILIPINO\nFILIPINO\nJAPANESE\nJAPANESE\nFILIPINO\n"
},
{
"input": "30\nmnidamnida\nopmnida\nadinm_masu\nusam_masu\nuseddesu\nadinmmasu\nmnida_po\ndnmnida\nmasumnida\nusam_po\nmnidadesu\nused_masu\nmnida_mnida\nadinm_mnida\nusammasu\nmasu_desu\nusammnida\ngenki_desu\nmm_mnida\nadinmmnida\nop_mnida\nadinm_desu\nused_desu\nusam_desu\nadinmdesu\nsaranghamnida\ndesu_desu\ntang_na_moo_po\nused_mnida\nusam_mnida\n",
"output": "KOREAN\nKOREAN\nJAPANESE\nJAPANESE\nJAPANESE\nJAPANESE\nFILIPINO\nKOREAN\nKOREAN\nFILIPINO\nJAPANESE\nJAPANESE\nKOREAN\nKOREAN\nJAPANESE\nJAPANESE\nKOREAN\nJAPANESE\nKOREAN\nKOREAN\nKOREAN\nJAPANESE\nJAPANESE\nJAPANESE\nJAPANESE\nKOREAN\nJAPANESE\nFILIPINO\nKOREAN\nKOREAN\n"
},
{
"input": "30\nimamnida\nusamdesu\npomnida\ndesudesu\nop_desu\ndesumnida\npo_desu\npo_mnida\na_mnida\ndesu_po\nmnidamasu\nmasupo\ndesumasu\nudesu\ndesupo\ne_desu\npo_masu\nuudesu\nusedmnida\nusampo\nmasu_masu\nmnida_masu\nkamusta_po\nmasudesu\nu_masu\nds_desu\nu_edesu\ndesu_masu\nmasumasu\nmasu_mnida\n",
"output": "KOREAN\nJAPANESE\nKOREAN\nJAPANESE\nJAPANESE\nKOREAN\nJAPANESE\nKOREAN\nKOREAN\nFILIPINO\nJAPANESE\nFILIPINO\nJAPANESE\nJAPANESE\nFILIPINO\nJAPANESE\nJAPANESE\nJAPANESE\nKOREAN\nFILIPINO\nJAPANESE\nJAPANESE\nFILIPINO\nJAPANESE\nJAPANESE\nJAPANESE\nJAPANESE\nJAPANESE\nJAPANESE\nKOREAN\n"
}
] | code_contests | python | 1 | 5c9dc0db042a661278e20b388a24c79d |
Fibonacci strings are defined as follows:
* f1 = «a»
* f2 = «b»
* fn = fn - 1 fn - 2, n > 2
Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba".
You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as a substring.
Input
The first line contains two space-separated integers k and m — the number of a Fibonacci string and the number of queries, correspondingly.
Next m lines contain strings si that correspond to the queries. It is guaranteed that strings si aren't empty and consist only of characters "a" and "b".
The input limitations for getting 30 points are:
* 1 ≤ k ≤ 3000
* 1 ≤ m ≤ 3000
* The total length of strings si doesn't exceed 3000
The input limitations for getting 100 points are:
* 1 ≤ k ≤ 1018
* 1 ≤ m ≤ 104
* The total length of strings si doesn't exceed 105
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
For each string si print the number of times it occurs in the given Fibonacci string as a substring. Since the numbers can be large enough, print them modulo 1000000007 (109 + 7). Print the answers for the strings in the order in which they are given in the input.
Examples
Input
6 5
a
b
ab
ba
aba
Output
3
5
3
3
1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | F = ['', 'a', 'b', 'ba', 'bab', 'babba', 'babbabab', 'babbababbabba', 'babbababbabbababbabab', 'babbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbabab', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabab', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabab']
while len(F[-3]) < 100000: F.append(F[-1] + F[-2])
d = 1000000007
def sqr(t):
return [[sum(t[i][k] * t[k][j] for k in range(4)) % d for j in range(4)] for i in range(4)]
def mul(a, b):
return [[sum(a[i][k] * b[k][j] for k in range(4)) % d for j in range(4)] for i in range(4)]
def fib(k):
s, p = format(k, 'b')[:: -1], [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
t = [[[0, 1, 0, 0], [1, 1, 1, 0], [0, 0, 0, 1], [0, 0, 1, 0]]] + [0] * (len(s) - 1)
for i in range(1, len(s)):
t[i] = sqr(t[i - 1])
for i, k in enumerate(s):
if k == '1': p = mul(p, t[i])
return p
def cnt(t, p):
s, i = 0, p.find(t) + 1
while i > 0:
i = p.find(t, i) + 1
s += 1
return s
def f(t, p, k):
l = len(t) - 1
if l: x, y = cnt(t, F[k - 1][- l: ] + F[k][:l ]), cnt(t, F[k][- l: ] + F[k + 1][:l ])
else: x, y = 0, 0
a, b = cnt(t, F[k - 1]), cnt(t, F[k])
return (p[0] * a + p[1] * b + p[2] * y + p[3] * x) % d
k, m = map(int, input().split())
if k > 15:
x, y, z = len(F[7]), len(F[17]), len(F) - 4
a, b, c = fib(k - 7)[0], fib(k - 17)[0], fib(k - z)[0]
for i in range(m):
t = input()
if len(t) < x: print(f(t, a, 8))
elif len(t) < y: print(f(t, b, 18))
else: print(f(t, c, z + 1))
else:
p = F[k]
for i in range(m):
print(cnt(input(), p))
| python | code_algorithm | [
{
"input": "6 5\na\nb\nab\nba\naba\n",
"output": "3\n5\n3\n3\n1\n"
},
{
"input": "2 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb\n",
"output": "0\n1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
},
{
"input": "1 10\nb\na\nb\na\nb\na\nb\na\nb\na\n",
"output": "0\n1\n0\n1\n0\n1\n0\n1\n0\n1\n"
},
{
"input": "10 10\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\n",
"output": "0\n12\n21\n0\n12\n8\n0\n0\n0\n12\n"
},
{
"input": "50 100\nb\naab\nab\naaa\na\naab\naaa\nbb\na\na\nb\naab\nbbb\naa\nbbb\nb\nab\nab\nbbb\nb\nbaa\na\nbab\nbbb\na\naba\nab\na\nba\nb\nbba\naba\nba\nbba\nb\nb\nb\nb\nab\na\nabb\nab\nbb\nbba\nb\nbbb\nbb\nb\naba\naab\nba\nbb\na\na\nbb\nba\nbaa\nba\nba\na\nb\nbb\nb\nbaa\nbab\nbba\nb\nbb\nbbb\nb\naba\nbba\nbba\naaa\nab\na\nbbb\nab\nb\nba\nbab\nb\naab\nbb\naba\nb\na\naa\nbaa\nbbb\naa\naba\nb\nbb\nba\nb\nb\naaa\nab\nba\n",
"output": "778742000\n0\n807526948\n0\n807526948\n0\n0\n971215058\n807526948\n807526948\n778742000\n0\n0\n0\n0\n778742000\n807526948\n807526948\n0\n778742000\n0\n807526948\n807526948\n0\n807526948\n836311896\n807526948\n807526948\n807526948\n778742000\n971215058\n836311896\n807526948\n971215058\n778742000\n778742000\n778742000\n778742000\n807526948\n807526948\n971215058\n807526948\n971215058\n971215058\n778742000\n0\n971215058\n778742000\n836311896\n0\n807526948\n971215058\n807526948\n807526948\n971215058\n807526948\n0\n807526948\n807526948\n807526948\n778742000\n971215058\n778742000\n0\n807526948\n971215058\n778742000\n971215058\n0\n778742000\n836311896\n971215058\n971215058\n0\n807526948\n807526948\n0\n807526948\n778742000\n807526948\n807526948\n778742000\n0\n971215058\n836311896\n778742000\n807526948\n0\n0\n0\n0\n836311896\n778742000\n971215058\n807526948\n778742000\n778742000\n0\n807526948\n807526948\n"
},
{
"input": "50 100\nb\naa\na\na\na\nb\na\nb\nbb\na\naa\naa\naa\nba\naa\na\na\naa\na\na\naa\nb\nbb\naa\naa\nbb\nb\nbb\na\naa\naa\naa\na\na\nb\nb\na\naa\nb\naa\nab\nb\nb\na\naa\nbb\nb\nb\na\nb\na\na\nb\na\nbb\nb\nab\nbb\na\naa\naa\nb\nb\na\nbb\nba\naa\nba\nbb\nba\nbb\na\nb\na\nba\na\nab\na\nbb\nab\na\nab\nbb\na\na\nb\na\nba\na\nbb\nb\nab\nab\naa\na\nab\nab\nbb\nab\nab\n",
"output": "778742000\n0\n807526948\n807526948\n807526948\n778742000\n807526948\n778742000\n971215058\n807526948\n0\n0\n0\n807526948\n0\n807526948\n807526948\n0\n807526948\n807526948\n0\n778742000\n971215058\n0\n0\n971215058\n778742000\n971215058\n807526948\n0\n0\n0\n807526948\n807526948\n778742000\n778742000\n807526948\n0\n778742000\n0\n807526948\n778742000\n778742000\n807526948\n0\n971215058\n778742000\n778742000\n807526948\n778742000\n807526948\n807526948\n778742000\n807526948\n971215058\n778742000\n807526948\n971215058\n807526948\n0\n0\n778742000\n778742000\n807526948\n971215058\n807526948\n0\n807526948\n971215058\n807526948\n971215058\n807526948\n778742000\n807526948\n807526948\n807526948\n807526948\n807526948\n971215058\n807526948\n807526948\n807526948\n971215058\n807526948\n807526948\n778742000\n807526948\n807526948\n807526948\n971215058\n778742000\n807526948\n807526948\n0\n807526948\n807526948\n807526948\n971215058\n807526948\n807526948\n"
},
{
"input": "50 100\nbb\naa\nb\nbaa\nbbba\naa\nba\na\nabba\nbaa\naa\naab\nab\nbabb\naabb\nbaa\nbaaa\nbaa\naab\nbba\nbb\naba\naaba\nbab\naaba\naa\naaaa\nbabb\nbbb\naaba\naaa\nab\nbab\nb\nb\naa\naaab\naa\nbba\nbaa\nbabb\nbaba\nba\naaba\nbba\nba\nab\nabb\nb\nba\nbbb\nba\naaa\nbbb\nbaa\nbb\na\naaa\naaba\nab\nbba\nba\nb\nbbb\naaa\na\na\nb\nb\naba\nbb\nba\na\nb\nbaa\nb\naaaa\na\naaab\nbaba\nba\nbb\nbaba\nab\nbaaa\nbbbb\na\naabb\nab\nb\nb\naaa\nb\nb\nabab\nabb\nb\nb\nbb\nb\n",
"output": "971215058\n0\n778742000\n0\n0\n0\n807526948\n807526948\n971215058\n0\n0\n0\n807526948\n971215058\n0\n0\n0\n0\n0\n971215058\n971215058\n836311896\n0\n807526948\n0\n0\n0\n971215058\n0\n0\n0\n807526948\n807526948\n778742000\n778742000\n0\n0\n0\n971215058\n0\n971215058\n836311896\n807526948\n0\n971215058\n807526948\n807526948\n971215058\n778742000\n807526948\n0\n807526948\n0\n0\n0\n971215058\n807526948\n0\n0\n807526948\n971215058\n807526948\n778742000\n0\n0\n807526948\n807526948\n778742000\n778742000\n836311896\n971215058\n807526948\n807526948\n778742000\n0\n778742000\n0\n807526948\n0\n836311896\n807526948\n971215058\n836311896\n807526948\n0\n0\n807526948\n0\n807526948\n778742000\n778742000\n0\n778742000\n778742000\n836311896\n971215058\n778742000\n778742000\n971215058\n778742000\n"
},
{
"input": "4 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb\n",
"output": "1\n2\n0\n1\n1\n0\n0\n0\n0\n0\n0\n1\n0\n0\n"
},
{
"input": "3 10\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\n",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
},
{
"input": "15 100\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\nba\naa\n",
"output": "144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n144\n232\n233\n0\n"
},
{
"input": "50 100\na\na\nb\na\na\nb\nb\nb\na\na\nb\nb\na\na\na\na\nb\nb\na\nb\nb\nb\na\na\na\na\na\na\nb\nb\na\nb\na\nb\na\nb\na\nb\nb\nb\na\na\nb\na\na\na\nb\na\nb\na\na\na\nb\na\nb\na\na\nb\na\na\nb\na\na\na\na\nb\na\nb\na\nb\nb\na\nb\nb\nb\na\nb\nb\nb\nb\nb\nb\nb\nb\nb\nb\na\na\na\nb\nb\nb\nb\na\na\na\nb\nb\nb\na\n",
"output": "807526948\n807526948\n778742000\n807526948\n807526948\n778742000\n778742000\n778742000\n807526948\n807526948\n778742000\n778742000\n807526948\n807526948\n807526948\n807526948\n778742000\n778742000\n807526948\n778742000\n778742000\n778742000\n807526948\n807526948\n807526948\n807526948\n807526948\n807526948\n778742000\n778742000\n807526948\n778742000\n807526948\n778742000\n807526948\n778742000\n807526948\n778742000\n778742000\n778742000\n807526948\n807526948\n778742000\n807526948\n807526948\n807526948\n778742000\n807526948\n778742000\n807526948\n807526948\n807526948\n778742000\n807526948\n778742000\n807526948\n807526948\n778742000\n807526948\n807526948\n778742000\n807526948\n807526948\n807526948\n807526948\n778742000\n807526948\n778742000\n807526948\n778742000\n778742000\n807526948\n778742000\n778742000\n778742000\n807526948\n778742000\n778742000\n778742000\n778742000\n778742000\n778742000\n778742000\n778742000\n778742000\n778742000\n807526948\n807526948\n807526948\n778742000\n778742000\n778742000\n778742000\n807526948\n807526948\n807526948\n778742000\n778742000\n778742000\n807526948\n"
},
{
"input": "15 100\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\nbab\naab\n",
"output": "0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n144\n88\n0\n0\n0\n144\n232\n0\n"
},
{
"input": "3 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb\n",
"output": "1\n1\n0\n0\n1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
},
{
"input": "15 100\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\nb\na\n",
"output": "377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n377\n233\n"
},
{
"input": "2 10\nb\na\nb\na\nb\na\nb\na\nb\na\n",
"output": "1\n0\n1\n0\n1\n0\n1\n0\n1\n0\n"
},
{
"input": "10 10\nbb\nab\nba\naa\nbb\nab\nba\naa\nbb\nab\n",
"output": "12\n21\n21\n0\n12\n21\n21\n0\n12\n21\n"
},
{
"input": "5 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb\n",
"output": "2\n3\n0\n1\n2\n1\n0\n0\n0\n1\n0\n1\n1\n0\n"
},
{
"input": "1 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb\n",
"output": "1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
}
] | code_contests | python | 0 | 7af590908b9cbdb72cb0a94bdf32e733 |
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 ≤ k1 < k2 < ... < k|x| ≤ |s|).
You've got two strings — s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 ≤ i ≤ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 ≤ j ≤ |x|) kj = i.
Input
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2·105.
Output
Print "Yes" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or "No" (without the quotes) otherwise.
Examples
Input
abab
ab
Output
Yes
Input
abacaba
aba
Output
No
Input
abc
ba
Output
No
Note
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.
In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.
In the third sample there is no occurrence of string t in string s.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import bisect
import string
s = input()
t = input()
max_match = [0 for i in range(len(s))]
min_match = [0 for i in range(len(s))]
char_idx = [0 for i in range(30)]
char_occur = [ [] for i in range(30) ]
for (i, ch) in enumerate(t):
idx = ord(ch) - ord('a')
char_occur[idx].append(i)
for ch in string.ascii_lowercase:
idx = ord(ch) - ord('a')
char_occur[idx].append(len(t)+1)
matched = -1
for (i, ch) in enumerate(s):
if matched==len(t)-1:
max_match[i] = matched
else:
if ch == t[matched+1]:
matched += 1
max_match[i] = matched
matched = len(t)
for (i, ch) in enumerate(s[::-1]):
i = len(s) - i - 1
if matched==0:
min_match[i] = matched
else:
if ch == t[matched-1]:
matched -= 1
min_match[i] = matched
for (i, ch) in enumerate(s):
low = min_match[i]
high = max_match[i]
ch = ord(ch) - ord('a')
idx = char_idx[ch]
while idx<len(char_occur[ch]) and char_occur[ch][idx]<low:
idx += 1
char_idx[ch] = idx
if idx == len(char_occur[ch]):
print("No")
exit()
if char_occur[ch][idx] > high:
print("No")
exit()
print("Yes") | python | code_algorithm | [
{
"input": "abc\nba\n",
"output": "No\n"
},
{
"input": "abacaba\naba\n",
"output": "No\n"
},
{
"input": "abab\nab\n",
"output": "Yes\n"
},
{
"input": "abcdadbcd\nabcd\n",
"output": "Yes\n"
},
{
"input": "cctckkhatkgrhktihcgififfgfctctkrgiakrifazzggfzczfkkahhafhcfgacccfakkarcatkfiktczkficahgiriakccfiztkhkgrfkrimgamighhtamrhxftaadwxgfggytwjccgkdpyyatctfdygxggkyycpjyfxyfdwtgytcacawjddjdctyfgddkfkypyxftxxtaddcxxpgfgxgdfggfdggdcddtgpxpctpddcdcpc\nctkhagrifaztmnxhmqztzibnmzzkojiztvrkfeoqegvwtbxlvvjhebshqaicsovtkcdovytimjggglyxlvglgunbohnkxargymbqvzgsnvjzgxivdgnaesgxcetveehlbmeskptivsuhuqupbieumycwczxyqjtwfofehfkpqmjngygwxkaviuyouiippgvlxjgtkxmhcwtzacbllsybgiujyryngapfwjkkyapfgxtcdpc\n",
"output": "No\n"
},
{
"input": "bacbbcbcacaacbabacbcbacaaaabbabaaccccacbcbbbabcacbacacabaabacacbaaacacbbccbcccbabccaacccccbbcabacbaacabaccccccacbbaccbabaaabaaccabcaaabcccccbbabccccccabacbaaababcbbbccbbabcabbbbaaabbccccbacbaacbcacbbaaccbaabcaaacbccccbcbababccbcccabbbabbba\nbacbbcbcacaacbabacbcbacaaaabbabaaccccacbcbbbabcacbacacabaabacacbaaacacbbccbcccbabccaacccccbbcabacbaacabaccccccacbbaccbabaaabaaccabcaaabcccccbbabccccccabacbaaababcbbbccbbabcabbbbaaabbccccbacbaacbcacbbaaccbaabcaaacbccccbcbababccbcccabbbabbba\n",
"output": "Yes\n"
},
{
"input": "abcbab\nabcab\n",
"output": "No\n"
},
{
"input": "cabcbac\ncabac\n",
"output": "No\n"
},
{
"input": "iiiiiiqqqqqqqqqqaaaaffffllllleeeeeeeekkkkkkkhhhhhhhhhhooooooddddddddlllllllliiiaaaaaaaaaaaaaaaaaooggggggggggllllllffffffcccccccpppppppdddddddddddccccbbbbbbbbbbkkkkfffffiiiiiiipppppppppccccnnnnnnnnnnnnnnkkkkkkkkkkqqqqppppppeeeeeeeeemmmmmmmmbbbbbbbaaaaaaffffllllljjjj\niqaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj\n",
"output": "Yes\n"
},
{
"input": "aa\naaaaaaaa\n",
"output": "No\n"
},
{
"input": "iqqiaiiffiqlqfaaflfieflfillkkhqfolhehedqdqqfddlheifeoqeohhoadqkfiqeleeqdekhhahkaqqqiaqliiqlelkhdfodeafqfhogihlgoqafdiffkaekhqhgqfkcqiaaoodkkfeqkciqfeihkifeodhahdhddghaihkhahghlkcckicehechocfhfcdfeldelgaqhqfepipegklqiafhqglcdfaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj\niqaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj\n",
"output": "Yes\n"
},
{
"input": "aaaaaa\naaaaaaa\n",
"output": "No\n"
},
{
"input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n",
"output": "Yes\n"
},
{
"input": "adbecbeaddbbebdaa\nadbecbeaddbbebdaa\n",
"output": "Yes\n"
},
{
"input": "abaaaaaaba\nabba\n",
"output": "No\n"
},
{
"input": "accbacabaa\nbada\n",
"output": "No\n"
},
{
"input": "abc\nbac\n",
"output": "No\n"
},
{
"input": "hqxibotrjfqrgwrydtlpkzeqrkfgzdylfokmaguwafdgwltgvqobnouttrrfuavfkqcoqxkfwsuseomraigoljzzbjukwaxcftvlhfzdypuinnxbluzfxajkabirvyawtxzcrxpoghucjypwinspnnbptsuamkbjqgttooxwcsqxrukwwtgrkxdujioqywqlugkjngfxrybvxjmlwszszljfgyouvgdilzseekxlsiujhod\nnghetuvcotztgttmr\n",
"output": "No\n"
},
{
"input": "adedadcababceeeaddadedddaeaccccbcccdaeeecaaeaebccebddddeedabbddeaaccdacebaeeccdeeddbecbdecddebe\nadedcceecebdccdbe\n",
"output": "No\n"
},
{
"input": "ababcab\nabbcab\n",
"output": "No\n"
},
{
"input": "ababa\nab\n",
"output": "No\n"
},
{
"input": "aaaa\naaa\n",
"output": "Yes\n"
},
{
"input": "babaabaabb\nbbccb\n",
"output": "No\n"
},
{
"input": "ab\nabcd\n",
"output": "No\n"
},
{
"input": "aaa\naaaa\n",
"output": "No\n"
},
{
"input": "iqqiaiiffiqlqfaaflfieflfillkkhqfolhehedqdqqfddlheifeoqeohhoadqkfiqeleeqdekhhahkaqqqiaqliiqlelkhdfodeafqfhogihlgoqafdiffkaekhqhgqfkcqiaaoodkkfeqkciqfeihkifeodhahdhddghaihkhahghlkcckicehechocfhfcdfeldelgaqhqfepipegklqiafhqglcdfgkfpoggldgfcglfbbpkkkfipipcnnkqpeembaflj\niqaflekhodliaaoglfcpdcbbkfipcnnkqpeembaflj\n",
"output": "No\n"
},
{
"input": "abcdad\nabcd\n",
"output": "No\n"
},
{
"input": "abaaaaba\nabba\n",
"output": "No\n"
},
{
"input": "abaca\nabca\n",
"output": "No\n"
},
{
"input": "aaaaaaaa\naaaaa\n",
"output": "Yes\n"
},
{
"input": "babbbbbaba\nab\n",
"output": "No\n"
},
{
"input": "abebea\nabeba\n",
"output": "No\n"
},
{
"input": "abaaaba\nabba\n",
"output": "No\n"
},
{
"input": "ctkhagrifaztmnxhmqztzibnmzzkojiztvrkfeoqegvwtbxlvvjhebshqaicsovtkcdovytimjggglyxlvglgunbohnkxargymbqvzgsnvjzgxivdgnaesgxqcruaopjuqsyyorrobnelehjnxcetveehlbmeskptivsuhuqupbieumycwczxyqjtwfofehfkpqmjngygwxkaviuyouiippgvlxjgtkxmhcwtzacbllsybgiujyryngapfwjkkyapfgxtcdpc\nctkhagrifaztmnxhmqztzibnmzzkojiztvrkfeoqegvwtbxlvvjhebshqaicsovtkcdovytimjggglyxlvglgunbohnkxargymbqvzgsnvjzgxivdgnaesgxcetveehlbmeskptivsuhuqupbieumycwczxyqjtwfofehfkpqmjngygwxkaviuyouiippgvlxjgtkxmhcwtzacbllsybgiujyryngapfwjkkyapfgxtcdpc\n",
"output": "No\n"
},
{
"input": "aa\naaa\n",
"output": "No\n"
},
{
"input": "cccbbiiiiiqqvvgggwwwwxxxxxxxxoooondddkkkpvvvdddddooqqxxxxxqqqqllllkkkkkkggggfwwwwwkkkfffeeeemmmmmmmqwwwwwwxxxxxxxdddddqqqqqqq\ncbiqvgwxxondkpvdoqxqlkgfwkfemqwxdq\n",
"output": "Yes\n"
}
] | code_contests | python | 0 | cd845608c82c5e4f5670a6f1c36974ce |
Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place.
To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and then each of them shows some number of fingers on one hand (one to five), and then the boys count in a circle, starting from Dima, the number of people, respective to the total number of fingers shown. The person on who the countdown stops will clean the apartment.
For example, if Dima and one of his friends played hide and seek, and 7 fingers were shown during the counting-out, then Dima would clean the place. If there were 2 or say, 8 fingers shown, then his friend would clean the place.
Dima knows how many fingers each of his friends will show during the counting-out. Now he is interested in the number of ways to show some number of fingers on one hand (one to five), so that he did not have to clean the place. Help Dima.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of Dima's friends. Dima himself isn't considered to be his own friend. The second line contains n positive integers, not exceeding 5, representing, how many fingers the Dima's friends will show.
The numbers in the lines are separated by a single space.
Output
In a single line print the answer to the problem.
Examples
Input
1
1
Output
3
Input
1
2
Output
2
Input
2
3 5
Output
3
Note
In the first sample Dima can show 1, 3 or 5 fingers. If Dima shows 3 fingers, then the counting-out will go like that: Dima, his friend, Dima, his friend.
In the second sample Dima can show 2 or 4 fingers.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def solve(n,d):
other = 0
for i in d:
other += i
ways = 0
for i in range(1,6):
if (other + i) % (n + 1) != 1:
ways += 1
print(ways)
def main():
n = int(input())
d = input()
d = [int(i) for i in d.split()]
solve(n,d)
main() | python | code_algorithm | [
{
"input": "1\n2\n",
"output": "2\n"
},
{
"input": "2\n3 5\n",
"output": "3\n"
},
{
"input": "1\n1\n",
"output": "3\n"
},
{
"input": "49\n1 4 4 3 5 2 2 1 5 1 2 1 2 5 1 4 1 4 5 2 4 5 3 5 2 4 2 1 3 4 2 1 4 2 1 1 3 3 2 3 5 4 3 4 2 4 1 4 1\n",
"output": "5\n"
},
{
"input": "1\n5\n",
"output": "3\n"
},
{
"input": "25\n4 5 5 5 3 1 1 4 4 4 3 5 4 4 1 4 4 1 2 4 2 5 4 5 3\n",
"output": "5\n"
},
{
"input": "100\n4 4 2 5 4 2 2 3 4 4 3 2 3 3 1 3 4 3 3 4 1 3 1 4 5 3 4 3 1 1 1 3 3 2 3 4 3 4 2 2 1 5 1 4 5 1 1 1 3 3 1 1 3 2 5 4 2 5 2 4 5 4 4 1 1 2 1 1 4 5 1 1 5 3 3 2 5 5 5 1 4 1 4 1 1 3 2 3 4 4 2 5 5 2 5 1 1 3 5 3\n",
"output": "5\n"
},
{
"input": "100\n3 3 1 4 2 4 4 3 1 5 1 1 4 4 3 4 4 3 5 4 5 2 4 3 4 1 2 4 5 4 2 1 5 4 1 1 4 3 2 4 1 2 1 4 4 5 5 4 4 5 3 2 5 1 4 2 2 1 1 2 5 2 5 1 5 3 1 4 3 2 4 3 2 2 4 5 5 1 2 3 1 4 1 2 2 2 5 5 2 3 2 4 3 1 1 2 1 2 1 2\n",
"output": "5\n"
},
{
"input": "73\n3 4 3 4 5 1 3 4 2 1 4 2 2 3 5 3 1 4 2 3 2 1 4 5 3 5 2 2 4 3 2 2 5 3 2 3 5 1 3 1 1 4 5 2 4 2 5 1 4 3 1 3 1 4 2 3 3 3 3 5 5 2 5 2 5 4 3 1 1 5 5 2 3\n",
"output": "4\n"
},
{
"input": "100\n2 1 1 3 5 4 4 2 3 4 3 4 5 4 5 4 2 4 5 3 4 5 4 1 1 4 4 1 1 2 5 4 2 4 5 3 2 5 4 3 4 5 1 3 4 2 5 4 5 4 5 2 4 1 2 5 3 1 4 4 5 3 4 3 1 2 5 4 2 5 4 1 5 3 5 4 1 2 5 3 1 1 1 1 5 3 4 3 5 1 1 5 5 1 1 2 2 1 5 1\n",
"output": "5\n"
},
{
"input": "46\n1 4 4 5 4 5 2 3 5 5 3 2 5 4 1 3 2 2 1 4 3 1 5 5 2 2 2 2 4 4 1 1 4 3 4 3 1 4 2 2 4 2 3 2 5 2\n",
"output": "4\n"
},
{
"input": "31\n3 2 3 3 3 3 4 4 1 5 5 4 2 4 3 2 2 1 4 4 1 2 3 1 1 5 5 3 4 4 1\n",
"output": "4\n"
},
{
"input": "6\n2 3 2 2 1 3\n",
"output": "4\n"
},
{
"input": "7\n4 1 3 2 2 4 5\n",
"output": "4\n"
},
{
"input": "100\n5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5\n",
"output": "5\n"
},
{
"input": "41\n5 3 5 4 2 5 4 3 1 1 1 5 4 3 4 3 5 4 2 5 4 1 1 3 2 4 5 3 5 1 5 5 1 1 1 4 4 1 2 4 3\n",
"output": "5\n"
},
{
"input": "23\n5 2 1 1 4 2 5 5 3 5 4 5 5 1 1 5 2 4 5 3 4 4 3\n",
"output": "5\n"
},
{
"input": "93\n1 3 1 4 3 3 5 3 1 4 5 4 3 2 2 4 3 1 4 1 2 3 3 3 2 5 1 3 1 4 5 1 1 1 4 2 1 2 3 1 1 1 5 1 5 5 1 2 5 4 3 2 2 4 4 2 5 4 5 5 3 1 3 1 2 1 3 1 1 2 3 4 4 5 5 3 2 1 3 3 5 1 3 5 4 4 1 3 3 4 2 3 2\n",
"output": "5\n"
},
{
"input": "95\n4 2 3 4 4 5 2 2 4 4 3 5 3 3 3 5 4 2 5 4 2 1 1 3 4 2 1 3 5 4 2 1 1 5 1 1 2 2 4 4 5 4 5 5 2 1 2 2 2 4 5 5 2 4 3 4 4 3 5 2 4 1 5 4 5 1 3 2 4 2 2 1 5 3 1 5 3 4 3 3 2 1 2 2 1 3 1 5 2 3 1 1 2 5 2\n",
"output": "5\n"
},
{
"input": "42\n3 1 2 2 5 1 2 2 4 5 4 5 2 5 4 5 4 4 1 4 3 3 4 4 4 4 3 2 1 3 4 5 5 2 1 2 1 5 5 2 4 4\n",
"output": "5\n"
},
{
"input": "73\n4 1 3 3 3 1 5 2 1 4 1 1 3 5 1 1 4 5 2 1 5 4 1 5 3 1 5 2 4 5 1 4 3 3 5 2 2 3 3 2 5 1 4 5 2 3 1 4 4 3 5 2 3 5 1 4 3 5 1 2 4 1 3 3 5 4 2 4 2 4 1 2 5\n",
"output": "5\n"
},
{
"input": "100\n4 4 3 3 2 5 4 4 2 1 4 4 4 5 4 1 2 1 5 2 4 3 4 1 4 1 2 5 1 4 5 4 2 1 2 5 3 4 5 5 2 1 2 2 2 2 2 3 2 5 1 2 2 3 2 5 5 1 3 4 5 2 1 3 4 2 2 4 4 3 3 3 2 3 2 1 5 5 5 2 1 4 2 3 5 1 4 4 2 3 2 5 5 4 3 5 1 3 5 5\n",
"output": "5\n"
},
{
"input": "6\n4 2 3 1 3 5\n",
"output": "4\n"
},
{
"input": "5\n4 4 3 5 1\n",
"output": "4\n"
},
{
"input": "100\n4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4\n",
"output": "4\n"
},
{
"input": "15\n5 5 5 3 5 4 1 3 3 4 3 4 1 4 4\n",
"output": "5\n"
},
{
"input": "3\n3 5 1\n",
"output": "4\n"
},
{
"input": "8\n2 2 5 3 4 3 3 2\n",
"output": "4\n"
},
{
"input": "96\n1 5 1 3 2 1 2 2 2 2 3 4 1 1 5 4 4 1 2 3 5 1 4 4 4 1 3 3 1 4 5 4 1 3 5 3 4 4 3 2 1 1 4 4 5 1 1 2 5 1 2 3 1 4 1 2 2 2 3 2 3 3 2 5 2 2 3 3 3 3 2 1 2 4 5 5 1 5 3 2 1 4 3 5 5 5 3 3 5 3 4 3 4 2 1 3\n",
"output": "5\n"
}
] | code_contests | python | 0.4 | 699adc9dafe14d7a15538395a79c085d |
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2n members. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member of NFC.
One of the parts of this tradition is one by one dance, where each member of MDC dances with a member of NFC. A dance pair is a pair of numbers (a, b) such that member a from MDC dances with member b from NFC.
The complexity of a pairs' assignment is the number of pairs of dancing pairs (a, b) and (c, d) such that a < c and b > d.
You are given a binary number of length n named x. We know that member i from MDC dances with member <image> from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109 + 7).
Expression <image> denotes applying «XOR» to numbers x and y. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as «^», in Pascal — «xor».
Input
The first line of input contains a binary number x of lenght n, (1 ≤ n ≤ 100).
This number may contain leading zeros.
Output
Print the complexity of the given dance assignent modulo 1000000007 (109 + 7).
Examples
Input
11
Output
6
Input
01
Output
2
Input
1
Output
1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | s = input()
res = pow(2, len(s)-1)*(int(s, 2))
print (res%1000000007) | python | code_algorithm | [
{
"input": "1\n",
"output": "1\n"
},
{
"input": "01\n",
"output": "2\n"
},
{
"input": "11\n",
"output": "6\n"
},
{
"input": "0111001111110010000001111100110100111110001100100001111111110000010010111010010010010111000110001111\n",
"output": "777947548\n"
},
{
"input": "10110\n",
"output": "352\n"
},
{
"input": "0110011110111000001101001010101000011011101001001101000000111101010101111101010011101001111010111001\n",
"output": "416862683\n"
},
{
"input": "100111100\n",
"output": "80896\n"
},
{
"input": "0\n",
"output": "0\n"
},
{
"input": "00001100100101000111111100110010001101001000011110110000\n",
"output": "526794740\n"
},
{
"input": "10100101000010011110101011011110001\n",
"output": "374541417\n"
},
{
"input": "10001010011010010101101010111001001001011110110101011000010100110\n",
"output": "276731670\n"
},
{
"input": "00000000000000000000111111111111111111111111111111111111111111\n",
"output": "738177230\n"
},
{
"input": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n",
"output": "261536897\n"
},
{
"input": "1111111111111111111111111111111111\n",
"output": "68817500\n"
},
{
"input": "1000000001101010101011111001001101011100011000010000100101001111001000110100100001110001100001000001\n",
"output": "759144998\n"
},
{
"input": "1000001010111011110011111110011001011111011001110011100101111110100110111001100001110000011101011011\n",
"output": "928069440\n"
},
{
"input": "11110111000110101111100100111110000011\n",
"output": "448062885\n"
},
{
"input": "1100110010110011001011001100101100110010110011001111001100101100110010110011001011001100101100100010\n",
"output": "499547155\n"
},
{
"input": "10\n",
"output": "4\n"
},
{
"input": "01100010011001101100001000000101001000101101000110011100101101111101010100000011101011100\n",
"output": "67141264\n"
},
{
"input": "00\n",
"output": "0\n"
},
{
"input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n",
"output": "0\n"
},
{
"input": "0000000000000000000000000000000000000\n",
"output": "0\n"
},
{
"input": "1101010110001010100110011011101011010100010001110100010011011100011011000101110001010101110001101011\n",
"output": "383088952\n"
},
{
"input": "11111111111111111111111111111111111000000000000000000000000000\n",
"output": "774857564\n"
},
{
"input": "1000101010000101111110100110011110000011000110001111001001000110110011110110111110100\n",
"output": "532893377\n"
},
{
"input": "01010100001010111111001111001000101010010101000111011011111000\n",
"output": "629793317\n"
}
] | code_contests | python | 0.1 | 3f2777b17bc923331bf3f5c4088e0a7a |
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.
However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements:
1. one resistor;
2. an element and one resistor plugged in sequence;
3. an element and one resistor plugged in parallel.
<image>
With the consecutive connection the resistance of the new element equals R = Re + R0. With the parallel connection the resistance of the new element equals <image>. In this case Re equals the resistance of the element being connected.
Mike needs to assemble an element with a resistance equal to the fraction <image>. Determine the smallest possible number of resistors he needs to make such an element.
Input
The single input line contains two space-separated integers a and b (1 ≤ a, b ≤ 1018). It is guaranteed that the fraction <image> is irreducible. It is guaranteed that a solution always exists.
Output
Print a single number — the answer to the problem.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.
Examples
Input
1 1
Output
1
Input
3 2
Output
3
Input
199 200
Output
200
Note
In the first sample, one resistor is enough.
In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance <image>. We cannot make this element using two resistors.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | I = lambda : list(map(int, input().split(' ')))
a, b = I()
ans = 0
while a > 0 and b > 0 and a//b > 0 or b//a > 0:
ans += a//b
a, b = b, a%b
print(ans) | python | code_algorithm | [
{
"input": "3 2\n",
"output": "3\n"
},
{
"input": "1 1\n",
"output": "1\n"
},
{
"input": "199 200\n",
"output": "200\n"
},
{
"input": "4052739537881 6557470319842\n",
"output": "62\n"
},
{
"input": "1 923438\n",
"output": "923438\n"
},
{
"input": "1 2\n",
"output": "2\n"
},
{
"input": "1 3\n",
"output": "3\n"
},
{
"input": "288565475053 662099878640\n",
"output": "88\n"
},
{
"input": "18 55\n",
"output": "21\n"
},
{
"input": "29906716 35911991\n",
"output": "92\n"
},
{
"input": "10000000000 1000000001\n",
"output": "100000019\n"
},
{
"input": "60236007668635342 110624799949034113\n",
"output": "179\n"
},
{
"input": "4 43470202936783249\n",
"output": "10867550734195816\n"
},
{
"input": "123 1000000000000000000\n",
"output": "8130081300813023\n"
},
{
"input": "1 4\n",
"output": "4\n"
},
{
"input": "13 4\n",
"output": "7\n"
},
{
"input": "999999999999999993 999999999999999991\n",
"output": "499999999999999998\n"
},
{
"input": "2 1\n",
"output": "2\n"
},
{
"input": "2 1000000001\n",
"output": "500000002\n"
},
{
"input": "2 3\n",
"output": "3\n"
},
{
"input": "36 316049483082136289\n",
"output": "8779152307837131\n"
},
{
"input": "999999999999999991 1000000000000000000\n",
"output": "111111111111111120\n"
},
{
"input": "1 1000000000000000000\n",
"output": "1000000000000000000\n"
},
{
"input": "15 110897893734203629\n",
"output": "7393192915613582\n"
},
{
"input": "16 310139055712567491\n",
"output": "19383690982035476\n"
},
{
"input": "3945894354376 1\n",
"output": "3945894354376\n"
},
{
"input": "752278442523506295 52\n",
"output": "14466893125452056\n"
},
{
"input": "1000000000000000000 3\n",
"output": "333333333333333336\n"
},
{
"input": "2 999999999999999999\n",
"output": "500000000000000001\n"
},
{
"input": "498454011879264 806515533049393\n",
"output": "72\n"
},
{
"input": "3 5\n",
"output": "4\n"
},
{
"input": "3 1\n",
"output": "3\n"
},
{
"input": "5 2\n",
"output": "4\n"
},
{
"input": "5 8\n",
"output": "5\n"
},
{
"input": "439910263967866789 38\n",
"output": "11576585893891241\n"
},
{
"input": "2 5\n",
"output": "4\n"
},
{
"input": "679891637638612258 420196140727489673\n",
"output": "86\n"
},
{
"input": "2377 1055\n",
"output": "33\n"
},
{
"input": "999999999999999999 1000000000000000000\n",
"output": "1000000000000000000\n"
},
{
"input": "999999999999999999 5\n",
"output": "200000000000000004\n"
},
{
"input": "9958408561221547 4644682781404278\n",
"output": "196\n"
},
{
"input": "74 99\n",
"output": "28\n"
},
{
"input": "21 8\n",
"output": "7\n"
},
{
"input": "13 21\n",
"output": "7\n"
},
{
"input": "8944394323791464 5527939700884757\n",
"output": "77\n"
},
{
"input": "999999999999999999 2\n",
"output": "500000000000000001\n"
},
{
"input": "4 5\n",
"output": "5\n"
},
{
"input": "44945570212853 72723460248141\n",
"output": "67\n"
},
{
"input": "3 1000000000000000000\n",
"output": "333333333333333336\n"
},
{
"input": "3052460231 856218974\n",
"output": "82\n"
},
{
"input": "645597 134285\n",
"output": "87\n"
},
{
"input": "21 17\n",
"output": "9\n"
},
{
"input": "11504415412768 12754036168327\n",
"output": "163\n"
}
] | code_contests | python | 0.5 | 32545d0679c6069822078564ae51224b |
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.
Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words, <image> , where aj is the taste of the j-th chosen fruit and bj is its calories.
Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands!
Inna loves Dima very much so she wants to make the salad from at least one fruit.
Input
The first line of the input contains two integers n, k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100) — the fruits' tastes. The third line of the input contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 100) — the fruits' calories. Fruit number i has taste ai and calories bi.
Output
If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer — the maximum possible sum of the taste values of the chosen fruits.
Examples
Input
3 2
10 8 1
2 7 1
Output
18
Input
5 3
4 4 4 4 4
2 2 2 2 2
Output
-1
Note
In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition <image> fulfills, that's exactly what Inna wants.
In the second test sample we cannot choose the fruits so as to follow Inna's principle.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from bisect import bisect_right
n, k = map(int, input().split())
t = sorted((u - k * v, v) for u, v in zip(*(map(int, input().split()), map(int, input().split()))))
m = n - bisect_right(t, (0, 0))
l, p, t = 0, [0] * 100001, t[:: -1]
for d, v in t[: m]:
for j in range(l, 0, -1):
if p[j]: p[j + d] = max(p[j + d], p[j] + v)
p[d] = max(p[d], p[0] + v)
l += d
for d, v in t[m: ]:
for j in range(- d, l + 1):
if p[j]: p[j + d] = max(p[j + d], p[j] + v)
print(p[0] * k if p[0] else -1) | python | code_algorithm | [
{
"input": "5 3\n4 4 4 4 4\n2 2 2 2 2\n",
"output": "-1\n"
},
{
"input": "3 2\n10 8 1\n2 7 1\n",
"output": "18\n"
},
{
"input": "1 1\n1\n2\n",
"output": "-1\n"
},
{
"input": "19 2\n68 24 95 24 94 82 37 87 68 67 59 28 68 5 70 53 80 46 61\n60 74 46 9 40 45 58 51 96 4 42 33 12 40 34 9 58 84 91\n",
"output": "816\n"
},
{
"input": "2 1\n75 65\n16 60\n",
"output": "-1\n"
},
{
"input": "21 8\n50 39 28 27 58 46 95 46 50 8 28 94 61 58 57 7 1 38 9 34 12\n94 1 77 1 17 40 99 31 26 1 1 1 15 7 6 1 85 3 32 65 78\n",
"output": "352\n"
},
{
"input": "47 4\n35 64 42 41 61 55 66 16 18 65 50 32 26 80 39 65 78 25 3 29 6 88 3 3 17 36 23 84 60 78 62 36 47 36 90 19 6 46 18 98 35 88 94 26 37 63 88\n1 29 1 1 30 1 1 1 1 37 1 75 2 74 41 1 16 1 56 36 1 3 51 1 13 1 1 1 1 1 1 1 58 90 1 1 1 4 1 1 1 1 67 72 1 1 87\n",
"output": "2044\n"
},
{
"input": "37 10\n29 83 52 50 29 8 24 6 15 95 94 41 2 20 93 86 96 6 64 92 93 73 88 26 91 60 17 4 70 32 89 87 92 89 43 33 94\n81 51 73 43 13 47 6 92 79 3 71 65 1 46 48 68 2 24 17 85 84 61 13 59 21 90 83 6 87 3 3 66 65 14 32 98 21\n",
"output": "520\n"
},
{
"input": "35 6\n99 26 11 66 36 8 38 7 68 23 14 5 89 14 14 95 33 83 74 21 81 98 86 17 16 25 51 44 90 17 12 23 77 15 63\n5 2 33 1 37 77 3 54 2 69 28 2 45 2 60 10 84 26 27 77 95 65 3 5 47 63 86 7 62 64 13 1 2 22 62\n",
"output": "894\n"
},
{
"input": "80 3\n84 61 7 14 79 81 16 61 38 62 16 71 14 6 56 91 91 94 85 52 80 51 97 26 46 39 87 76 69 19 57 54 34 65 49 24 35 20 68 40 92 11 35 32 70 89 83 50 18 67 48 82 65 97 100 70 89 42 40 2 91 29 78 92 11 3 59 84 35 11 90 66 30 61 74 55 83 89 98 51\n93 9 7 95 47 3 19 61 69 10 8 58 49 65 4 45 79 64 30 34 59 1 22 37 1 15 20 72 6 34 51 90 1 77 19 64 41 83 90 71 35 64 18 88 1 86 52 92 88 66 68 43 85 55 60 11 27 56 98 89 53 96 19 97 55 85 38 3 34 59 96 65 51 10 1 3 26 3 6 43\n",
"output": "2793\n"
},
{
"input": "21 6\n1 94 34 73 75 73 7 70 31 73 54 81 78 37 74 82 34 49 67 47 98\n79 77 84 42 28 49 81 98 64 62 83 2 40 92 1 87 86 95 69 45 41\n",
"output": "-1\n"
},
{
"input": "60 3\n97 90 34 70 30 57 18 58 87 93 32 93 14 45 24 97 99 61 75 44 11 62 76 52 29 54 24 8 21 79 10 37 54 2 38 72 65 24 30 42 70 96 71 58 91 1 35 22 43 80 55 26 90 7 17 34 49 12 44 29\n28 63 66 7 64 100 59 51 71 90 14 10 66 86 35 44 16 74 40 3 77 19 51 12 58 71 88 7 74 7 89 28 92 25 4 37 76 33 12 2 62 46 36 23 93 20 86 14 65 69 37 19 47 9 7 25 40 44 30 71\n",
"output": "1374\n"
},
{
"input": "69 8\n2 1 41 1 72 44 75 23 1 76 5 50 92 56 1 34 1 55 66 20 77 92 94 34 76 63 90 25 29 44 68 53 9 54 87 74 2 4 19 36 1 87 36 17 23 14 89 62 52 40 44 74 72 77 69 11 50 69 3 72 3 1 70 96 90 5 25 49 1\n42 1 1 1 85 19 67 1 22 44 84 1 1 69 1 2 1 75 17 3 55 1 12 23 71 33 3 22 1 59 60 1 1 33 1 1 51 33 1 1 1 8 19 1 2 1 62 34 77 36 87 27 17 1 8 1 68 17 1 14 6 16 1 73 1 1 12 94 1\n",
"output": "1808\n"
},
{
"input": "1 1\n1\n1\n",
"output": "1\n"
},
{
"input": "68 6\n32 34 18 21 1 37 55 5 25 1 1 2 57 54 1 1 1 24 1 1 100 1 2 1 1 19 77 53 1 67 76 81 1 38 1 45 54 88 1 29 96 80 100 1 1 1 1 34 80 1 75 76 93 1 63 67 1 92 26 94 55 1 68 76 57 88 87 4\n95 57 1 1 74 70 29 1 1 1 1 1 17 14 97 4 66 14 1 86 94 7 84 84 71 1 96 73 1 12 19 3 80 1 82 3 37 36 39 1 96 1 85 32 75 38 66 4 70 1 3 1 1 1 8 22 1 1 1 1 37 1 65 1 9 1 5 3\n",
"output": "1830\n"
},
{
"input": "55 1\n42 45 79 90 55 14 46 34 98 30 26 100 26 61 52 85 62 26 17 32 23 76 24 35 60 41 2 94 66 16 48 81 81 30 9 23 91 71 62 76 83 10 11 37 15 45 85 31 38 42 42 34 86 49 78\n43 37 78 2 48 79 7 55 47 7 75 78 100 10 11 4 83 82 26 95 70 67 9 34 10 85 32 60 28 98 81 78 52 47 91 51 98 33 26 40 82 46 60 27 75 9 35 11 65 61 28 62 11 95 72\n",
"output": "2671\n"
},
{
"input": "11 5\n29 26 61 52 10 50 26 68 85 93 86\n26 32 1 24 2 1 2 4 2 1 52\n",
"output": "330\n"
},
{
"input": "88 10\n6 64 43 1 1 1 8 15 39 1 95 2 1 80 36 40 25 2 52 24 29 26 16 45 96 99 1 91 16 97 67 1 39 91 1 41 72 67 93 84 1 12 67 53 26 1 14 39 94 92 28 75 10 16 81 97 77 22 1 1 41 90 51 49 90 74 5 61 1 45 88 1 40 7 4 59 16 33 6 4 92 1 38 20 4 53 10 80\n70 45 1 73 52 1 20 78 68 98 1 95 2 61 1 56 5 70 92 1 99 52 84 87 87 1 76 51 30 20 1 12 4 52 80 63 33 1 1 3 1 12 43 29 51 64 1 82 6 81 1 15 93 74 11 1 41 89 40 40 20 6 80 42 1 1 1 83 3 69 42 2 55 37 7 1 1 1 43 79 79 50 79 68 52 1 77 59\n",
"output": "1750\n"
},
{
"input": "42 5\n2 75 38 94 77 91 37 4 50 56 55 31 87 57 7 44 38 71 91 50 77 92 48 28 92 39 79 66 25 85 44 96 30 46 15 48 76 44 48 18 26 48\n90 46 64 99 17 16 43 90 21 50 91 45 20 4 58 41 97 91 85 47 64 90 27 77 14 4 56 37 1 20 15 82 1 85 29 99 16 13 60 69 8 86\n",
"output": "710\n"
},
{
"input": "16 2\n60 5 39 38 43 10 99 2 88 24 2 73 21 57 60 69\n59 92 96 9 1 15 4 42 23 7 100 10 90 97 13 2\n",
"output": "528\n"
},
{
"input": "27 9\n68 3 3 4 87 74 82 49 10 9 15 2 48 3 74 96 3 21 37 44 4 3 14 14 10 16 72\n1 1 59 1 1 97 1 1 40 1 20 1 9 1 70 1 89 1 1 69 40 4 7 65 1 1 2\n",
"output": "621\n"
}
] | code_contests | python | 0.1 | b78bf918590abe9aa17b8ecab1a3f62d |
Two chess pieces, a rook and a knight, stand on a standard chessboard 8 × 8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the other one.
Your task is to find the number of ways to place another knight on the board so that none of the three pieces on the board beat another one. A new piece can only be placed on an empty square.
Input
The first input line contains the description of the rook's position on the board. This description is a line which is 2 in length. Its first symbol is a lower-case Latin letter from a to h, and its second symbol is a number from 1 to 8. The second line contains the description of the knight's position in a similar way. It is guaranteed that their positions do not coincide.
Output
Print a single number which is the required number of ways.
Examples
Input
a1
b2
Output
44
Input
a8
d4
Output
38
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` |
l=[]
for i in range(1,9):
for j in range(1,9):
l.append(int(str(i)+str(j)))
l2=[]
def check(l2,c,d):
if c+1<=8 and d+2<=8:
l2.append(int(str(c+1)+str(d+2)))
if c+2<9 and d+1<9:
l2.append(int(str(c+2)+str(d+1)))
if c-1>0 and d-2>0:
l2.append(int(str(c-1)+str(d-2)))
if c-2>0 and d-1>0:
l2.append(int(str(c-2)+str(d-1)))
if c+1<9 and d-2>0:
l2.append(int(str(c+1)+str(d-2)))
if c-1>0 and d+2<9:
l2.append(int(str(c-1)+str(d+2)))
if c+2<9 and d-1>0:
l2.append(int(str(c+2)+str(d-1)))
if c-2>0 and d+1<9:
l2.append(int(str(c-2)+str(d+1)))
#input
r=input()
k=input()
r1=str(ord(r[0])-96)+r[1]
k1=str(ord(k[0])-96)+k[1]
#for rook given
a,b=int(r1[0]),int(r1[1])
# print(a,b)
for i in range(1,9):
for j in range(1,9):
if i==a or j==b:
l2.append(int(str(i)+str(j)))
check(l2,a,b)
#for knight
c,d=int(k1[0]),int(k1[1])
check(l2,c,d)
l2.append(int(str(c)+str(d))) #pos of curr knight
#answer
print(64-len(set(l2)))
| python | code_algorithm | [
{
"input": "a1\nb2\n",
"output": "44\n"
},
{
"input": "a8\nd4\n",
"output": "38\n"
},
{
"input": "c1\nd2\n",
"output": "42\n"
},
{
"input": "g1\ne6\n",
"output": "39\n"
},
{
"input": "h8\ng2\n",
"output": "43\n"
},
{
"input": "a8\nf1\n",
"output": "42\n"
},
{
"input": "e4\nc1\n",
"output": "37\n"
},
{
"input": "b6\ng7\n",
"output": "39\n"
},
{
"input": "a4\nd1\n",
"output": "42\n"
},
{
"input": "h6\na1\n",
"output": "42\n"
},
{
"input": "e3\ng5\n",
"output": "38\n"
},
{
"input": "g2\nb7\n",
"output": "40\n"
},
{
"input": "b2\ne1\n",
"output": "43\n"
},
{
"input": "e1\nd6\n",
"output": "38\n"
},
{
"input": "e8\nb6\n",
"output": "40\n"
},
{
"input": "b6\nd8\n",
"output": "41\n"
},
{
"input": "a1\nb7\n",
"output": "43\n"
},
{
"input": "e6\nf2\n",
"output": "35\n"
},
{
"input": "g8\nb7\n",
"output": "42\n"
},
{
"input": "e5\nh6\n",
"output": "39\n"
},
{
"input": "b5\nh8\n",
"output": "40\n"
},
{
"input": "e1\na7\n",
"output": "41\n"
},
{
"input": "g5\nd2\n",
"output": "38\n"
},
{
"input": "c8\ne6\n",
"output": "40\n"
},
{
"input": "c8\nb2\n",
"output": "41\n"
},
{
"input": "a3\nc8\n",
"output": "41\n"
},
{
"input": "b5\nc1\n",
"output": "39\n"
},
{
"input": "g6\nb7\n",
"output": "39\n"
},
{
"input": "h1\ng5\n",
"output": "42\n"
},
{
"input": "h6\na8\n",
"output": "43\n"
},
{
"input": "f8\nh3\n",
"output": "42\n"
},
{
"input": "c3\na8\n",
"output": "39\n"
},
{
"input": "c6\nb5\n",
"output": "39\n"
},
{
"input": "a8\nb7\n",
"output": "44\n"
},
{
"input": "f1\nc3\n",
"output": "38\n"
},
{
"input": "a3\nd6\n",
"output": "38\n"
},
{
"input": "a2\nh8\n",
"output": "43\n"
},
{
"input": "g3\nh4\n",
"output": "42\n"
},
{
"input": "f8\na3\n",
"output": "40\n"
},
{
"input": "a3\nc5\n",
"output": "40\n"
}
] | code_contests | python | 0 | 394a1dc0cbd3602fa1a2799e3376b500 |
Not so long ago as a result of combat operations the main Berland place of interest — the magic clock — was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin corresponds to the clock center. The clock was painted two colors as is shown in the picture:
<image>
The picture shows only the central part of the clock. This coloring naturally extends to infinity.
The balls can be taken to be points on the plane. Your task is to find the color of the area, damaged by the given ball.
All the points located on the border of one of the areas have to be considered painted black.
Input
The first and single line contains two integers x and y — the coordinates of the hole made in the clock by the ball. Each of the numbers x and y has an absolute value that does not exceed 1000.
Output
Find the required color.
All the points between which and the origin of coordinates the distance is integral-value are painted black.
Examples
Input
-2 1
Output
white
Input
2 1
Output
black
Input
4 3
Output
black
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | # _
#####################################################################################################################
from math import sqrt, ceil
def colorOfDamagedArea(x, y):
location = sqrt(x*x+y*y)
area_sBorder = ceil(location)
if location == area_sBorder:
return 'black'
area_sAddress = x*y
area_sColorCode = area_sBorder%2
if area_sAddress > 0 and area_sColorCode or area_sAddress < 0 and not area_sColorCode:
return 'black'
return 'white'
print(colorOfDamagedArea(*map(int, input().split())))
| python | code_algorithm | [
{
"input": "-2 1\n",
"output": "white\n"
},
{
"input": "4 3\n",
"output": "black\n"
},
{
"input": "2 1\n",
"output": "black\n"
},
{
"input": "-12 -35\n",
"output": "black\n"
},
{
"input": "-215 -996\n",
"output": "black\n"
},
{
"input": "-96 -556\n",
"output": "black\n"
},
{
"input": "-1000 0\n",
"output": "black\n"
},
{
"input": "20 -21\n",
"output": "black\n"
},
{
"input": "0 0\n",
"output": "black\n"
},
{
"input": "207 -224\n",
"output": "black\n"
},
{
"input": "-668 970\n",
"output": "black\n"
},
{
"input": "-496 -644\n",
"output": "black\n"
},
{
"input": "253 -204\n",
"output": "black\n"
},
{
"input": "-129 489\n",
"output": "black\n"
},
{
"input": "15 -8\n",
"output": "black\n"
},
{
"input": "-589 952\n",
"output": "black\n"
},
{
"input": "0 2\n",
"output": "black\n"
},
{
"input": "-216 -90\n",
"output": "black\n"
},
{
"input": "-72 -646\n",
"output": "black\n"
},
{
"input": "280 342\n",
"output": "black\n"
},
{
"input": "0 1000\n",
"output": "black\n"
},
{
"input": "875 -129\n",
"output": "white\n"
},
{
"input": "-201 278\n",
"output": "black\n"
},
{
"input": "-196 -484\n",
"output": "black\n"
},
{
"input": "-4 -4\n",
"output": "white\n"
},
{
"input": "-677 492\n",
"output": "white\n"
},
{
"input": "377 -902\n",
"output": "black\n"
},
{
"input": "-441 572\n",
"output": "white\n"
},
{
"input": "217 221\n",
"output": "white\n"
},
{
"input": "4 4\n",
"output": "white\n"
},
{
"input": "117 -44\n",
"output": "black\n"
},
{
"input": "3 3\n",
"output": "black\n"
},
{
"input": "351 -280\n",
"output": "black\n"
},
{
"input": "1000 -1000\n",
"output": "white\n"
},
{
"input": "-211 243\n",
"output": "black\n"
},
{
"input": "0 -1000\n",
"output": "black\n"
},
{
"input": "12 5\n",
"output": "black\n"
},
{
"input": "-796 -365\n",
"output": "white\n"
},
{
"input": "658 198\n",
"output": "white\n"
},
{
"input": "72 -154\n",
"output": "black\n"
},
{
"input": "128 -504\n",
"output": "black\n"
},
{
"input": "4 -4\n",
"output": "black\n"
},
{
"input": "902 479\n",
"output": "white\n"
},
{
"input": "-206 -518\n",
"output": "white\n"
},
{
"input": "-673 -270\n",
"output": "white\n"
},
{
"input": "-4 4\n",
"output": "black\n"
},
{
"input": "0 72\n",
"output": "black\n"
},
{
"input": "12 -5\n",
"output": "black\n"
},
{
"input": "189 -387\n",
"output": "white\n"
},
{
"input": "64 0\n",
"output": "black\n"
},
{
"input": "132 224\n",
"output": "black\n"
},
{
"input": "-399 -40\n",
"output": "black\n"
},
{
"input": "165 -738\n",
"output": "white\n"
},
{
"input": "0 1\n",
"output": "black\n"
},
{
"input": "-180 -432\n",
"output": "black\n"
},
{
"input": "61 -175\n",
"output": "black\n"
},
{
"input": "-469 -36\n",
"output": "black\n"
},
{
"input": "-281 -552\n",
"output": "white\n"
},
{
"input": "17 144\n",
"output": "black\n"
},
{
"input": "847 -294\n",
"output": "white\n"
},
{
"input": "8 882\n",
"output": "black\n"
},
{
"input": "60 -448\n",
"output": "black\n"
},
{
"input": "-220 208\n",
"output": "white\n"
},
{
"input": "-40 198\n",
"output": "black\n"
},
{
"input": "-693 -929\n",
"output": "white\n"
},
{
"input": "555 319\n",
"output": "black\n"
},
{
"input": "168 -26\n",
"output": "black\n"
},
{
"input": "-42 389\n",
"output": "black\n"
},
{
"input": "-192 -256\n",
"output": "black\n"
},
{
"input": "1000 0\n",
"output": "black\n"
}
] | code_contests | python | 0 | 8b049b4dee9f23fbe804023088b38846 |
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.
The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.
Input
The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.
Output
Print a single integer, denoting the minimum possible height of the tallest tower.
Examples
Input
1 3
Output
9
Input
3 2
Output
8
Input
5 0
Output
10
Note
In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.
In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n, m = map(int, input().split())
x = max(n*2, m*3)
while x//2+x//3-x//6 < m+n:
x += 1
print(x) | python | code_algorithm | [
{
"input": "3 2\n",
"output": "8\n"
},
{
"input": "5 0\n",
"output": "10\n"
},
{
"input": "1 3\n",
"output": "9\n"
},
{
"input": "4 2\n",
"output": "9\n"
},
{
"input": "984327 24352\n",
"output": "1968654\n"
},
{
"input": "918273 219\n",
"output": "1836546\n"
},
{
"input": "651 420\n",
"output": "1606\n"
},
{
"input": "5 3\n",
"output": "12\n"
},
{
"input": "6 4\n",
"output": "15\n"
},
{
"input": "192056 131545\n",
"output": "485402\n"
},
{
"input": "108752 129872\n",
"output": "389616\n"
},
{
"input": "1285 877\n",
"output": "3243\n"
},
{
"input": "817544 553980\n",
"output": "2057286\n"
},
{
"input": "500000 167000\n",
"output": "1000500\n"
},
{
"input": "961920 647392\n",
"output": "2413968\n"
},
{
"input": "1515 1415\n",
"output": "4395\n"
},
{
"input": "32 16\n",
"output": "72\n"
},
{
"input": "8 9\n",
"output": "27\n"
},
{
"input": "0 5\n",
"output": "15\n"
},
{
"input": "15 10\n",
"output": "38\n"
},
{
"input": "987521 53\n",
"output": "1975042\n"
},
{
"input": "129439 98443\n",
"output": "341823\n"
},
{
"input": "300 200\n",
"output": "750\n"
},
{
"input": "1 1\n",
"output": "3\n"
},
{
"input": "1184 868\n",
"output": "3078\n"
},
{
"input": "300000 200000\n",
"output": "750000\n"
},
{
"input": "1 0\n",
"output": "2\n"
},
{
"input": "185253 152723\n",
"output": "506964\n"
},
{
"input": "50 30\n",
"output": "120\n"
},
{
"input": "65 56\n",
"output": "182\n"
},
{
"input": "9 4\n",
"output": "20\n"
},
{
"input": "8 5\n",
"output": "20\n"
},
{
"input": "0 1\n",
"output": "3\n"
},
{
"input": "789123 0\n",
"output": "1578246\n"
},
{
"input": "18 12\n",
"output": "45\n"
},
{
"input": "500000 166000\n",
"output": "1000000\n"
},
{
"input": "14 42\n",
"output": "126\n"
},
{
"input": "143568 628524\n",
"output": "1885572\n"
},
{
"input": "500000 500100\n",
"output": "1500300\n"
},
{
"input": "163706 157895\n",
"output": "482402\n"
},
{
"input": "195173 150801\n",
"output": "518961\n"
},
{
"input": "825496 807050\n",
"output": "2448819\n"
},
{
"input": "13 10\n",
"output": "34\n"
},
{
"input": "813242 543613\n",
"output": "2035282\n"
},
{
"input": "8 6\n",
"output": "21\n"
},
{
"input": "4 3\n",
"output": "10\n"
},
{
"input": "1000000 1\n",
"output": "2000000\n"
},
{
"input": "5 4\n",
"output": "14\n"
},
{
"input": "968867 651952\n",
"output": "2431228\n"
},
{
"input": "7 4\n",
"output": "16\n"
},
{
"input": "19170 15725\n",
"output": "52342\n"
},
{
"input": "1000000 1000000\n",
"output": "3000000\n"
},
{
"input": "9 5\n",
"output": "21\n"
},
{
"input": "1000000 999999\n",
"output": "2999998\n"
},
{
"input": "820189 548173\n",
"output": "2052543\n"
},
{
"input": "10 6\n",
"output": "24\n"
},
{
"input": "6 3\n",
"output": "14\n"
},
{
"input": "969872 899794\n",
"output": "2804499\n"
},
{
"input": "1083 724\n",
"output": "2710\n"
},
{
"input": "928375 1253\n",
"output": "1856750\n"
},
{
"input": "967227 894524\n",
"output": "2792626\n"
},
{
"input": "123456 1\n",
"output": "246912\n"
},
{
"input": "197973 140806\n",
"output": "508168\n"
},
{
"input": "7 3\n",
"output": "15\n"
},
{
"input": "999999 888888\n",
"output": "2833330\n"
},
{
"input": "234 298374\n",
"output": "895122\n"
},
{
"input": "818549 720669\n",
"output": "2308827\n"
},
{
"input": "500000 499000\n",
"output": "1498500\n"
},
{
"input": "3000 2000\n",
"output": "7500\n"
},
{
"input": "1000000 666667\n",
"output": "2500000\n"
},
{
"input": "999999 1000000\n",
"output": "3000000\n"
},
{
"input": "10 988723\n",
"output": "2966169\n"
},
{
"input": "12345 981732\n",
"output": "2945196\n"
},
{
"input": "974174 827926\n",
"output": "2703150\n"
},
{
"input": "2 999123\n",
"output": "2997369\n"
},
{
"input": "0 1000000\n",
"output": "3000000\n"
},
{
"input": "1 1000000\n",
"output": "3000000\n"
},
{
"input": "2365 981235\n",
"output": "2943705\n"
},
{
"input": "175983 870607\n",
"output": "2611821\n"
}
] | code_contests | python | 0 | 6b3b810920614438665d0486235b12d3 |
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every a minutes, but a train goes to Masha's direction every b minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often.
Input
The first line contains two integers a and b (a ≠ b, 1 ≤ a, b ≤ 106).
Output
Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency.
Examples
Input
3 7
Output
Dasha
Input
5 3
Output
Masha
Input
2 3
Output
Equal
Note
Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time — three minutes for each one, thus, Vasya will go to both girlfriends equally often.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | a,b=map(int,input().split())
if(a==b):
print('Equal')
exit()
import math
lcm=(a*b)//(math.gcd(a,b))
if(a<b):
da=(lcm//a)-1
ma=lcm//b
if(da>ma):
print('Dasha')
elif(da<ma):
print('Masha')
else:
print('Equal')
else:
da=(lcm//a)
ma=(lcm//b)-1
if(da>ma):
print('Dasha')
elif(da<ma):
print('Masha')
else:
print('Equal')
| python | code_algorithm | [
{
"input": "2 3\n",
"output": "Equal\n"
},
{
"input": "3 7\n",
"output": "Dasha\n"
},
{
"input": "5 3\n",
"output": "Masha\n"
},
{
"input": "999983 999979\n",
"output": "Masha\n"
},
{
"input": "419 430\n",
"output": "Dasha\n"
},
{
"input": "51291 51292\n",
"output": "Equal\n"
},
{
"input": "99087 99090\n",
"output": "Equal\n"
},
{
"input": "906 912\n",
"output": "Equal\n"
},
{
"input": "5036 5037\n",
"output": "Equal\n"
},
{
"input": "9886 8671\n",
"output": "Masha\n"
},
{
"input": "450766 610961\n",
"output": "Dasha\n"
},
{
"input": "999999 1000000\n",
"output": "Equal\n"
},
{
"input": "99207 30728\n",
"output": "Masha\n"
},
{
"input": "664690 630787\n",
"output": "Masha\n"
},
{
"input": "999997 1000000\n",
"output": "Dasha\n"
},
{
"input": "638067 409048\n",
"output": "Masha\n"
},
{
"input": "7660 7658\n",
"output": "Equal\n"
},
{
"input": "2661 8975\n",
"output": "Dasha\n"
},
{
"input": "461363 256765\n",
"output": "Masha\n"
},
{
"input": "9632 9640\n",
"output": "Equal\n"
},
{
"input": "30362 10712\n",
"output": "Masha\n"
},
{
"input": "32 99\n",
"output": "Dasha\n"
},
{
"input": "638 619\n",
"output": "Masha\n"
},
{
"input": "77 4\n",
"output": "Masha\n"
},
{
"input": "397 568\n",
"output": "Dasha\n"
},
{
"input": "31 88\n",
"output": "Dasha\n"
},
{
"input": "779183 786727\n",
"output": "Dasha\n"
},
{
"input": "64854 77725\n",
"output": "Dasha\n"
},
{
"input": "1 2\n",
"output": "Equal\n"
},
{
"input": "929061 929052\n",
"output": "Equal\n"
},
{
"input": "552 551\n",
"output": "Equal\n"
},
{
"input": "27 1\n",
"output": "Masha\n"
},
{
"input": "716249 716248\n",
"output": "Equal\n"
},
{
"input": "996219 996216\n",
"output": "Equal\n"
},
{
"input": "983794 986389\n",
"output": "Dasha\n"
},
{
"input": "3647 7698\n",
"output": "Dasha\n"
},
{
"input": "52 53\n",
"output": "Equal\n"
},
{
"input": "55381 55382\n",
"output": "Equal\n"
},
{
"input": "84 11\n",
"output": "Masha\n"
},
{
"input": "20393 86640\n",
"output": "Dasha\n"
},
{
"input": "8 75\n",
"output": "Dasha\n"
},
{
"input": "9846 9844\n",
"output": "Equal\n"
},
{
"input": "22 332\n",
"output": "Dasha\n"
},
{
"input": "1000000 999993\n",
"output": "Masha\n"
},
{
"input": "2 1\n",
"output": "Equal\n"
},
{
"input": "4982 2927\n",
"output": "Masha\n"
},
{
"input": "77545 13842\n",
"output": "Masha\n"
},
{
"input": "876 218\n",
"output": "Masha\n"
},
{
"input": "251 9731\n",
"output": "Dasha\n"
},
{
"input": "999 996\n",
"output": "Equal\n"
},
{
"input": "393 325\n",
"output": "Masha\n"
},
{
"input": "652 653\n",
"output": "Equal\n"
},
{
"input": "4 6\n",
"output": "Equal\n"
},
{
"input": "91560 91550\n",
"output": "Equal\n"
},
{
"input": "782250 782252\n",
"output": "Equal\n"
},
{
"input": "4965 85708\n",
"output": "Dasha\n"
},
{
"input": "8545 7312\n",
"output": "Masha\n"
}
] | code_contests | python | 0 | 08549fe0926cb6d306eb9c430e99d931 |
Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai.
Mishka can put a box i into another box j if the following conditions are met:
* i-th box is not put into another box;
* j-th box doesn't contain any other boxes;
* box i is smaller than box j (ai < aj).
Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box.
Help Mishka to determine the minimum possible number of visible boxes!
Input
The first line contains one integer n (1 ≤ n ≤ 5000) — the number of boxes Mishka has got.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the side length of i-th box.
Output
Print the minimum possible number of visible boxes.
Examples
Input
3
1 2 3
Output
1
Input
4
4 2 4 3
Output
2
Note
In the first example it is possible to put box 1 into box 2, and 2 into 3.
In the second example Mishka can put box 2 into box 3, and box 4 into box 1.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from sys import stdin,stdout
from collections import Counter
def ai(): return list(map(int, stdin.readline().split()))
def ei(): return map(int, stdin.readline().split())
def ip(): return int(stdin.readline().strip())
def op(ans): return stdout.write(str(ans) + '\n')
t = ip()
li = ai()
x = max(li)
c = Counter(li).values()
print(max(c))
| python | code_algorithm | [
{
"input": "3\n1 2 3\n",
"output": "1\n"
},
{
"input": "4\n4 2 4 3\n",
"output": "2\n"
},
{
"input": "10\n58 58 58 58 58 58 58 58 58 58\n",
"output": "10\n"
},
{
"input": "8\n1 2 3 4 5 6 7 8\n",
"output": "1\n"
},
{
"input": "1\n2\n",
"output": "1\n"
},
{
"input": "1\n131\n",
"output": "1\n"
},
{
"input": "11\n1 1 1 1 1 1 1 1 1 1 1\n",
"output": "11\n"
},
{
"input": "10\n86 89 89 86 86 89 86 86 89 89\n",
"output": "5\n"
},
{
"input": "8\n1 1 1 1 1 1 1 1\n",
"output": "8\n"
},
{
"input": "1\n1\n",
"output": "1\n"
},
{
"input": "1\n5\n",
"output": "1\n"
},
{
"input": "9\n1 1 1 1 1 1 1 1 1\n",
"output": "9\n"
},
{
"input": "100\n981 288 186 186 292 876 341 288 981 360 783 907 292 186 341 292 360 876 360 360 981 398 783 288 292 398 876 981 398 907 783 360 288 981 907 186 360 288 186 981 186 288 907 876 288 907 876 360 341 292 907 783 907 783 292 981 907 292 876 398 783 876 398 341 876 186 288 186 981 341 398 360 907 981 341 186 292 981 292 398 876 783 292 186 360 292 288 292 876 398 288 292 341 288 398 360 360 292 981 360\n",
"output": "14\n"
},
{
"input": "5\n1 1 1 1 1\n",
"output": "5\n"
},
{
"input": "1\n9\n",
"output": "1\n"
}
] | code_contests | python | 0.1 | f58ce7582a2a67d55225890c5fd623e2 |
In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong — the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure — and then terrible Game Cubes fall down on the city, bringing death to those modules, who cannot win the game...
For sure, as guard Bob appeared in Mainframe many modules stopped fearing Game Cubes. Because Bob (as he is alive yet) has never been defeated by User, and he always meddles with Game Cubes, because he is programmed to this.
However, unpleasant situations can happen, when a Game Cube falls down on Lost Angles. Because there lives a nasty virus — Hexadecimal, who is... mmm... very strange. And she likes to play very much. So, willy-nilly, Bob has to play with her first, and then with User.
This time Hexadecimal invented the following entertainment: Bob has to leap over binary search trees with n nodes. We should remind you that a binary search tree is a binary tree, each node has a distinct key, for each node the following is true: the left sub-tree of a node contains only nodes with keys less than the node's key, the right sub-tree of a node contains only nodes with keys greater than the node's key. All the keys are different positive integer numbers from 1 to n. Each node of such a tree can have up to two children, or have no children at all (in the case when a node is a leaf).
In Hexadecimal's game all the trees are different, but the height of each is not lower than h. In this problem «height» stands for the maximum amount of nodes on the way from the root to the remotest leaf, the root node and the leaf itself included. When Bob leaps over a tree, it disappears. Bob gets the access to a Cube, when there are no trees left. He knows how many trees he will have to leap over in the worst case. And you?
Input
The input data contains two space-separated positive integer numbers n and h (n ≤ 35, h ≤ n).
Output
Output one number — the answer to the problem. It is guaranteed that it does not exceed 9·1018.
Examples
Input
3 2
Output
5
Input
3 3
Output
4
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n,h=map(int,input().split())
L=[]
for i in range(n+1):
g=[]
for j in range(n+1):
g.append(0)
L.append(g)
L[0][0]=1
for i in range(1,n+1):
for j in range(1,n+1):
sumu=0
for m in range(1,i+1):
t1=L[m-1][j-1]
tot=0
for k in range(0,j):
tot+=L[i-m][k]
t1*=tot
t2=L[i-m][j-1]
tot=0
for k in range(0,j-1):
tot+=L[m-1][k]
t2*=tot
sumu+=(t1+t2)
L[i][j]=sumu
ans=0
for i in range(h,n+1):
ans+=L[n][i]
print(ans)
| python | code_algorithm | [
{
"input": "3 2\n",
"output": "5\n"
},
{
"input": "3 3\n",
"output": "4\n"
},
{
"input": "21 12\n",
"output": "12153990144\n"
},
{
"input": "23 21\n",
"output": "275251200\n"
},
{
"input": "16 5\n",
"output": "35357670\n"
},
{
"input": "14 11\n",
"output": "488448\n"
},
{
"input": "16 11\n",
"output": "11819008\n"
},
{
"input": "8 5\n",
"output": "1336\n"
},
{
"input": "28 23\n",
"output": "739948625920\n"
},
{
"input": "7 7\n",
"output": "64\n"
},
{
"input": "35 35\n",
"output": "17179869184\n"
},
{
"input": "23 17\n",
"output": "19649347584\n"
},
{
"input": "22 22\n",
"output": "2097152\n"
},
{
"input": "32 27\n",
"output": "22643872890880\n"
},
{
"input": "15 5\n",
"output": "9694844\n"
},
{
"input": "7 4\n",
"output": "428\n"
},
{
"input": "27 11\n",
"output": "61162698256896\n"
},
{
"input": "24 20\n",
"output": "8171945984\n"
},
{
"input": "33 17\n",
"output": "75307983624118272\n"
},
{
"input": "10 10\n",
"output": "512\n"
},
{
"input": "33 26\n",
"output": "434871797284864\n"
},
{
"input": "19 2\n",
"output": "1767263190\n"
},
{
"input": "12 8\n",
"output": "127200\n"
},
{
"input": "35 1\n",
"output": "3116285494907301262\n"
},
{
"input": "16 14\n",
"output": "1032192\n"
},
{
"input": "4 3\n",
"output": "14\n"
},
{
"input": "27 15\n",
"output": "25162319484928\n"
},
{
"input": "19 18\n",
"output": "2424832\n"
},
{
"input": "16 9\n",
"output": "25607552\n"
},
{
"input": "33 21\n",
"output": "14830955929665536\n"
},
{
"input": "35 13\n",
"output": "2690352397519398400\n"
},
{
"input": "4 4\n",
"output": "8\n"
},
{
"input": "20 14\n",
"output": "1094473728\n"
},
{
"input": "34 1\n",
"output": "812944042149730764\n"
},
{
"input": "33 18\n",
"output": "54307238601375744\n"
},
{
"input": "27 25\n",
"output": "6081740800\n"
},
{
"input": "10 2\n",
"output": "16796\n"
},
{
"input": "29 14\n",
"output": "577801978306560\n"
},
{
"input": "33 4\n",
"output": "212336130412243110\n"
},
{
"input": "9 4\n",
"output": "4862\n"
},
{
"input": "9 1\n",
"output": "4862\n"
},
{
"input": "2 1\n",
"output": "2\n"
},
{
"input": "2 2\n",
"output": "2\n"
},
{
"input": "21 18\n",
"output": "211156992\n"
},
{
"input": "1 1\n",
"output": "1\n"
},
{
"input": "30 16\n",
"output": "1375710400053248\n"
},
{
"input": "4 1\n",
"output": "14\n"
},
{
"input": "7 3\n",
"output": "429\n"
}
] | code_contests | python | 0 | 3eb725abe3f12899c65c8338be38076e |
Vitya has just started learning Berlanese language. It is known that Berlanese uses the Latin alphabet. Vowel letters are "a", "o", "u", "i", and "e". Other letters are consonant.
In Berlanese, there has to be a vowel after every consonant, but there can be any letter after any vowel. The only exception is a consonant "n"; after this letter, there can be any letter (not only a vowel) or there can be no letter at all. For example, the words "harakiri", "yupie", "man", and "nbo" are Berlanese while the words "horse", "king", "my", and "nz" are not.
Help Vitya find out if a word s is Berlanese.
Input
The first line of the input contains the string s consisting of |s| (1≤ |s|≤ 100) lowercase Latin letters.
Output
Print "YES" (without quotes) if there is a vowel after every consonant except "n", otherwise print "NO".
You can print each letter in any case (upper or lower).
Examples
Input
sumimasen
Output
YES
Input
ninja
Output
YES
Input
codeforces
Output
NO
Note
In the first and second samples, a vowel goes after each consonant except "n", so the word is Berlanese.
In the third sample, the consonant "c" goes after the consonant "r", and the consonant "s" stands on the end, so the word is not Berlanese.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | s=input()
a=['a','e','i','o','u']
c=0
for i in range(len(s)):
if(i==len(s)-1):
if s[i] in a or s[i]=='n':
c+=1
continue
elif s[i] in a:
c+=1
continue
elif(s[i]=='n'):
c+=1
continue
else:
if s[i+1] in a:
c+=1
continue
if(c==len(s)):
print("YES")
else:
print("NO") | python | code_algorithm | [
{
"input": "codeforces\n",
"output": "NO\n"
},
{
"input": "ninja\n",
"output": "YES\n"
},
{
"input": "sumimasen\n",
"output": "YES\n"
},
{
"input": "s\n",
"output": "NO\n"
},
{
"input": "d\n",
"output": "NO\n"
},
{
"input": "anujemogawautiedoneobninnibonuunaoennnyoorufonxionntinimiboonununnnnnleenqunminzayoutanlalo\n",
"output": "NO\n"
},
{
"input": "uixinnepnlinqaingieianndeakuniooudidonnnqeaituioeneiroionxuowudiooonayenfeonuino\n",
"output": "NO\n"
},
{
"input": "king\n",
"output": "NO\n"
},
{
"input": "z\n",
"output": "NO\n"
},
{
"input": "yy\n",
"output": "NO\n"
},
{
"input": "aaz\n",
"output": "NO\n"
},
{
"input": "nbaaaaaaaa\n",
"output": "YES\n"
},
{
"input": "pya\n",
"output": "NO\n"
},
{
"input": "aeionnhhhn\n",
"output": "NO\n"
},
{
"input": "ab\n",
"output": "NO\n"
},
{
"input": "nb\n",
"output": "NO\n"
},
{
"input": "by\n",
"output": "NO\n"
},
{
"input": "nzzen\n",
"output": "NO\n"
},
{
"input": "bbaaaaaaaa\n",
"output": "NO\n"
},
{
"input": "npn\n",
"output": "NO\n"
},
{
"input": "ny\n",
"output": "NO\n"
},
{
"input": "yn\n",
"output": "NO\n"
},
{
"input": "n\n",
"output": "YES\n"
},
{
"input": "auuaoonntanonnuewannnnpuuinniwoonennyolonnnvienonpoujinndinunnenannmuveoiuuhikucuziuhunnnmunzancenen\n",
"output": "YES\n"
},
{
"input": "x\n",
"output": "NO\n"
},
{
"input": "aaaaaaaaan\n",
"output": "YES\n"
},
{
"input": "aeo\n",
"output": "YES\n"
},
{
"input": "a\n",
"output": "YES\n"
},
{
"input": "aaaaaaaaaa\n",
"output": "YES\n"
},
{
"input": "y\n",
"output": "NO\n"
},
{
"input": "nbn\n",
"output": "NO\n"
},
{
"input": "nn\n",
"output": "YES\n"
},
{
"input": "aaab\n",
"output": "NO\n"
},
{
"input": "baaaaaaaaa\n",
"output": "YES\n"
},
{
"input": "nz\n",
"output": "NO\n"
},
{
"input": "nnnzaaa\n",
"output": "YES\n"
},
{
"input": "aucunuohja\n",
"output": "NO\n"
},
{
"input": "at\n",
"output": "NO\n"
},
{
"input": "b\n",
"output": "NO\n"
},
{
"input": "nnnnnyigaveteononnnnxaalenxuiiwannntoxonyoqonlejuoxuoconnnentoinnul\n",
"output": "NO\n"
},
{
"input": "p\n",
"output": "NO\n"
},
{
"input": "kini\n",
"output": "YES\n"
},
{
"input": "ndonneasoiunhomuunnhuitonnntunntoanerekonoupunanuauenu\n",
"output": "YES\n"
},
{
"input": "h\n",
"output": "NO\n"
},
{
"input": "aaaaaaaaab\n",
"output": "NO\n"
},
{
"input": "az\n",
"output": "NO\n"
},
{
"input": "m\n",
"output": "NO\n"
},
{
"input": "aaaaaaaak\n",
"output": "NO\n"
},
{
"input": "naaaaaaaaa\n",
"output": "YES\n"
},
{
"input": "aab\n",
"output": "NO\n"
},
{
"input": "necnei\n",
"output": "NO\n"
},
{
"input": "nternn\n",
"output": "NO\n"
},
{
"input": "eonwonojannonnufimiiniewuqaienokacevecinfuqihatenhunliquuyebayiaenifuexuanenuaounnboancaeowonu\n",
"output": "YES\n"
},
{
"input": "zn\n",
"output": "NO\n"
},
{
"input": "aaaaaak\n",
"output": "NO\n"
},
{
"input": "g\n",
"output": "NO\n"
},
{
"input": "bnaaaaaaaa\n",
"output": "NO\n"
}
] | code_contests | python | 0.6 | ae5e24b0d333d0dd193c6d376ff14483 |
Two integer sequences existed initially — one of them was strictly increasing, and the other one — strictly decreasing.
Strictly increasing sequence is a sequence of integers [x_1 < x_2 < ... < x_k]. And strictly decreasing sequence is a sequence of integers [y_1 > y_2 > ... > y_l]. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
They were merged into one sequence a. After that sequence a got shuffled. For example, some of the possible resulting sequences a for an increasing sequence [1, 3, 4] and a decreasing sequence [10, 4, 2] are sequences [1, 2, 3, 4, 4, 10] or [4, 2, 1, 10, 4, 3].
This shuffled sequence a is given in the input.
Your task is to find any two suitable initial sequences. One of them should be strictly increasing and the other one — strictly decreasing. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO".
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 2 ⋅ 10^5), where a_i is the i-th element of a.
Output
If there is a contradiction in the input and it is impossible to split the given sequence a to increasing and decreasing sequences, print "NO" in the first line.
Otherwise print "YES" in the first line and any two suitable sequences. Note that the empty sequence and the sequence consisting of one element can be considered as increasing or decreasing.
In the second line print n_i — the number of elements in the strictly increasing sequence. n_i can be zero, in this case the increasing sequence is empty.
In the third line print n_i integers inc_1, inc_2, ..., inc_{n_i} in the increasing order of its values (inc_1 < inc_2 < ... < inc_{n_i}) — the strictly increasing sequence itself. You can keep this line empty if n_i = 0 (or just print the empty line).
In the fourth line print n_d — the number of elements in the strictly decreasing sequence. n_d can be zero, in this case the decreasing sequence is empty.
In the fifth line print n_d integers dec_1, dec_2, ..., dec_{n_d} in the decreasing order of its values (dec_1 > dec_2 > ... > dec_{n_d}) — the strictly decreasing sequence itself. You can keep this line empty if n_d = 0 (or just print the empty line).
n_i + n_d should be equal to n and the union of printed sequences should be a permutation of the given sequence (in case of "YES" answer).
Examples
Input
7
7 2 7 3 3 1 4
Output
YES
2
3 7
5
7 4 3 2 1
Input
5
4 3 1 5 3
Output
YES
1
3
4
5 4 3 1
Input
5
1 1 2 1 2
Output
NO
Input
5
0 1 2 3 4
Output
YES
0
5
4 3 2 1 0
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
a = [int(s) for s in input().split()]
d = dict()
for i in a:
x = d.get(i,0)
if x == 0:
d[i] = 1
else:
d[i] += 1
up = []
down = []
for i in d.keys():
k = d[i]
if k == 1:
up.append(i)
elif k == 2:
up.append(i)
down.append(i)
else:
print('NO')
exit()
up.sort()
down.sort(reverse = True)
print('YES')
print(len(up))
print(' '.join([str(i) for i in up]))
print(len(down))
print(' '.join([str(i) for i in down])) | python | code_algorithm | [
{
"input": "5\n4 3 1 5 3\n",
"output": "YES\n4\n1 3 4 5\n1\n3\n"
},
{
"input": "5\n0 1 2 3 4\n",
"output": "YES\n5\n0 1 2 3 4\n0\n\n"
},
{
"input": "7\n7 2 7 3 3 1 4\n",
"output": "YES\n5\n1 2 3 4 7\n2\n7 3\n"
},
{
"input": "5\n1 1 2 1 2\n",
"output": "NO\n"
},
{
"input": "12\n12 8 1 3 7 5 9 6 4 10 11 2\n",
"output": "YES\n12\n1 2 3 4 5 6 7 8 9 10 11 12\n0\n\n"
},
{
"input": "6\n5 1 4 2 6 3\n",
"output": "YES\n6\n1 2 3 4 5 6\n0\n\n"
},
{
"input": "8\n0 9 7 5 8 6 9 3\n",
"output": "YES\n7\n0 3 5 6 7 8 9\n1\n9\n"
},
{
"input": "1\n1337\n",
"output": "YES\n1\n1337\n0\n\n"
},
{
"input": "9\n5 1 3 6 8 2 9 0 10\n",
"output": "YES\n9\n0 1 2 3 5 6 8 9 10\n0\n\n"
},
{
"input": "7\n3 2 0 1 0 1 2\n",
"output": "YES\n4\n0 1 2 3\n3\n2 1 0\n"
},
{
"input": "1\n0\n",
"output": "YES\n1\n0\n0\n\n"
},
{
"input": "7\n1 2 3 3 3 3 4\n",
"output": "NO\n"
},
{
"input": "8\n3 2 0 1 0 1 2 3\n",
"output": "YES\n4\n0 1 2 3\n4\n3 2 1 0\n"
},
{
"input": "6\n1 2 100 3 101 4\n",
"output": "YES\n6\n1 2 3 4 100 101\n0\n\n"
},
{
"input": "5\n1 2 4 0 2\n",
"output": "YES\n4\n0 1 2 4\n1\n2\n"
},
{
"input": "5\n1 4 2 5 3\n",
"output": "YES\n5\n1 2 3 4 5\n0\n\n"
},
{
"input": "12\n11 2 1 3 5 8 9 10 7 6 4 12\n",
"output": "YES\n12\n1 2 3 4 5 6 7 8 9 10 11 12\n0\n\n"
}
] | code_contests | python | 0.2 | 7c1bb63fb62623f261870bfb728e3748 |
Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly 1 problem, during the second day — exactly 2 problems, during the third day — exactly 3 problems, and so on. During the k-th day he should solve k problems.
Polycarp has a list of n contests, the i-th contest consists of a_i problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly k problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least k problems that Polycarp didn't solve yet during the k-th day, then Polycarp stops his training.
How many days Polycarp can train if he chooses the contests optimally?
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of contests.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 2 ⋅ 10^5) — the number of problems in the i-th contest.
Output
Print one integer — the maximum number of days Polycarp can train if he chooses the contests optimally.
Examples
Input
4
3 1 4 1
Output
3
Input
3
1 1 1
Output
1
Input
5
1 1 1 2 2
Output
2
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | a = int(input())
b = [int(x) for x in input().split()]
b.sort()
s = 1
q = 0
for i in b:
if i >= s:
s += 1
q += 1
print(q) | python | code_algorithm | [
{
"input": "4\n3 1 4 1\n",
"output": "3\n"
},
{
"input": "5\n1 1 1 2 2\n",
"output": "2\n"
},
{
"input": "3\n1 1 1\n",
"output": "1\n"
},
{
"input": "5\n200000 200000 200000 200000 200000\n",
"output": "5\n"
},
{
"input": "3\n5 6 7\n",
"output": "3\n"
},
{
"input": "3\n100 100 100\n",
"output": "3\n"
},
{
"input": "2\n2 3\n",
"output": "2\n"
},
{
"input": "3\n10 10 10\n",
"output": "3\n"
},
{
"input": "1\n2\n",
"output": "1\n"
}
] | code_contests | python | 0.6 | f89d7738e9396aef7ca85d335fb29209 |
For the multiset of positive integers s=\\{s_1,s_2,...,s_k\}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:
* \gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
* lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, \gcd(\{8,12\})=4,\gcd(\{12,18,6\})=6 and lcm(\{4,6\})=12. Note that for any positive integer x, \gcd(\\{x\})=lcm(\\{x\})=x.
Orac has a sequence a with length n. He come up with the multiset t=\{lcm(\\{a_i,a_j\})\ |\ i<j\}, and asked you to find the value of \gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.
Input
The first line contains one integer n\ (2≤ n≤ 100 000).
The second line contains n integers, a_1, a_2, …, a_n (1 ≤ a_i ≤ 200 000).
Output
Print one integer: \gcd(\{lcm(\\{a_i,a_j\})\ |\ i<j\}).
Examples
Input
2
1 1
Output
1
Input
4
10 24 40 80
Output
40
Input
10
540 648 810 648 720 540 594 864 972 648
Output
54
Note
For the first example, t=\{lcm(\{1,1\})\}=\{1\}, so \gcd(t)=1.
For the second example, t=\{120,40,80,120,240,80\}, and it's not hard to see that \gcd(t)=40.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from collections import Counter
from math import floor, sqrt
try:
long
except NameError:
long = int
def fac(n):
step = lambda x: 1 + (x<<2) - ((x>>1)<<1)
maxq = long(floor(sqrt(n)))
d = 1
q = 2 if n % 2 == 0 else 3
while q <= maxq and n % q != 0:
q = step(d)
d += 1
return [q] + fac(n // q) if q <= maxq else [n]
for _ in range(1):
n=int(input())
li=list(map(int,input().split()))
e=[[] for i in range(200007)]
#f=[30 for i in range(200007)]
#g=[0 for i in range(200007)]
d=[0 for i in range(200007)]
for i in li:
k=Counter(fac(i))
for j in k:
e[j].append(k[j])
d[j]+=1
ans=1
for i in range(200007):
if d[i]==n:
p=min(e[i])
f=30
o=0
for l in e[i]:
if l!=p:
o+=1
f=min(f,l)
if o==n-1:
p=f
ans*=i**(p)
#print(i,p)
if d[i]==n-1:
ans*=i**min(e[i])
print(ans)
| python | code_algorithm | [
{
"input": "2\n1 1\n",
"output": "1\n"
},
{
"input": "10\n540 648 810 648 720 540 594 864 972 648\n",
"output": "54\n"
},
{
"input": "4\n10 24 40 80\n",
"output": "40\n"
},
{
"input": "2\n10007 20014\n",
"output": "20014\n"
},
{
"input": "2\n199999 200000\n",
"output": "39999800000\n"
},
{
"input": "2\n3 3\n",
"output": "3\n"
},
{
"input": "2\n198761 199999\n",
"output": "39752001239\n"
},
{
"input": "3\n166299 110866 86856\n",
"output": "332598\n"
},
{
"input": "10\n972 972 324 972 324 648 1944 243 324 474\n",
"output": "162\n"
},
{
"input": "2\n256 256\n",
"output": "256\n"
},
{
"input": "2\n4 6\n",
"output": "12\n"
},
{
"input": "5\n25 25 5 5 5\n",
"output": "5\n"
}
] | code_contests | python | 0 | d0a118166568a342fb83b7ae497c8688 |
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix a consisting of n rows and m columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings — refer to the Notes section for their formal definitions.
Input
The first line of input contains two integers n and m (1 ≤ n ≤ m ≤ 10^6 and n⋅ m ≤ 10^6) — the number of rows and columns in a, respectively.
The following n lines each contain m characters, each of which is one of 0 and 1. If the j-th character on the i-th line is 1, then a_{i,j} = 1. Similarly, if the j-th character on the i-th line is 0, then a_{i,j} = 0.
Output
Output the minimum number of cells you need to change to make a good, or output -1 if it's not possible at all.
Examples
Input
3 3
101
001
110
Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
Output
-1
Note
In the first case, changing a_{1,1} to 0 and a_{2,2} to 1 is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions —
* A binary matrix is one in which every element is either 1 or 0.
* A sub-matrix is described by 4 parameters — r_1, r_2, c_1, and c_2; here, 1 ≤ r_1 ≤ r_2 ≤ n and 1 ≤ c_1 ≤ c_2 ≤ m.
* This sub-matrix contains all elements a_{i,j} that satisfy both r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2.
* A sub-matrix is, further, called an even length square if r_2-r_1 = c_2-c_1 and r_2-r_1+1 is divisible by 2.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` |
def f( l, a):
i, count = 0,0
for x in l:
count += x not in a[i]
#print( x, a[i], count )
i = (i+1) %2
return count
def g( l, mx ):
return min( [ f(l,mx[i]) for i in range(len(mx)) ] )
def two():
l = [ x[0]+x[1] for x in zip(input(),input())]
mx = (
[['00','11'],['10','01']] ,
[['10','01'],['00','11']] )
return g(l,mx)
def three():
l = [ x[0]+x[1]+x[2] for x in zip(input(),input(),input())]
mx = (
[['100','011'],['110','001']] ,
[['110','001'],['100','011']] ,
[['111','000'],['010','101']] ,
[['010','101'],['000','111']] )
return g(l,mx)
nm = input()
n,m = [ int(x) for x in nm.split() ]
if n == 1:
print(0)
exit()
if n > 3:
print( -1 )
exit()
if n == 2:
print( two() )
exit()
if n == 3:
print( three() )
exit()
| python | code_algorithm | [
{
"input": "3 3\n101\n001\n110\n",
"output": "2\n"
},
{
"input": "7 15\n000100001010010\n100111010110001\n101101111100100\n010000111111010\n111010010100001\n000011001111101\n111111011010011\n",
"output": "-1\n"
},
{
"input": "2 58\n1100001110010010100001000000000110110001101001100010101110\n1110110010101111001110010001100010001010100011111110110100\n",
"output": "27\n"
},
{
"input": "4 4\n1100\n0011\n1100\n0011\n",
"output": "-1\n"
},
{
"input": "1 1\n0\n",
"output": "0\n"
},
{
"input": "1 1\n1\n",
"output": "0\n"
}
] | code_contests | python | 0 | 4499d491d24ab1b9b862ebe56c16375d |
Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm:
* On the first step the string consists of a single character "a".
* On the k-th step Polycarpus concatenates two copies of the string obtained on the (k - 1)-th step, while inserting the k-th character of the alphabet between them. Polycarpus uses the alphabet that consists of lowercase Latin letters and digits (a total of 36 characters). The alphabet characters are numbered like this: the 1-st character is "a", the 2-nd — "b", ..., the 26-th — "z", the 27-th — "0", the 28-th — "1", ..., the 36-th — "9".
Let's have a closer look at the algorithm. On the second step Polycarpus will concatenate two strings "a" and insert the character "b" between them, resulting in "aba" string. The third step will transform it into "abacaba", and the fourth one - into "abacabadabacaba". Thus, the string constructed on the k-th step will consist of 2k - 1 characters.
Polycarpus wrote down the string he got after 30 steps of the given algorithm and chose two non-empty substrings of it. Your task is to find the length of the longest common substring of the two substrings selected by Polycarpus.
A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = "abacaba" equals "bac". The string is its own substring.
The longest common substring of two strings s and t is the longest string that is a substring of both s and t. For example, the longest common substring of "contest" and "systemtesting" is string "test". There can be several common substrings of maximum length.
Input
The input consists of a single line containing four integers l1, r1, l2, r2 (1 ≤ li ≤ ri ≤ 109, i = 1, 2). The numbers are separated by single spaces. li and ri give the indices of the first and the last characters of the i-th chosen substring, correspondingly (i = 1, 2). The characters of string abracadabra are numbered starting from 1.
Output
Print a single number — the length of the longest common substring of the given strings. If there are no common substrings, print 0.
Examples
Input
3 6 1 4
Output
2
Input
1 1 4 4
Output
0
Note
In the first sample the first substring is "acab", the second one is "abac". These two substrings have two longest common substrings "ac" and "ab", but we are only interested in their length — 2.
In the second sample the first substring is "a", the second one is "c". These two substrings don't have any common characters, so the length of their longest common substring is 0.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def solve(x,l1,r1,l2,r2):
if x==0:return 1
if l1>x:
l1-=x+1
r1-=x+1
if l2>x:
l2-=x+1
r2-=x+1
ans=max(0,min(r1,r2)-max(l1,l2)+1)
if l1<=x and x<=r1 and l2<=x and x<=r2:
if l1==0 or r1==x*2:
ans=max(ans,max(x-l2,r2-x))
elif l2==0 or r2==x*2:
ans=max(ans,max(x-l1,r1-x))
else:
if l1<=x-1 and r2>=x+1:
ans=max(ans,solve(x//2,l1,x-1,0,r2-x-1))
if l2<=x-1 and r1>=x+1:
ans=max(ans,solve(x//2,l2,x-1,0,r1-x-1))
elif l1<=x and x<=r1:
if l1==0 or r1==x*2:
ans=max(ans,r2-l2+1)
else:
if l1<=x-1:
ans=max(ans,solve(x//2,l1,x-1,l2,r2))
if r1>=x+1:
ans=max(ans,solve(x//2,0,r1-x-1,l2,r2))
elif l2<=x and x<=r2:
if l2==0 or r2==x*2:
ans=max(ans,r1-l1+1)
else:
if l2<=x-1:
ans=max(ans,solve(x//2,l2,x-1,l1,r1))
if r2>=x+1:
ans=max(ans,solve(x//2,0,r2-x-1,l1,r1))
else:
ans=max(ans,solve(x//2,l1,r1,l2,r2))
return ans
l1,r1,l2,r2=map(int,input().split())
print(solve((1<<36)-1,l1-1,r1-1,l2-1,r2-1))
| python | code_algorithm | [
{
"input": "3 6 1 4\n",
"output": "2\n"
},
{
"input": "1 1 4 4\n",
"output": "0\n"
},
{
"input": "4 7 12 15\n",
"output": "4\n"
},
{
"input": "1 463129088 536870913 1000000000\n",
"output": "463129088\n"
},
{
"input": "1 4 4 7\n",
"output": "3\n"
},
{
"input": "1 2 3 3\n",
"output": "1\n"
},
{
"input": "1 1 2 1000000000\n",
"output": "1\n"
},
{
"input": "8136 12821 10573 15189\n",
"output": "2901\n"
},
{
"input": "169720415 312105195 670978284 671296539\n",
"output": "207899\n"
},
{
"input": "654444727 988815385 77276659 644738371\n",
"output": "334370659\n"
},
{
"input": "26733 47464 19138 46248\n",
"output": "19516\n"
},
{
"input": "42765 7043311 3930802 8641200\n",
"output": "4151539\n"
},
{
"input": "3 3 1 2\n",
"output": "1\n"
},
{
"input": "58660225 863918362 315894896 954309337\n",
"output": "548023467\n"
},
{
"input": "1 2 3 6\n",
"output": "2\n"
},
{
"input": "2 2 6 6\n",
"output": "1\n"
},
{
"input": "601080293 742283208 417827259 630484959\n",
"output": "71194568\n"
},
{
"input": "156642200 503020953 296806626 871864091\n",
"output": "234585497\n"
},
{
"input": "5 7 13 15\n",
"output": "3\n"
},
{
"input": "225343773 292960163 388346281 585652974\n",
"output": "43091683\n"
},
{
"input": "293057586 653835431 583814665 643163992\n",
"output": "59349328\n"
},
{
"input": "5 6 5 10\n",
"output": "2\n"
},
{
"input": "3563 8248 1195 5811\n",
"output": "2901\n"
},
{
"input": "4 7 1 4\n",
"output": "3\n"
},
{
"input": "1 3 4 1000000000\n",
"output": "3\n"
},
{
"input": "1 3 5 7\n",
"output": "3\n"
},
{
"input": "5 7 1 3\n",
"output": "3\n"
},
{
"input": "933937636 947664621 406658382 548532154\n",
"output": "8140525\n"
},
{
"input": "462616550 929253987 199885647 365920450\n",
"output": "166034804\n"
},
{
"input": "73426655 594361930 343984155 989446962\n",
"output": "379149396\n"
},
{
"input": "3 4 1 2\n",
"output": "1\n"
},
{
"input": "489816019 571947327 244679586 543875061\n",
"output": "54059043\n"
},
{
"input": "166724572 472113234 358126054 528083792\n",
"output": "125430608\n"
},
{
"input": "926028190 962292871 588752738 848484542\n",
"output": "36264682\n"
},
{
"input": "656438998 774335411 16384880 470969252\n",
"output": "117896414\n"
},
{
"input": "1 999999999 999999998 1000000000\n",
"output": "3\n"
},
{
"input": "331458616 472661531 443256865 655914565\n",
"output": "71194568\n"
},
{
"input": "59 797 761 863\n",
"output": "103\n"
},
{
"input": "1 1 2 3\n",
"output": "1\n"
},
{
"input": "183307 582175 813247 925985\n",
"output": "112739\n"
},
{
"input": "1 1 1 1\n",
"output": "1\n"
},
{
"input": "156266169 197481622 529043030 565300081\n",
"output": "28429169\n"
},
{
"input": "443495607 813473994 192923319 637537620\n",
"output": "268435455\n"
},
{
"input": "377544108 461895419 242140460 901355034\n",
"output": "84351312\n"
},
{
"input": "2 3 1 1\n",
"output": "1\n"
},
{
"input": "20 59 93 97\n",
"output": "5\n"
},
{
"input": "1 1000000000 1 1000000000\n",
"output": "1000000000\n"
},
{
"input": "876260202 917475655 508441743 544698794\n",
"output": "28429169\n"
},
{
"input": "326428072 910655768 241366302 856438517\n",
"output": "530010446\n"
},
{
"input": "48358214 56090000 19994986 77748608\n",
"output": "7731787\n"
},
{
"input": "260267830 630246217 436204204 880818505\n",
"output": "268435455\n"
},
{
"input": "1 4 9 12\n",
"output": "4\n"
},
{
"input": "677764866 754506263 454018800 668014358\n",
"output": "76741398\n"
},
{
"input": "229012373 968585257 177685154 283692208\n",
"output": "106007055\n"
},
{
"input": "346539730 828420288 373318830 643522086\n",
"output": "270203257\n"
},
{
"input": "760202684 921630809 8799976 434695123\n",
"output": "161428126\n"
},
{
"input": "1 3 9 11\n",
"output": "3\n"
},
{
"input": "1 463129088 536870914 1000000000\n",
"output": "463129087\n"
},
{
"input": "79844257 998861014 59606735 909001530\n",
"output": "829157274\n"
},
{
"input": "287551411 788248606 147317343 692683069\n",
"output": "405131659\n"
},
{
"input": "1 2 2 1000000000\n",
"output": "2\n"
},
{
"input": "4 5 6 7\n",
"output": "1\n"
}
] | code_contests | python | 0 | d6de925fbfa20dccc22ae8224793118d |
The Little Elephant very much loves sums on intervals.
This time he has a pair of integers l and r (l ≤ r). The Little Elephant has to find the number of such integers x (l ≤ x ≤ r), that the first digit of integer x equals the last one (in decimal notation). For example, such numbers as 101, 477474 or 9 will be included in the answer and 47, 253 or 1020 will not.
Help him and count the number of described numbers x for a given pair l and r.
Input
The single line contains a pair of integers l and r (1 ≤ l ≤ r ≤ 1018) — the boundaries of the interval.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
On a single line print a single integer — the answer to the problem.
Examples
Input
2 47
Output
12
Input
47 1024
Output
98
Note
In the first sample the answer includes integers 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def F (x) :
if (x < 10) :
return x
rv = 9
tmp = x
len = 0
while (tmp > 0) :
len += 1
tmp = tmp // 10
d = int (x // pow(10, len-1))
tmp = 1
for i in range(len - 2) :
rv += 9 * tmp
tmp *= 10
for i in range (1, 10) :
if (i < d) :
rv += 1 * tmp
if (x // (pow(10, len-1)) <= x % 10) :
rv += 1
rv += (x % (pow(10, len - 1))) // 10
return rv
l, r = map(int, input().split())
print( abs(F(r) - F(l-1)))
# author: Ashwin Ginoria
# email: [email protected]
# 123456789012345678
# 100000000000000000
# 1000000000000000000
| python | code_algorithm | [
{
"input": "47 1024\n",
"output": "98\n"
},
{
"input": "2 47\n",
"output": "12\n"
},
{
"input": "645762257531682046 885295120956158518\n",
"output": "23953286342447648\n"
},
{
"input": "1 11\n",
"output": "10\n"
},
{
"input": "47 8545\n",
"output": "849\n"
},
{
"input": "10001 10000002\n",
"output": "999001\n"
},
{
"input": "1827171 232817181719384635\n",
"output": "23281718171755747\n"
},
{
"input": "9999999999999987 99999999999999711\n",
"output": "8999999999999973\n"
},
{
"input": "819875140559301752 946247219812473271\n",
"output": "12637207925317152\n"
},
{
"input": "2 3\n",
"output": "2\n"
},
{
"input": "522842183413115088 853628713003942530\n",
"output": "33078652959082744\n"
},
{
"input": "235 236\n",
"output": "0\n"
},
{
"input": "919845424847912645 970651082117950285\n",
"output": "5080565727003764\n"
},
{
"input": "975400104587000 48754000000000001\n",
"output": "4777859989541300\n"
},
{
"input": "1 1\n",
"output": "1\n"
},
{
"input": "47547 4587554587754542\n",
"output": "458755458770699\n"
},
{
"input": "100000000000000000 1000000000000000000\n",
"output": "90000000000000000\n"
},
{
"input": "1 2\n",
"output": "2\n"
},
{
"input": "88 990\n",
"output": "91\n"
},
{
"input": "1 10\n",
"output": "9\n"
},
{
"input": "73 678\n",
"output": "61\n"
},
{
"input": "111 111\n",
"output": "1\n"
},
{
"input": "111111111111111111 333333333444444445\n",
"output": "22222222233333334\n"
},
{
"input": "45481484484 848469844684844\n",
"output": "84842436320036\n"
},
{
"input": "4 19\n",
"output": "7\n"
},
{
"input": "115998725487587451 245744899758754501\n",
"output": "12974617427116705\n"
},
{
"input": "7777 88888\n",
"output": "8112\n"
},
{
"input": "458754 4588754\n",
"output": "413001\n"
},
{
"input": "2 2\n",
"output": "1\n"
},
{
"input": "1 1000000000000000000\n",
"output": "100000000000000008\n"
},
{
"input": "10000 100000\n",
"output": "9000\n"
},
{
"input": "11 111111111111111100\n",
"output": "11111111111111109\n"
},
{
"input": "779547115376367013 980561039207670775\n",
"output": "20101392383130376\n"
},
{
"input": "84324827171274023 607953653548585226\n",
"output": "52362882637731121\n"
},
{
"input": "7 10\n",
"output": "3\n"
},
{
"input": "9754875457700 1000000000000000000\n",
"output": "99999024512454230\n"
},
{
"input": "11220451511 51511665251233335\n",
"output": "5151165403078183\n"
},
{
"input": "458985985498001244 985458425544874008\n",
"output": "52647244004687276\n"
},
{
"input": "47 74\n",
"output": "2\n"
},
{
"input": "9997 87878000008\n",
"output": "8787799002\n"
},
{
"input": "12 10000000000\n",
"output": "999999998\n"
},
{
"input": "1312148742261681 277460340506883334\n",
"output": "27614819176462166\n"
},
{
"input": "77 77\n",
"output": "1\n"
},
{
"input": "252509053898415172 285803555062529649\n",
"output": "3329450116411448\n"
},
{
"input": "110 147\n",
"output": "4\n"
},
{
"input": "999999999999999999 1000000000000000000\n",
"output": "1\n"
},
{
"input": "7 8\n",
"output": "2\n"
},
{
"input": "335408916782916802 416495628489807285\n",
"output": "8108671170689049\n"
},
{
"input": "10000000001 111111111111100001\n",
"output": "11111110111110001\n"
},
{
"input": "1000 1000\n",
"output": "0\n"
},
{
"input": "822981258385599125 841978899930248528\n",
"output": "1899764154464941\n"
},
{
"input": "1 1000000\n",
"output": "100008\n"
},
{
"input": "4 7\n",
"output": "4\n"
},
{
"input": "1 1000\n",
"output": "108\n"
},
{
"input": "8758754570000 999999999999999999\n",
"output": "99999124124543000\n"
},
{
"input": "1000000000 1000000000\n",
"output": "0\n"
},
{
"input": "5 45\n",
"output": "9\n"
},
{
"input": "999999999 10000000000\n",
"output": "900000001\n"
},
{
"input": "99 102\n",
"output": "2\n"
},
{
"input": "1 1000000000\n",
"output": "100000008\n"
},
{
"input": "595754249475458004 615044544745124547\n",
"output": "1929029526966655\n"
},
{
"input": "1 10000\n",
"output": "1008\n"
}
] | code_contests | python | 0 | d128888ab068e32830d19f7b306eca42 |
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.
Note that the order of the points inside the group of three chosen points doesn't matter.
Input
The first line contains two integers: n and d (1 ≤ n ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got.
It is guaranteed that the coordinates of the points in the input strictly increase.
Output
Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 10 20 30 50
Output
1
Note
In the first sample any group of three points meets our conditions.
In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.
In the third sample only one group does: {1, 10, 20}.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n,d=map(int,input().split())
nums=list(map(int,input().split()))
count=0
j=0
for i in range(n):
while j<n and nums[j]-nums[i]<=d:
j+=1
temp_length=(j-i-1)
count+=int((temp_length*(temp_length-1))/2)
print(count)
| python | code_algorithm | [
{
"input": "4 3\n1 2 3 4\n",
"output": "4\n"
},
{
"input": "4 2\n-3 -2 -1 0\n",
"output": "2\n"
},
{
"input": "5 19\n1 10 20 30 50\n",
"output": "1\n"
},
{
"input": "2 1000000000\n-14348867 1760823\n",
"output": "0\n"
},
{
"input": "10 5\n31 36 43 47 48 50 56 69 71 86\n",
"output": "2\n"
},
{
"input": "10 90\n24 27 40 41 61 69 73 87 95 97\n",
"output": "120\n"
},
{
"input": "10 50\n1 4 20 27 65 79 82 83 99 100\n",
"output": "25\n"
},
{
"input": "3 1000000000\n-5 -1 1\n",
"output": "1\n"
},
{
"input": "2 1000000000\n-907894512 -289906312\n",
"output": "0\n"
},
{
"input": "100 100\n-98 -97 -96 -93 -92 -91 -90 -87 -86 -84 -81 -80 -79 -78 -76 -75 -73 -71 -69 -67 -65 -64 -63 -62 -61 -54 -51 -50 -49 -48 -46 -45 -44 -37 -36 -33 -30 -28 -27 -16 -15 -13 -12 -10 -9 -7 -6 -5 -4 2 3 5 8 9 10 11 13 14 15 16 17 19 22 24 25 26 27 28 30 31 32 36 40 43 45 46 47 50 51 52 53 58 60 63 69 70 73 78 80 81 82 85 88 89 90 91 95 96 97 99\n",
"output": "79351\n"
},
{
"input": "1 14751211\n847188590\n",
"output": "0\n"
}
] | code_contests | python | 0.9 | fbf9eadad933d5a6c135c8c62620838d |
The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.
Input
The first line contains an integer number n (1 ≤ n ≤ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
Output
Print the name of the winner.
Examples
Input
3
mike 3
andrew 5
mike 2
Output
andrew
Input
3
andrew 3
andrew 2
mike 5
Output
andrew
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
d = {}
l = []
for _ in range(n):
name, score = input().split()
score = int(score)
l.append((name,score))
d[name] = d.get(name, 0) + score
m = max([x for x in d.values()])
ties = [x for x,y in d.items() if y == m]
# print(ties)
if len(ties) == 1:
print(ties[0])
exit()
else:
d.clear()
for row in l:
name,score = row
d[name] = d.get(name,0) + score
# print(name, d[name])
if name in ties and d[name] >= m:
print(name)
break
| python | code_algorithm | [
{
"input": "3\nandrew 3\nandrew 2\nmike 5\n",
"output": "andrew\n"
},
{
"input": "3\nmike 3\nandrew 5\nmike 2\n",
"output": "andrew\n"
},
{
"input": "15\naawtvezfntstrcpgbzjbf 681\nzhahpvqiptvksnbjkdvmknb -74\naawtvezfntstrcpgbzjbf 661\njpdwmyke 474\naawtvezfntstrcpgbzjbf -547\naawtvezfntstrcpgbzjbf 600\nzhahpvqiptvksnbjkdvmknb -11\njpdwmyke 711\nbjmj 652\naawtvezfntstrcpgbzjbf -1000\naawtvezfntstrcpgbzjbf -171\nbjmj -302\naawtvezfntstrcpgbzjbf 961\nzhahpvqiptvksnbjkdvmknb 848\nbjmj -735\n",
"output": "aawtvezfntstrcpgbzjbf\n"
},
{
"input": "12\natrtthfpcvishmqbakprquvnejr 185\natrtthfpcvishmqbakprquvnejr -699\natrtthfpcvishmqbakprquvnejr -911\natrtthfpcvishmqbakprquvnejr -220\nfcgslzkicjrpbqaifgweyzreajjfdo 132\nfcgslzkicjrpbqaifgweyzreajjfdo -242\nm 177\nm -549\natrtthfpcvishmqbakprquvnejr -242\nm 38\natrtthfpcvishmqbakprquvnejr -761\nfcgslzkicjrpbqaifgweyzreajjfdo 879\n",
"output": "fcgslzkicjrpbqaifgweyzreajjfdo\n"
},
{
"input": "7\nksjuuerbnlklcfdjeyq 312\ndthjlkrvvbyahttifpdewvyslsh -983\nksjuuerbnlklcfdjeyq 268\ndthjlkrvvbyahttifpdewvyslsh 788\nksjuuerbnlklcfdjeyq -79\nksjuuerbnlklcfdjeyq -593\nksjuuerbnlklcfdjeyq 734\n",
"output": "ksjuuerbnlklcfdjeyq\n"
},
{
"input": "17\nqdplghhx -649\nivhgbxiv 424\nivhgbxiv -72\nivhgbxiv 479\nugowfouebkelargxylsxn 83\nugowfouebkelargxylsxn -840\nivhgbxiv 382\nqdplghhx -904\nivhgbxiv -710\nugowfouebkelargxylsxn -838\nqdplghhx -974\nqdplghhx 571\nivhgbxiv -145\nugowfouebkelargxylsxn -788\nivhgbxiv 209\nivhgbxiv 420\nqdplghhx -158\n",
"output": "ivhgbxiv\n"
},
{
"input": "5\nkaxqybeultn -352\nmgochgrmeyieyskhuourfg -910\nkaxqybeultn 691\nmgochgrmeyieyskhuourfg -76\nkaxqybeultn -303\n",
"output": "kaxqybeultn\n"
}
] | code_contests | python | 0.1 | 7607b599a8fc5a72a57e1b333e32a21c |
You are given n rectangles. The corners of rectangles have integer coordinates and their edges are parallel to the Ox and Oy axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle).
Your task is to determine if the rectangles form a square. In other words, determine if the set of points inside or on the border of at least one rectangle is precisely equal to the set of points inside or on the border of some square.
Input
The first line contains a single integer n (1 ≤ n ≤ 5). Next n lines contain four integers each, describing a single rectangle: x1, y1, x2, y2 (0 ≤ x1 < x2 ≤ 31400, 0 ≤ y1 < y2 ≤ 31400) — x1 and x2 are x-coordinates of the left and right edges of the rectangle, and y1 and y2 are y-coordinates of the bottom and top edges of the rectangle.
No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle).
Output
In a single line print "YES", if the given rectangles form a square, or "NO" otherwise.
Examples
Input
5
0 0 2 3
0 3 3 5
2 0 5 2
3 2 5 5
2 2 3 3
Output
YES
Input
4
0 0 2 3
0 3 3 5
2 0 5 2
3 2 5 5
Output
NO
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | xmin, ymin, xmax, ymax, a = 31400, 31400, 0, 0, 0
for i in range(int(input())):
x1, y1, x2, y2 = map(int, input().split())
xmin = min(xmin, x1)
ymin = min(ymin, y1)
xmax = max(xmax, x2)
ymax = max(ymax, y2)
a += (x2 - x1) * (y2 - y1)
print('YES' if xmax - xmin == ymax - ymin and a == (xmax - xmin) ** 2 else 'NO') | python | code_algorithm | [
{
"input": "5\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n2 2 3 3\n",
"output": "YES\n"
},
{
"input": "4\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n",
"output": "NO\n"
},
{
"input": "3\n25784 0 31400 20408\n0 20408 31400 20582\n15802 0 18106 20408\n",
"output": "NO\n"
},
{
"input": "2\n1 1 5 5\n1 5 5 7\n",
"output": "NO\n"
},
{
"input": "5\n1 1 3 5\n3 3 5 5\n4 1 5 3\n3 1 4 2\n2 5 3 6\n",
"output": "NO\n"
},
{
"input": "5\n0 0 10000 20000\n10000 0 15000 19999\n10000 19999 14999 20000\n0 20000 15000 31400\n15000 0 31400 31400\n",
"output": "NO\n"
},
{
"input": "1\n0 3006 1 17592\n",
"output": "NO\n"
},
{
"input": "5\n3091 4819 5743 13222\n123 13222 5819 29511\n5743 4819 5819 13222\n123 4819 2215 13222\n2215 4819 3091 13222\n",
"output": "NO\n"
},
{
"input": "4\n0 0 10 10\n10 10 20 20\n10 0 20 10\n10 20 11 120\n",
"output": "NO\n"
},
{
"input": "5\n0 0 2 1\n2 0 3 2\n0 1 1 3\n1 2 3 3\n1 1 2 2\n",
"output": "YES\n"
},
{
"input": "2\n0 0 3 1\n0 2 3 3\n",
"output": "NO\n"
},
{
"input": "5\n0 0 100 30000\n100 0 31400 5000\n100 5000 20000 30000\n0 30000 20000 31400\n20000 5000 31400 31400\n",
"output": "YES\n"
},
{
"input": "1\n20499 0 31400 22815\n",
"output": "NO\n"
},
{
"input": "2\n0 0 10000 31400\n10000 0 31400 31400\n",
"output": "YES\n"
},
{
"input": "5\n0 25169 1 27914\n0 0 1 1366\n0 10763 1 25169\n0 1366 1 10138\n0 27914 1 31400\n",
"output": "NO\n"
},
{
"input": "2\n0 0 1 1\n1 0 2 1\n",
"output": "NO\n"
},
{
"input": "2\n0 0 2 6\n2 2 4 4\n",
"output": "NO\n"
},
{
"input": "2\n0 0 1 1\n1 1 2 2\n",
"output": "NO\n"
},
{
"input": "2\n0 1273 26470 9100\n0 16615 31400 31400\n",
"output": "NO\n"
},
{
"input": "5\n12750 0 25688 1\n1094 0 12750 1\n0 0 956 1\n956 0 1094 1\n25688 0 31400 1\n",
"output": "NO\n"
},
{
"input": "3\n0 1 1 3\n0 3 1 5\n0 0 1 1\n",
"output": "NO\n"
},
{
"input": "2\n0 0 1 1\n2 2 3 3\n",
"output": "NO\n"
},
{
"input": "5\n20812 5661 27208 5898\n20812 581 29415 5661\n27539 5661 29415 5898\n18961 581 20812 5898\n27208 5661 27539 5898\n",
"output": "NO\n"
},
{
"input": "1\n31399 31399 31400 31400\n",
"output": "YES\n"
},
{
"input": "1\n0 0 1 1\n",
"output": "YES\n"
},
{
"input": "3\n0 9388 31400 31400\n26020 0 31400 9388\n0 0 26020 9388\n",
"output": "YES\n"
},
{
"input": "4\n18006 16484 25725 31400\n0 0 31400 16484\n29563 16484 31400 31400\n25725 16484 29563 31400\n",
"output": "NO\n"
},
{
"input": "2\n0 0 1 18\n5 0 6 18\n",
"output": "NO\n"
},
{
"input": "1\n0 0 1 4\n",
"output": "NO\n"
},
{
"input": "1\n0 0 1 5\n",
"output": "NO\n"
},
{
"input": "5\n15164 0 19356 3925\n0 0 15164 31400\n15164 3925 31400 31400\n19356 3278 31400 3925\n19356 0 31400 3278\n",
"output": "YES\n"
},
{
"input": "2\n1 1 2 2\n3 3 4 4\n",
"output": "NO\n"
},
{
"input": "5\n0 0 10000 20000\n10000 0 15000 19999\n10000 19999 15000 20000\n0 20000 15000 31400\n15000 0 31400 31400\n",
"output": "YES\n"
},
{
"input": "2\n0 0 10000 31400\n10000 0 31400 31399\n",
"output": "NO\n"
},
{
"input": "5\n0 0 100 30000\n100 0 31400 5000\n100 5000 20000 30000\n0 30000 20000 31000\n20000 5000 31400 31000\n",
"output": "NO\n"
},
{
"input": "1\n0 0 31400 31400\n",
"output": "YES\n"
},
{
"input": "1\n123 4819 5819 29511\n",
"output": "NO\n"
},
{
"input": "2\n0 0 2 4\n10 0 12 4\n",
"output": "NO\n"
},
{
"input": "5\n1338 31399 1525 31400\n1525 31399 2595 31400\n961 31399 1338 31400\n2956 31399 31400 31400\n2595 31399 2956 31400\n",
"output": "NO\n"
},
{
"input": "2\n2 2 3 3\n4 4 6 7\n",
"output": "NO\n"
},
{
"input": "5\n0 7098 1 7460\n0 7460 1 15218\n0 15218 1 31400\n0 4974 1 7098\n0 0 1 4974\n",
"output": "NO\n"
},
{
"input": "5\n1234 1234 1235 1235\n1238 1234 1239 1235\n1235 1234 1236 1235\n1237 1234 1238 1235\n1236 1234 1237 1235\n",
"output": "NO\n"
},
{
"input": "4\n0 0 1 2\n0 3 1 4\n0 4 1 5\n0 2 1 3\n",
"output": "NO\n"
},
{
"input": "5\n8591 1234 9517 19512\n696 19512 9517 31400\n696 696 8591 19512\n8591 696 31400 1234\n9517 1234 31400 31400\n",
"output": "YES\n"
},
{
"input": "1\n15819 5189 23141 12511\n",
"output": "YES\n"
},
{
"input": "5\n26466 0 26474 6206\n10906 0 17073 6321\n19720 0 26356 31400\n0 0 10906 7852\n0 21437 18466 31400\n",
"output": "NO\n"
},
{
"input": "5\n8030 7681 8491 7682\n8491 7681 8961 7682\n7666 7681 7963 7682\n7963 7681 8030 7682\n678 7681 7666 7682\n",
"output": "NO\n"
},
{
"input": "5\n10359 859 28918 4384\n2895 26520 28918 26882\n2895 26424 28918 26520\n2895 859 10359 4384\n2895 4384 28918 26424\n",
"output": "YES\n"
},
{
"input": "1\n0 0 10575 1\n",
"output": "NO\n"
},
{
"input": "2\n0 0 31400 13313\n0 13313 31400 31400\n",
"output": "YES\n"
},
{
"input": "4\n0 0 1 1\n5 5 6 6\n10 10 11 11\n13 13 14 14\n",
"output": "NO\n"
},
{
"input": "2\n0 0 1 1\n3 3 4 4\n",
"output": "NO\n"
},
{
"input": "4\n0 0 2 1\n2 0 3 2\n0 1 1 3\n1 2 3 3\n",
"output": "NO\n"
},
{
"input": "1\n0 0 1 7\n",
"output": "NO\n"
},
{
"input": "5\n20421 5189 23141 12511\n16414 10436 17880 12511\n17880 10436 20421 12511\n15819 10436 16414 12511\n15819 5189 20421 10436\n",
"output": "YES\n"
},
{
"input": "4\n0 0 1 1\n0 1 1 2\n0 2 1 3\n0 3 1 4\n",
"output": "NO\n"
},
{
"input": "3\n0 0 1 1\n1 0 3 3\n0 2 1 4\n",
"output": "NO\n"
},
{
"input": "3\n12052 12345 12343 18147\n12343 12345 12345 18147\n6543 12345 12052 18147\n",
"output": "YES\n"
},
{
"input": "3\n123 4819 5819 6612\n123 6612 5819 12692\n123 12692 5819 29511\n",
"output": "NO\n"
},
{
"input": "5\n0 0 1 1\n0 3 1 4\n0 1 1 2\n0 2 1 3\n0 4 1 5\n",
"output": "NO\n"
},
{
"input": "4\n0 0 4 2\n0 2 3 6\n3 4 6 6\n4 0 6 4\n",
"output": "NO\n"
},
{
"input": "4\n10 10 11 11\n11 11 12 12\n11 10 12 11\n9 12 10 13\n",
"output": "NO\n"
},
{
"input": "1\n0 0 1 31400\n",
"output": "NO\n"
},
{
"input": "1\n0 0 31400 1\n",
"output": "NO\n"
},
{
"input": "5\n1349 0 1391 3766\n1234 0 1238 417\n1391 0 5000 3766\n1234 417 1238 3766\n1238 0 1349 3766\n",
"output": "YES\n"
}
] | code_contests | python | 0.4 | 58882983af67ad0b52d05aeeb11f6a3e |
Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own hands. Polycarpus thinks that there are only three decent ingredients to make hamburgers from: a bread, sausage and cheese. He writes down the recipe of his favorite "Le Hamburger de Polycarpus" as a string of letters 'B' (bread), 'S' (sausage) и 'C' (cheese). The ingredients in the recipe go from bottom to top, for example, recipe "ВSCBS" represents the hamburger where the ingredients go from bottom to top as bread, sausage, cheese, bread and sausage again.
Polycarpus has nb pieces of bread, ns pieces of sausage and nc pieces of cheese in the kitchen. Besides, the shop nearby has all three ingredients, the prices are pb rubles for a piece of bread, ps for a piece of sausage and pc for a piece of cheese.
Polycarpus has r rubles and he is ready to shop on them. What maximum number of hamburgers can he cook? You can assume that Polycarpus cannot break or slice any of the pieces of bread, sausage or cheese. Besides, the shop has an unlimited number of pieces of each ingredient.
Input
The first line of the input contains a non-empty string that describes the recipe of "Le Hamburger de Polycarpus". The length of the string doesn't exceed 100, the string contains only letters 'B' (uppercase English B), 'S' (uppercase English S) and 'C' (uppercase English C).
The second line contains three integers nb, ns, nc (1 ≤ nb, ns, nc ≤ 100) — the number of the pieces of bread, sausage and cheese on Polycarpus' kitchen. The third line contains three integers pb, ps, pc (1 ≤ pb, ps, pc ≤ 100) — the price of one piece of bread, sausage and cheese in the shop. Finally, the fourth line contains integer r (1 ≤ r ≤ 1012) — the number of rubles Polycarpus has.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print the maximum number of hamburgers Polycarpus can make. If he can't make any hamburger, print 0.
Examples
Input
BBBSSC
6 4 1
1 2 3
4
Output
2
Input
BBC
1 10 1
1 10 1
21
Output
7
Input
BSC
1 1 1
1 1 3
1000000000000
Output
200000000001
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | st = input()
B = st.count('B')
S = st.count('S')
C = st.count('C')
b,s,c=map(int,input().split())
bp, sp, cp = map(int,input().split())
r=int(input())
lm=0; rm=int(1e15)+1
while rm-lm>1:
m=(rm+lm)//2
# print(m)
bb=max(m*B-b,0)
ss=max(m*S-s,0)
cc=max(m*C-c, 0)
#print(bp*bb+ss*sp+cc*cp)
if bp*bb+ss*sp+cc*cp <= r:
lm=m
else:
rm=m
print(lm) | python | code_algorithm | [
{
"input": "BBC\n1 10 1\n1 10 1\n21\n",
"output": "7\n"
},
{
"input": "BSC\n1 1 1\n1 1 3\n1000000000000\n",
"output": "200000000001\n"
},
{
"input": "BBBSSC\n6 4 1\n1 2 3\n4\n",
"output": "2\n"
},
{
"input": "B\n100 100 100\n1 1 1\n1\n",
"output": "101\n"
},
{
"input": "SBCSSCBBSSBCSSBBBSSBSCBSSSCBBSBBBBCSBCSBSCBSCBSCBSBSSCCCCBSBCCBCBSCCCBSCCBSBBCBSSCCCCSBSBBBSSSBCSCBC\n94 16 85\n14 18 91\n836590091442\n",
"output": "217522127\n"
},
{
"input": "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n1 1 1\n100 100 100\n1000000000000\n",
"output": "100000000\n"
},
{
"input": "CCSCCCSBBBSCBSCSCCSSBBBSSBBBSBBBCBCSSBCSCBBCCCBCBCBCCCSSBSBBCCCCCBBSCBSCBCBBCBBCSSBCSBSSCCSCCSCCBBBS\n33 73 67\n4 56 42\n886653164314\n",
"output": "277425898\n"
},
{
"input": "CBBCBSBCCSCBSSCCBCSBCSBBSCBBCSCCBSCCSCSBBSSBSBSCBBSBBCSSSSBBBBSBBCBCSBBCBCSSBBCSBSCCSCSBCSCBSCCBBCSC\n71 71 52\n52 88 3\n654400055575\n",
"output": "137826467\n"
},
{
"input": "BSC\n100 1 1\n100 1 1\n100\n",
"output": "51\n"
},
{
"input": "BSC\n3 5 6\n7 3 9\n100\n",
"output": "10\n"
},
{
"input": "SC\n2 1 1\n1 1 1\n100000000000\n",
"output": "50000000001\n"
},
{
"input": "C\n100 100 100\n1 1 1\n1000000000000\n",
"output": "1000000000100\n"
},
{
"input": "B\n100 100 100\n1 1 1\n1000000000000\n",
"output": "1000000000100\n"
},
{
"input": "BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n10 20 40\n100 100 100\n300000000\n",
"output": "42858\n"
},
{
"input": "BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n10 20 40\n100 100 100\n2000\n",
"output": "1\n"
},
{
"input": "SSSSSSSSSSBBBBBBBBBCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSBB\n31 53 97\n13 17 31\n914159265358\n",
"output": "647421579\n"
},
{
"input": "CC\n1 1 1\n100 100 100\n1\n",
"output": "0\n"
},
{
"input": "BSC\n100 100 100\n1 1 1\n1000000000000\n",
"output": "333333333433\n"
},
{
"input": "BBBBCCCCCCCCCCCCCCCCCCCCSSSSBBBBBBBBSS\n100 100 100\n1 1 1\n3628800\n",
"output": "95502\n"
},
{
"input": "BBBCSBSBBSSSSCCCCBBCSBBBBSSBBBCBSCCSSCSSCSBSSSCCCCBSCSSBSSSCCCBBCCCSCBCBBCCSCCCCSBBCCBBBBCCCCCCBSSCB\n91 87 17\n64 44 43\n958532915587\n",
"output": "191668251\n"
},
{
"input": "CBBCBSBCCSCBSSCCBCSBCSBBSCBBCSCCBSCCSCSBBSBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBCBBCSC\n100 1 1\n1 17 23\n954400055575\n",
"output": "1355681897\n"
},
{
"input": "BSCSBSCCSCSSCCCSBCSSBCBBSCCBSCCSSSSSSSSSCCSBSCCBBCBBSBSCCCCBCSBSBSSBBBBBSSBSSCBCCSSBSSSCBBCSBBSBCCCB\n67 54 8\n36 73 37\n782232051273\n",
"output": "154164772\n"
},
{
"input": "B\n1 1 1\n1 1 1\n381\n",
"output": "382\n"
},
{
"input": "BSC\n100 100 100\n1 1 1\n1\n",
"output": "100\n"
},
{
"input": "BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n10 20 40\n100 100 100\n914159265358\n",
"output": "130594181\n"
},
{
"input": "SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n100 100 100\n100 100 100\n1000000000000\n",
"output": "100000001\n"
},
{
"input": "B\n100 1 1\n1 1 1\n1000000000000\n",
"output": "1000000000100\n"
},
{
"input": "BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n10 20 40\n100 100 100\n300\n",
"output": "0\n"
},
{
"input": "BBBBBBBBBBCCCCCCCCCCCCCCCCCCCCSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n10 20 40\n100 100 100\n200\n",
"output": "0\n"
},
{
"input": "SBBCCSBB\n1 50 100\n31 59 21\n100000\n",
"output": "370\n"
},
{
"input": "CSSCBBCCCSBSCBBBCSBBBCBSBCSCBCSCBCBSBCBCSSBBSBBCBBBBSCSBBCCBCCBCBBSBSBCSCSBBSSBBCSSBCSCSCCSSBCBBCBSB\n56 34 48\n78 6 96\n904174875419\n",
"output": "140968956\n"
}
] | code_contests | python | 0 | 124278b5440b034a25570877ff3ad8de |
Recently, Duff has been practicing weight lifting. As a hard practice, Malek gave her a task. He gave her a sequence of weights. Weight of i-th of them is 2wi pounds. In each step, Duff can lift some of the remaining weights and throw them away. She does this until there's no more weight left. Malek asked her to minimize the number of steps.
<image>
Duff is a competitive programming fan. That's why in each step, she can only lift and throw away a sequence of weights 2a1, ..., 2ak if and only if there exists a non-negative integer x such that 2a1 + 2a2 + ... + 2ak = 2x, i. e. the sum of those numbers is a power of two.
Duff is a competitive programming fan, but not a programmer. That's why she asked for your help. Help her minimize the number of steps.
Input
The first line of input contains integer n (1 ≤ n ≤ 106), the number of weights.
The second line contains n integers w1, ..., wn separated by spaces (0 ≤ wi ≤ 106 for each 1 ≤ i ≤ n), the powers of two forming the weights values.
Output
Print the minimum number of steps in a single line.
Examples
Input
5
1 1 2 3 3
Output
2
Input
4
0 1 2 3
Output
4
Note
In the first sample case: One optimal way would be to throw away the first three in the first step and the rest in the second step. Also, it's not possible to do it in one step because their sum is not a power of two.
In the second sample case: The only optimal way is to throw away one weight in each step. It's not possible to do it in less than 4 steps because there's no subset of weights with more than one weight and sum equal to a power of two.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | N = int(1e6+100)
n = int(input())
arr = list(map(int, input().split()))
cnt = [0] * N
for i in arr:
cnt[i] += 1
res, s = 0, 0
for i in range(N):
s += cnt[i]
res += s % 2
s //= 2
print(res)
| python | code_algorithm | [
{
"input": "5\n1 1 2 3 3\n",
"output": "2\n"
},
{
"input": "4\n0 1 2 3\n",
"output": "4\n"
},
{
"input": "2\n95745 95745\n",
"output": "1\n"
},
{
"input": "100\n196 1681 196 0 61 93 196 196 196 196 196 0 0 96 18 1576 0 93 666463 18 93 1 1278 8939 93 196 196 1278 3 0 67416 869956 10 56489 196 745 39 783 196 8939 196 81 69634 4552 39 3 14 20 25 8 10 4 7302 0 19579 20 1140 15990 7302 0 19579 4142 11 1354 75252 93 311 1278 0 79475 10 75252 93 7302 0 81 408441 19579 10 39 19 37748 4364 31135 47700 105818 47700 10 4142 543356 3 30647 45917 60714 8939 18 22925 7302 93 75252\n",
"output": "59\n"
},
{
"input": "2\n28288 0\n",
"output": "2\n"
},
{
"input": "13\n688743 688743 1975 688743 688743 688743 688743 688743 688743 0 0 688743 688743\n",
"output": "4\n"
},
{
"input": "35\n0 0 298 0 0 0 0 0 689063 65442 0 984598 2054 43668 0 369 0 2054 0 996220 0 16327 369 0 996220 0 0 0 4693 2054 348 0 118 0 0\n",
"output": "16\n"
},
{
"input": "13\n92 194 580495 0 10855 41704 13 96429 33 213 0 92 140599\n",
"output": "11\n"
},
{
"input": "35\n130212 3176 77075 8071 18 1369 7539 1683 80757 1847 0 1374 122 8524 4 2 21333 270264 4 9254 151921 0 1 33596 73002 54382 0 1 29233 75952 15 38892 1877 6167 4\n",
"output": "31\n"
},
{
"input": "1\n120287\n",
"output": "1\n"
},
{
"input": "35\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
"output": "3\n"
}
] | code_contests | python | 0.1 | 91916225290388c310f56a118427d7b1 |
In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server.
In order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression ma - mb, where a is the most loaded server and b is the least loaded one.
In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.
Write a program to find the minimum number of seconds needed to balance the load of servers.
Input
The first line contains positive number n (1 ≤ n ≤ 105) — the number of the servers.
The second line contains the sequence of non-negative integers m1, m2, ..., mn (0 ≤ mi ≤ 2·104), where mi is the number of tasks assigned to the i-th server.
Output
Print the minimum number of seconds required to balance the load.
Examples
Input
2
1 6
Output
2
Input
7
10 11 10 11 10 11 11
Output
0
Input
5
1 2 3 4 5
Output
3
Note
In the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.
In the second example the load is already balanced.
A possible sequence of task movements for the third example is:
1. move a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5);
2. then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4);
3. then move task from server #5 to server #2 (the sequence m becomes: 3 3 3 3 3).
The above sequence is one of several possible ways to balance the load of servers in three seconds.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | input()
servers = [int(x) for x in input().split(" ", )]
servers.sort()
avg = int(sum(servers) / len(servers))
extra = sum(servers) % len(servers)
count = 0
for i in range(len(servers)):
count += abs(avg + (i >= (len(servers) - extra)) - servers[i]) / 2
print(int(count))
| python | code_algorithm | [
{
"input": "2\n1 6\n",
"output": "2\n"
},
{
"input": "7\n10 11 10 11 10 11 11\n",
"output": "0\n"
},
{
"input": "5\n1 2 3 4 5\n",
"output": "3\n"
},
{
"input": "2\n10 3\n",
"output": "3\n"
},
{
"input": "5\n2 10 20 30 50\n",
"output": "34\n"
},
{
"input": "1\n0\n",
"output": "0\n"
},
{
"input": "10\n19999 19999 20000 20000 19999 20000 20000 20000 19999 19999\n",
"output": "0\n"
},
{
"input": "1\n20000\n",
"output": "0\n"
},
{
"input": "5\n6 5 9 7 6\n",
"output": "2\n"
},
{
"input": "7\n2 2 2 2 2 3 4\n",
"output": "1\n"
},
{
"input": "10\n0 0 0 0 0 0 0 0 0 0\n",
"output": "0\n"
},
{
"input": "3\n1 10000 20000\n",
"output": "9999\n"
},
{
"input": "10\n8 5 5 5 6 6 6 6 5 5\n",
"output": "2\n"
}
] | code_contests | python | 0 | 5b62ca3e0143a507fdb7a45a598f0b91 |
Input
The only line of the input is a string of 7 characters. The first character is letter A, followed by 6 digits. The input is guaranteed to be valid (for certain definition of "valid").
Output
Output a single integer.
Examples
Input
A221033
Output
21
Input
A223635
Output
22
Input
A232726
Output
23
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | s = input()
ans = 1
for i in s[1:]:
x = int(i)
if x == 0:
x = 9
ans += x
print(ans)
| python | code_algorithm | [
{
"input": "A232726\n",
"output": "23\n"
},
{
"input": "A223635\n",
"output": "22\n"
},
{
"input": "A221033\n",
"output": "21\n"
},
{
"input": "A222222\n",
"output": "13\n"
},
{
"input": "A101010\n",
"output": "31\n"
},
{
"input": "A999999\n",
"output": "55\n"
},
{
"input": "A910224\n",
"output": "28\n"
},
{
"input": "A232232\n",
"output": "15\n"
},
{
"input": "A767653\n",
"output": "35\n"
},
{
"input": "A458922\n",
"output": "31\n"
},
{
"input": "A764598\n",
"output": "40\n"
},
{
"input": "A987654\n",
"output": "40\n"
},
{
"input": "A332567\n",
"output": "27\n"
},
{
"input": "A638495\n",
"output": "36\n"
},
{
"input": "A234567\n",
"output": "28\n"
},
{
"input": "A292992\n",
"output": "34\n"
},
{
"input": "A102222\n",
"output": "19\n"
},
{
"input": "A710210\n",
"output": "30\n"
},
{
"input": "A987623\n",
"output": "36\n"
},
{
"input": "A342987\n",
"output": "34\n"
},
{
"input": "A101099\n",
"output": "39\n"
},
{
"input": "A321046\n",
"output": "26\n"
},
{
"input": "A246810\n",
"output": "31\n"
},
{
"input": "A910109\n",
"output": "39\n"
},
{
"input": "A388338\n",
"output": "34\n"
},
{
"input": "A222210\n",
"output": "19\n"
},
{
"input": "A231010\n",
"output": "26\n"
},
{
"input": "A210210\n",
"output": "25\n"
},
{
"input": "A109109\n",
"output": "39\n"
},
{
"input": "A555555\n",
"output": "31\n"
},
{
"input": "A102210\n",
"output": "25\n"
},
{
"input": "A888888\n",
"output": "49\n"
},
{
"input": "A108888\n",
"output": "43\n"
}
] | code_contests | python | 0 | 6ce520a322c0c64898217a900192b449 |
A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer.
Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards.
He is allowed to at most once discard two or three cards with the same number. Of course, he won't discard cards if it's impossible to choose two or three cards with the same number.
Given five numbers written on cards, cay you find the minimum sum of numbers on remaining cards?
Input
The only line of the input contains five integers t1, t2, t3, t4 and t5 (1 ≤ ti ≤ 100) — numbers written on cards.
Output
Print the minimum possible sum of numbers written on remaining cards.
Examples
Input
7 3 7 3 20
Output
26
Input
7 9 3 1 8
Output
28
Input
10 10 10 10 10
Output
20
Note
In the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following.
* Do nothing and the sum would be 7 + 3 + 7 + 3 + 20 = 40.
* Remove two cards with a number 7. The remaining sum would be 3 + 3 + 20 = 26.
* Remove two cards with a number 3. The remaining sum would be 7 + 7 + 20 = 34.
You are asked to minimize the sum so the answer is 26.
In the second sample, it's impossible to find two or three cards with the same number. Hence, Limak does nothing and the sum is 7 + 9 + 1 + 3 + 8 = 28.
In the third sample, all cards have the same number. It's optimal to discard any three cards. The sum of two remaining numbers is 10 + 10 = 20.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | l=list(map(int,input().split()))
ma=0
for i in l :
k=0
for j in l :
if i==j :
k=k+1
if k>2 :
break
if k>1 :
if ma<k*i :
ma=k*i
print(sum(l)-ma)
| python | code_algorithm | [
{
"input": "7 9 3 1 8\n",
"output": "28\n"
},
{
"input": "7 3 7 3 20\n",
"output": "26\n"
},
{
"input": "10 10 10 10 10\n",
"output": "20\n"
},
{
"input": "1 2 2 2 2\n",
"output": "3\n"
},
{
"input": "20 21 22 23 24\n",
"output": "110\n"
},
{
"input": "8 8 8 2 2\n",
"output": "4\n"
},
{
"input": "1 90 1 91 1\n",
"output": "181\n"
},
{
"input": "1 1 1 1 4\n",
"output": "5\n"
},
{
"input": "1 1 1 1 2\n",
"output": "3\n"
},
{
"input": "2 2 2 100 100\n",
"output": "6\n"
},
{
"input": "1 2 3 4 5\n",
"output": "15\n"
},
{
"input": "98 99 100 99 100\n",
"output": "296\n"
},
{
"input": "1 1 1 8 1\n",
"output": "9\n"
},
{
"input": "100 100 100 100 100\n",
"output": "200\n"
},
{
"input": "1 1 1 1 1\n",
"output": "2\n"
},
{
"input": "1 1 2 2 5\n",
"output": "7\n"
},
{
"input": "98 98 98 98 23\n",
"output": "121\n"
},
{
"input": "60 1 75 1 92\n",
"output": "227\n"
},
{
"input": "20 29 20 29 20\n",
"output": "58\n"
},
{
"input": "1 1 2 98 99\n",
"output": "199\n"
},
{
"input": "12 12 7 7 7\n",
"output": "21\n"
},
{
"input": "1 9 9 9 10\n",
"output": "11\n"
},
{
"input": "5 50 5 5 60\n",
"output": "110\n"
},
{
"input": "20 31 20 31 20\n",
"output": "60\n"
},
{
"input": "100 99 98 97 96\n",
"output": "490\n"
},
{
"input": "100 100 99 99 98\n",
"output": "296\n"
},
{
"input": "31 31 20 20 20\n",
"output": "60\n"
},
{
"input": "29 29 20 20 20\n",
"output": "58\n"
},
{
"input": "3 7 7 7 10\n",
"output": "13\n"
},
{
"input": "1 1 100 100 100\n",
"output": "2\n"
},
{
"input": "2 2 5 10 10\n",
"output": "9\n"
},
{
"input": "2 2 2 10 10\n",
"output": "6\n"
},
{
"input": "30 30 20 20 20\n",
"output": "60\n"
},
{
"input": "11 10 10 10 10\n",
"output": "21\n"
},
{
"input": "20 20 20 31 31\n",
"output": "60\n"
},
{
"input": "90 11 11 10 10\n",
"output": "110\n"
},
{
"input": "8 8 2 2 2\n",
"output": "6\n"
},
{
"input": "1 2 3 100 100\n",
"output": "6\n"
},
{
"input": "1 3 3 3 1\n",
"output": "2\n"
},
{
"input": "8 7 1 8 7\n",
"output": "15\n"
},
{
"input": "15 40 90 40 90\n",
"output": "95\n"
},
{
"input": "1 2 3 4 1\n",
"output": "9\n"
},
{
"input": "7 7 7 8 8\n",
"output": "16\n"
},
{
"input": "100 1 1 1 1\n",
"output": "101\n"
},
{
"input": "8 1 8 8 8\n",
"output": "9\n"
},
{
"input": "2 2 3 3 3\n",
"output": "4\n"
},
{
"input": "20 20 20 30 30\n",
"output": "60\n"
},
{
"input": "1 1 15 20 20\n",
"output": "17\n"
}
] | code_contests | python | 0.6 | cf2eaa3d4547cba677e7f7f5052aeb91 |
One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in the middle of a lesson. And only a loud ringing of a school bell could interrupt his sweet dream.
Of course, the valuable material and the teacher's explanations were lost. However, Valera will one way or another have to do the homework. As he does not know the new material absolutely, he cannot do the job himself. That's why he asked you to help. You're his best friend after all, you just cannot refuse to help.
Valera's home task has only one problem, which, though formulated in a very simple way, has not a trivial solution. Its statement looks as follows: if we consider all positive integers in the interval [a;b] then it is required to count the amount of such numbers in this interval that their smallest divisor will be a certain integer k (you do not have to consider divisor equal to one). In other words, you should count the amount of such numbers from the interval [a;b], that are not divisible by any number between 2 and k - 1 and yet are divisible by k.
Input
The first and only line contains three positive integers a, b, k (1 ≤ a ≤ b ≤ 2·109, 2 ≤ k ≤ 2·109).
Output
Print on a single line the answer to the given problem.
Examples
Input
1 10 2
Output
5
Input
12 23 3
Output
2
Input
6 19 5
Output
0
Note
Comments to the samples from the statement:
In the first sample the answer is numbers 2, 4, 6, 8, 10.
In the second one — 15, 21
In the third one there are no such numbers.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def pr(x):
d = 2
while d * d <= x:
if x % d == 0:
return 0
d += 1
return 1
def cnt(n, k):
if not pr(k) or n < k: return 0
n1 = n // k
return n1 - sum(cnt(n1, i) for i in range(2, min(k, n1 + 1)))
a, b, k = map(int, input().split())
ans = cnt(b, k) - cnt(a - 1, k)
print(ans)
| python | code_algorithm | [
{
"input": "12 23 3\n",
"output": "2\n"
},
{
"input": "1 10 2\n",
"output": "5\n"
},
{
"input": "6 19 5\n",
"output": "0\n"
},
{
"input": "1 2000000000 10007\n",
"output": "16746\n"
},
{
"input": "19431 20000000 17\n",
"output": "225438\n"
},
{
"input": "1570000 800000000 30011\n",
"output": "0\n"
},
{
"input": "1000 2000000000 211\n",
"output": "989868\n"
},
{
"input": "1999999000 2000000000 23\n",
"output": "6\n"
},
{
"input": "1 2000000000 23\n",
"output": "14871653\n"
},
{
"input": "44711 44711 44711\n",
"output": "1\n"
},
{
"input": "1 2000000000 44711\n",
"output": "3\n"
},
{
"input": "300303 600000 503\n",
"output": "87\n"
},
{
"input": "1000000000 2000000000 4001\n",
"output": "19490\n"
},
{
"input": "5002230 10002230 233\n",
"output": "2079\n"
},
{
"input": "1009 1009 1009\n",
"output": "1\n"
},
{
"input": "1 2000000000 2003\n",
"output": "78092\n"
},
{
"input": "201 522 233\n",
"output": "1\n"
},
{
"input": "1 2000000000 41011\n",
"output": "724\n"
},
{
"input": "1999999999 2000000000 19\n",
"output": "0\n"
},
{
"input": "1000 2000000000 2\n",
"output": "999999501\n"
},
{
"input": "1 2000000000 100000007\n",
"output": "1\n"
},
{
"input": "1 2000000000 44017\n",
"output": "135\n"
},
{
"input": "1000000000 2000000000 5\n",
"output": "66666666\n"
},
{
"input": "213 1758 41\n",
"output": "1\n"
},
{
"input": "35000 100000000 50021\n",
"output": "1\n"
},
{
"input": "12 20000000 11\n",
"output": "415584\n"
},
{
"input": "1 2000000000 503\n",
"output": "347553\n"
},
{
"input": "800000 90000000 13000027\n",
"output": "1\n"
},
{
"input": "1999999999 2000000000 601\n",
"output": "0\n"
},
{
"input": "1999999999 2000000000 31\n",
"output": "1\n"
},
{
"input": "1999999999 2000000000 97\n",
"output": "0\n"
},
{
"input": "7000 43000 61\n",
"output": "96\n"
},
{
"input": "97 10403 101\n",
"output": "3\n"
},
{
"input": "4500 400000 30011\n",
"output": "1\n"
},
{
"input": "300 700 41\n",
"output": "0\n"
},
{
"input": "1999999999 2000000000 44017\n",
"output": "0\n"
},
{
"input": "1 2000000000 20011\n",
"output": "7327\n"
},
{
"input": "1 50341999 503\n",
"output": "9504\n"
},
{
"input": "108314 57823000 3001\n",
"output": "1755\n"
},
{
"input": "1 2000000000 1000000007\n",
"output": "1\n"
},
{
"input": "10034 20501000 53\n",
"output": "53698\n"
},
{
"input": "1000000000 2000000000 2\n",
"output": "500000001\n"
},
{
"input": "1008055011 1500050000 41\n",
"output": "1784635\n"
},
{
"input": "2000000000 2000000000 53\n",
"output": "0\n"
},
{
"input": "40 200000000 31\n",
"output": "1019019\n"
},
{
"input": "1 2000000000 50021\n",
"output": "1\n"
},
{
"input": "11 1840207360 44711\n",
"output": "1\n"
},
{
"input": "1 2000000000 1700000009\n",
"output": "1\n"
},
{
"input": "2020 6300 29\n",
"output": "28\n"
},
{
"input": "120 57513234 121\n",
"output": "0\n"
},
{
"input": "50 60000000 5\n",
"output": "3999997\n"
},
{
"input": "911186 911186 73\n",
"output": "0\n"
},
{
"input": "1 2000000000 107\n",
"output": "2205007\n"
},
{
"input": "4 4 4\n",
"output": "0\n"
},
{
"input": "599999 1000000000 653\n",
"output": "124742\n"
},
{
"input": "1 2000000000 2\n",
"output": "1000000000\n"
},
{
"input": "1 2000000000 3\n",
"output": "333333333\n"
},
{
"input": "1 2000000000 103\n",
"output": "2312816\n"
},
{
"input": "500000 8000000 4001\n",
"output": "0\n"
},
{
"input": "1 2000000000 1009\n",
"output": "151176\n"
},
{
"input": "2000000000 2000000000 2\n",
"output": "1\n"
},
{
"input": "18800310 20000000 53\n",
"output": "3135\n"
},
{
"input": "100000000 500000000 500\n",
"output": "0\n"
},
{
"input": "1 2000000000 46021\n",
"output": "1\n"
},
{
"input": "1 1000000000 999999997\n",
"output": "0\n"
},
{
"input": "41939949 2000000000 127\n",
"output": "1770826\n"
},
{
"input": "1000 10000 19\n",
"output": "86\n"
},
{
"input": "1 2000000000 5\n",
"output": "133333333\n"
},
{
"input": "200000000 2000000000 1800000011\n",
"output": "1\n"
},
{
"input": "300 300000 5003\n",
"output": "1\n"
},
{
"input": "40000000 1600000000 3001\n",
"output": "42482\n"
},
{
"input": "800201 90043000 307\n",
"output": "26902\n"
},
{
"input": "11 124 11\n",
"output": "2\n"
},
{
"input": "1 2000000000 900000011\n",
"output": "1\n"
},
{
"input": "1 2000000000 40009\n",
"output": "928\n"
},
{
"input": "100000 100000 5\n",
"output": "0\n"
},
{
"input": "100 1000 1009\n",
"output": "0\n"
},
{
"input": "1 20000000 3\n",
"output": "3333333\n"
},
{
"input": "1 1840207360 44711\n",
"output": "1\n"
},
{
"input": "1 2000000000 17\n",
"output": "22565668\n"
},
{
"input": "1 2000000000 37\n",
"output": "8262288\n"
},
{
"input": "1 2000000000 83\n",
"output": "2998028\n"
},
{
"input": "43104 2000000000 3\n",
"output": "333326149\n"
},
{
"input": "1 2000000000 800000011\n",
"output": "1\n"
},
{
"input": "1500000000 2000000000 11\n",
"output": "10389612\n"
},
{
"input": "1 2000000000 29\n",
"output": "11281946\n"
},
{
"input": "1900000000 2000000000 44711\n",
"output": "2\n"
},
{
"input": "19999999 2000000000 11\n",
"output": "41142857\n"
},
{
"input": "1 340431 3\n",
"output": "56739\n"
},
{
"input": "1 2000000000 1999073521\n",
"output": "0\n"
},
{
"input": "1 2000000000 67\n",
"output": "3927637\n"
},
{
"input": "2000000000 2000000000 211\n",
"output": "0\n"
},
{
"input": "1 2000000000 10000019\n",
"output": "1\n"
},
{
"input": "1 80 7\n",
"output": "3\n"
},
{
"input": "1 2000000000 19\n",
"output": "19002671\n"
},
{
"input": "99999 99999999 4001\n",
"output": "2212\n"
},
{
"input": "1 2000000000 13\n",
"output": "31968032\n"
},
{
"input": "1 2000000000 30011\n",
"output": "3399\n"
},
{
"input": "3500 100000 1009\n",
"output": "0\n"
},
{
"input": "1 2000000000 97\n",
"output": "2505943\n"
},
{
"input": "2 1000 4\n",
"output": "0\n"
},
{
"input": "1000 1000000000 1950000023\n",
"output": "0\n"
},
{
"input": "1 2000000000 400000009\n",
"output": "1\n"
},
{
"input": "1 1000000000 10\n",
"output": "0\n"
},
{
"input": "12000 700000000 97\n",
"output": "877658\n"
},
{
"input": "1 2000000000 1511\n",
"output": "101472\n"
},
{
"input": "30000 400000000 500009\n",
"output": "1\n"
},
{
"input": "1 2000000000 7\n",
"output": "76190476\n"
},
{
"input": "1 2000000000 4001\n",
"output": "40979\n"
},
{
"input": "1999999999 2000000000 2\n",
"output": "1\n"
},
{
"input": "1 2000000000 11\n",
"output": "41558442\n"
},
{
"input": "1999950000 2000000000 151\n",
"output": "38\n"
},
{
"input": "1 2000000000 50000017\n",
"output": "1\n"
},
{
"input": "50 600000000 2\n",
"output": "299999976\n"
},
{
"input": "1 2000000000 200000033\n",
"output": "1\n"
},
{
"input": "1 2000000000 8009\n",
"output": "21014\n"
}
] | code_contests | python | 0 | cf1fc032cdaa29e89108c14162261c42 |
You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix a consisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp.
Initially all m lamps are turned off.
Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.
It is guaranteed that if you push all n switches then all m lamps will be turned on.
Your think that you have too many switches and you would like to ignore one of them.
Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the m lamps will be turned on.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps.
The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise.
It is guaranteed that if you press all n switches all m lamps will be turned on.
Output
Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.
Examples
Input
4 5
10101
01000
00111
10000
Output
YES
Input
4 5
10100
01000
00110
00101
Output
NO
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def main():
n,m=map(int,input().split())
counts=[0]*m
a=[None]*n
for i in range(n):
line=input()
a[i]=line
for j in range(m):
counts[j]+=1 if line[j]=='1' else 0
if 1 not in counts:
print('YES')
return
checks=[]
for i in range(m):
if counts[i]==1:
checks.append(i)
leng=len(checks)
for i in range(n):
cur=a[i]
for j in range(leng):
if cur[checks[j]]=='1':
break
else:
print('YES')
return
print('NO')
main() | python | code_algorithm | [
{
"input": "4 5\n10100\n01000\n00110\n00101\n",
"output": "NO\n"
},
{
"input": "4 5\n10101\n01000\n00111\n10000\n",
"output": "YES\n"
},
{
"input": "3 5\n11100\n00110\n00011\n",
"output": "YES\n"
},
{
"input": "3 4\n1010\n0100\n1101\n",
"output": "YES\n"
},
{
"input": "2 5\n01000\n11111\n",
"output": "YES\n"
},
{
"input": "2 3\n101\n111\n",
"output": "YES\n"
},
{
"input": "2 5\n10000\n11111\n",
"output": "YES\n"
},
{
"input": "4 2\n10\n01\n10\n01\n",
"output": "YES\n"
},
{
"input": "3 3\n010\n101\n000\n",
"output": "YES\n"
},
{
"input": "2 6\n111111\n000000\n",
"output": "YES\n"
},
{
"input": "3 5\n00000\n10101\n01010\n",
"output": "YES\n"
},
{
"input": "2 3\n100\n111\n",
"output": "YES\n"
},
{
"input": "1 1\n1\n",
"output": "NO\n"
},
{
"input": "3 4\n0001\n1101\n1010\n",
"output": "YES\n"
},
{
"input": "4 5\n10000\n10101\n01000\n00111\n",
"output": "YES\n"
},
{
"input": "4 5\n01000\n10100\n00010\n10101\n",
"output": "YES\n"
},
{
"input": "3 3\n100\n100\n111\n",
"output": "YES\n"
},
{
"input": "2 2\n00\n11\n",
"output": "YES\n"
},
{
"input": "4 6\n111000\n100100\n010010\n001001\n",
"output": "YES\n"
},
{
"input": "3 4\n0001\n1101\n0110\n",
"output": "YES\n"
},
{
"input": "3 5\n10101\n01010\n01010\n",
"output": "YES\n"
},
{
"input": "2 2\n01\n11\n",
"output": "YES\n"
},
{
"input": "3 10\n1111011100\n0001100011\n1111010101\n",
"output": "YES\n"
},
{
"input": "4 5\n00000\n11000\n00110\n00011\n",
"output": "YES\n"
},
{
"input": "4 6\n100000\n110000\n001100\n000011\n",
"output": "YES\n"
},
{
"input": "2 3\n111\n000\n",
"output": "YES\n"
},
{
"input": "4 3\n000\n010\n001\n100\n",
"output": "YES\n"
},
{
"input": "4 4\n1111\n0000\n0000\n0000\n",
"output": "YES\n"
},
{
"input": "4 5\n10100\n11000\n00110\n00101\n",
"output": "YES\n"
},
{
"input": "5 5\n10000\n11000\n11100\n11110\n11111\n",
"output": "YES\n"
},
{
"input": "4 5\n00101\n00011\n01000\n10010\n",
"output": "YES\n"
},
{
"input": "10 1\n1\n0\n0\n0\n0\n0\n0\n0\n0\n1\n",
"output": "YES\n"
},
{
"input": "2 2\n11\n01\n",
"output": "YES\n"
},
{
"input": "2 1\n0\n1\n",
"output": "YES\n"
},
{
"input": "3 3\n111\n000\n000\n",
"output": "YES\n"
},
{
"input": "3 4\n0110\n1010\n0101\n",
"output": "YES\n"
},
{
"input": "2 5\n10101\n11111\n",
"output": "YES\n"
},
{
"input": "3 4\n1010\n0101\n1000\n",
"output": "YES\n"
},
{
"input": "3 3\n000\n000\n111\n",
"output": "YES\n"
},
{
"input": "3 3\n100\n010\n001\n",
"output": "NO\n"
},
{
"input": "3 8\n00111111\n01011100\n11000000\n",
"output": "YES\n"
},
{
"input": "3 3\n010\n100\n011\n",
"output": "YES\n"
},
{
"input": "2 3\n010\n111\n",
"output": "YES\n"
},
{
"input": "4 4\n1000\n1001\n0010\n0100\n",
"output": "YES\n"
},
{
"input": "3 5\n10110\n11000\n00111\n",
"output": "YES\n"
},
{
"input": "3 3\n111\n101\n001\n",
"output": "YES\n"
},
{
"input": "3 3\n010\n100\n001\n",
"output": "NO\n"
},
{
"input": "3 5\n00110\n10011\n01100\n",
"output": "YES\n"
},
{
"input": "1 5\n11111\n",
"output": "NO\n"
},
{
"input": "2 2\n11\n00\n",
"output": "YES\n"
},
{
"input": "3 3\n100\n001\n011\n",
"output": "YES\n"
},
{
"input": "4 15\n111110100011010\n111111011010110\n101000001011001\n100110000111011\n",
"output": "YES\n"
},
{
"input": "4 5\n10000\n01000\n10101\n00111\n",
"output": "YES\n"
},
{
"input": "2 2\n10\n11\n",
"output": "YES\n"
},
{
"input": "4 5\n10001\n10010\n01010\n00101\n",
"output": "YES\n"
},
{
"input": "3 3\n100\n110\n111\n",
"output": "YES\n"
},
{
"input": "2 5\n11111\n00000\n",
"output": "YES\n"
},
{
"input": "3 5\n10101\n00111\n01000\n",
"output": "NO\n"
},
{
"input": "2 5\n00100\n11111\n",
"output": "YES\n"
}
] | code_contests | python | 0.7 | 2abb124d791b68514ac99e949d37728b |
You have n coins, each of the same value of 1.
Distribute them into packets such that any amount x (1 ≤ x ≤ n) can be formed using some (possibly one or all) number of these packets.
Each packet may only be used entirely or not used at all. No packet may be used more than once in the formation of the single x, however it may be reused for the formation of other x's.
Find the minimum number of packets in such a distribution.
Input
The only line contains a single integer n (1 ≤ n ≤ 10^9) — the number of coins you have.
Output
Output a single integer — the minimum possible number of packets, satisfying the condition above.
Examples
Input
6
Output
3
Input
2
Output
2
Note
In the first example, three packets with 1, 2 and 3 coins can be made to get any amount x (1≤ x≤ 6).
* To get 1 use the packet with 1 coin.
* To get 2 use the packet with 2 coins.
* To get 3 use the packet with 3 coins.
* To get 4 use packets with 1 and 3 coins.
* To get 5 use packets with 2 and 3 coins
* To get 6 use all packets.
In the second example, two packets with 1 and 1 coins can be made to get any amount x (1≤ x≤ 2).
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math
n, = map(int, input().split())
print(math.floor(math.log2(n)) + 1) | python | code_algorithm | [
{
"input": "6\n",
"output": "3\n"
},
{
"input": "2\n",
"output": "2\n"
},
{
"input": "723061816\n",
"output": "30\n"
},
{
"input": "40052789\n",
"output": "26\n"
},
{
"input": "8\n",
"output": "4\n"
},
{
"input": "1\n",
"output": "1\n"
},
{
"input": "33554432\n",
"output": "26\n"
},
{
"input": "517188853\n",
"output": "29\n"
},
{
"input": "237913214\n",
"output": "28\n"
},
{
"input": "1023\n",
"output": "10\n"
},
{
"input": "536870911\n",
"output": "29\n"
},
{
"input": "781222222\n",
"output": "30\n"
},
{
"input": "952679912\n",
"output": "30\n"
},
{
"input": "56546546\n",
"output": "26\n"
},
{
"input": "33554434\n",
"output": "26\n"
},
{
"input": "33554430\n",
"output": "25\n"
},
{
"input": "543654364\n",
"output": "30\n"
},
{
"input": "1000000000\n",
"output": "30\n"
},
{
"input": "96145460\n",
"output": "27\n"
},
{
"input": "654645765\n",
"output": "30\n"
},
{
"input": "1022\n",
"output": "10\n"
},
{
"input": "32453535\n",
"output": "25\n"
},
{
"input": "443566\n",
"output": "19\n"
},
{
"input": "946942894\n",
"output": "30\n"
},
{
"input": "536870913\n",
"output": "30\n"
},
{
"input": "222222222\n",
"output": "28\n"
},
{
"input": "108461089\n",
"output": "27\n"
},
{
"input": "536870912\n",
"output": "30\n"
},
{
"input": "435543666\n",
"output": "29\n"
},
{
"input": "458841431\n",
"output": "29\n"
},
{
"input": "47814355\n",
"output": "26\n"
},
{
"input": "1025\n",
"output": "11\n"
},
{
"input": "1024\n",
"output": "11\n"
},
{
"input": "3\n",
"output": "2\n"
},
{
"input": "33554431\n",
"output": "25\n"
}
] | code_contests | python | 1 | c22aabb06777ad5f197b954fa258521d |
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are n bricks lined in a row on the ground. Chouti has got m paint buckets of different colors at hand, so he painted each brick in one of those m colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are k bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998 244 353.
Input
The first and only line contains three integers n, m and k (1 ≤ n,m ≤ 2000, 0 ≤ k ≤ n-1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
Output
Print one integer — the number of ways to color bricks modulo 998 244 353.
Examples
Input
3 3 0
Output
3
Input
3 2 1
Output
4
Note
In the first example, since k=0, the color of every brick should be the same, so there will be exactly m=3 ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 4 possible colorings.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from math import factorial
[n, m, k] = [int(i) for i in input().split()]
mod = 998244353
l = m*((m-1)**k)
L = factorial(n-1)//factorial(n-1-k)//factorial(k)
print((l*L)%mod)
| python | code_algorithm | [
{
"input": "3 3 0\n",
"output": "3\n"
},
{
"input": "3 2 1\n",
"output": "4\n"
},
{
"input": "1859 96 1471\n",
"output": "33410781\n"
},
{
"input": "555 1594 412\n",
"output": "438918750\n"
},
{
"input": "1987 237 1286\n",
"output": "609458581\n"
},
{
"input": "1 1 0\n",
"output": "1\n"
},
{
"input": "3 3 2\n",
"output": "12\n"
},
{
"input": "369 511 235\n",
"output": "520296460\n"
},
{
"input": "918 1704 848\n",
"output": "229358995\n"
},
{
"input": "1 534 0\n",
"output": "534\n"
},
{
"input": "1939 407 1072\n",
"output": "405465261\n"
},
{
"input": "22 1481 21\n",
"output": "870324282\n"
},
{
"input": "289 393 19\n",
"output": "184294470\n"
},
{
"input": "1446 1030 111\n",
"output": "794075632\n"
},
{
"input": "1969 30 744\n",
"output": "618963319\n"
},
{
"input": "50 2000 40\n",
"output": "461299924\n"
},
{
"input": "1894 637 1635\n",
"output": "189307651\n"
},
{
"input": "1926 817 0\n",
"output": "817\n"
},
{
"input": "1999 233 1998\n",
"output": "929315320\n"
},
{
"input": "1440 704 520\n",
"output": "954730150\n"
},
{
"input": "1411 1081 1082\n",
"output": "238077838\n"
},
{
"input": "1569 1548 644\n",
"output": "925334150\n"
},
{
"input": "1139 1252 348\n",
"output": "933447185\n"
},
{
"input": "70 1311 53\n",
"output": "697377585\n"
},
{
"input": "515 1563 110\n",
"output": "971635624\n"
},
{
"input": "2000 1234 1800\n",
"output": "550745740\n"
},
{
"input": "387 1422 339\n",
"output": "383877548\n"
},
{
"input": "518 518 36\n",
"output": "600327272\n"
},
{
"input": "2000 23 45\n",
"output": "383618765\n"
},
{
"input": "259 770 5\n",
"output": "711761505\n"
},
{
"input": "3 1 0\n",
"output": "1\n"
},
{
"input": "945 563 152\n",
"output": "386567314\n"
},
{
"input": "123 45 67\n",
"output": "212505593\n"
},
{
"input": "1046 1844 18\n",
"output": "502244233\n"
},
{
"input": "1841 1185 1765\n",
"output": "773828348\n"
},
{
"input": "3 1 1\n",
"output": "0\n"
},
{
"input": "1393 874 432\n",
"output": "676801447\n"
},
{
"input": "1999 333 1000\n",
"output": "101641915\n"
},
{
"input": "817 1719 588\n",
"output": "569385432\n"
},
{
"input": "2000 2000 1999\n",
"output": "694730459\n"
},
{
"input": "1539 1221 351\n",
"output": "938987357\n"
},
{
"input": "1234 567 890\n",
"output": "661457723\n"
},
{
"input": "1494 155 101\n",
"output": "97784646\n"
},
{
"input": "2000 2 1999\n",
"output": "2\n"
},
{
"input": "417 1045 383\n",
"output": "517114866\n"
},
{
"input": "646 1171 131\n",
"output": "200804462\n"
},
{
"input": "1174 688 472\n",
"output": "937340189\n"
}
] | code_contests | python | 0 | bc02c6dbc50be1c295d80c95420a4325 |
Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by 2 but the number itself is not divisible by 2. For example, 13, 1227, 185217 are ebne numbers, while 12, 2, 177013, 265918 are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification.
You are given a non-negative integer s, consisting of n digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between 0 (do not delete any digits at all) and n-1.
For example, if you are given s=222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 → 2237344218521717191. The sum of digits of 2237344218521717191 is equal to 70 and is divisible by 2, but number itself is not divisible by 2: it means that the resulting number is ebne.
Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer n (1 ≤ n ≤ 3000) — the number of digits in the original number.
The second line of each test case contains a non-negative integer number s, consisting of n digits.
It is guaranteed that s does not contain leading zeros and the sum of n over all test cases does not exceed 3000.
Output
For each test case given in the input print the answer in the following format:
* If it is impossible to create an ebne number, print "-1" (without quotes);
* Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits.
Example
Input
4
4
1227
1
0
6
177013
24
222373204424185217171912
Output
1227
-1
17703
2237344218521717191
Note
In the first test case of the example, 1227 is already an ebne number (as 1 + 2 + 2 + 7 = 12, 12 is divisible by 2, while in the same time, 1227 is not divisible by 2) so we don't need to delete any digits. Answers such as 127 and 17 will also be accepted.
In the second test case of the example, it is clearly impossible to create an ebne number from the given number.
In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, 1 digit such as 17703, 77013 or 17013. Answers such as 1701 or 770 will not be accepted as they are not ebne numbers. Answer 013 will not be accepted as it contains leading zeroes.
Explanation:
* 1 + 7 + 7 + 0 + 3 = 18. As 18 is divisible by 2 while 17703 is not divisible by 2, we can see that 17703 is an ebne number. Same with 77013 and 17013;
* 1 + 7 + 0 + 1 = 9. Because 9 is not divisible by 2, 1701 is not an ebne number;
* 7 + 7 + 0 = 14. This time, 14 is divisible by 2 but 770 is also divisible by 2, therefore, 770 is not an ebne number.
In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 → 22237320442418521717191 (delete the last digit).
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | for _ in range(int(input())):
n=int(input())
s=input()
if len(s)==1:
print(-1)
else:
#ans=s[0]
ans=''
f=0
for i in range(0,n):
if int(s[i])%2==1 and f<2:
ans+=s[i]
f+=1
#break
if f<2:
print(-1)
else:
print(ans)
| python | code_algorithm | [
{
"input": "4\n4\n1227\n1\n0\n6\n177013\n24\n222373204424185217171912\n",
"output": "17\n-1\n17\n37\n"
},
{
"input": "36\n6\n165310\n6\n177978\n6\n211759\n6\n212643\n6\n229540\n6\n250029\n6\n211519\n6\n256097\n6\n163478\n5\n91505\n5\n79280\n6\n260629\n6\n128051\n6\n121972\n6\n261633\n6\n172044\n6\n119055\n6\n208323\n6\n149942\n6\n161236\n6\n177150\n6\n233766\n5\n97949\n6\n230107\n6\n175822\n6\n213545\n6\n232837\n6\n179166\n6\n138603\n6\n130183\n6\n173543\n6\n234642\n6\n268015\n6\n228293\n6\n268893\n6\n239536\n",
"output": "15\n17\n11\n13\n95\n59\n11\n59\n13\n91\n79\n-1\n15\n11\n13\n17\n11\n33\n19\n11\n17\n33\n97\n31\n17\n13\n33\n17\n13\n13\n17\n-1\n15\n93\n93\n39\n"
},
{
"input": "14\n6\n173936\n5\n89468\n6\n162677\n5\n33260\n5\n86895\n6\n121916\n6\n173926\n6\n106186\n6\n177810\n6\n144529\n6\n139808\n5\n81892\n5\n73111\n5\n65712\n",
"output": "17\n-1\n17\n33\n95\n11\n17\n11\n17\n15\n13\n19\n73\n57\n"
},
{
"input": "1\n8\n11735682\n",
"output": "11\n"
},
{
"input": "4\n4\n1227\n1\n0\n6\n177013\n24\n222373204424185217171912\n",
"output": "17\n-1\n17\n37\n"
}
] | code_contests | python | 0 | aab6a3c70091367cd6693a5ff060b07c |
You are given three integers x, y and n. Your task is to find the maximum integer k such that 0 ≤ k ≤ n that k mod x = y, where mod is modulo operation. Many programming languages use percent operator % to implement it.
In other words, with given x, y and n you need to find the maximum possible integer from 0 to n that has the remainder y modulo x.
You have to answer t independent test cases. It is guaranteed that such k exists for each test case.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 5 ⋅ 10^4) — the number of test cases. The next t lines contain test cases.
The only line of the test case contains three integers x, y and n (2 ≤ x ≤ 10^9;~ 0 ≤ y < x;~ y ≤ n ≤ 10^9).
It can be shown that such k always exists under the given constraints.
Output
For each test case, print the answer — maximum non-negative integer k such that 0 ≤ k ≤ n and k mod x = y. It is guaranteed that the answer always exists.
Example
Input
7
7 5 12345
5 0 4
10 5 15
17 8 54321
499999993 9 1000000000
10 5 187
2 0 999999999
Output
12339
0
15
54306
999999995
185
999999998
Note
In the first test case of the example, the answer is 12339 = 7 ⋅ 1762 + 5 (thus, 12339 mod 7 = 5). It is obvious that there is no greater integer not exceeding 12345 which has the remainder 5 modulo 7.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | t=int(input())
for i in range(t):
x, y, n=map(int,input().split())
print(n-(n-y)%x) | python | code_algorithm | [
{
"input": "7\n7 5 12345\n5 0 4\n10 5 15\n17 8 54321\n499999993 9 1000000000\n10 5 187\n2 0 999999999\n",
"output": "12339\n0\n15\n54306\n999999995\n185\n999999998\n"
},
{
"input": "1\n31 2 104\n",
"output": "95\n"
},
{
"input": "1\n43284 1 33424242\n",
"output": "33415249\n"
},
{
"input": "1\n943643 1 23522222\n",
"output": "22647433\n"
},
{
"input": "1\n33 6 100\n",
"output": "72\n"
},
{
"input": "1\n1000000000 0 999999999\n",
"output": "0\n"
},
{
"input": "1\n4452384 1 3573842\n",
"output": "1\n"
}
] | code_contests | python | 0.9 | e16a294c9724a4cdff4d2863ab9a8094 |
Let's call a list of positive integers a_0, a_1, ..., a_{n-1} a power sequence if there is a positive integer c, so that for every 0 ≤ i ≤ n-1 then a_i = c^i.
Given a list of n positive integers a_0, a_1, ..., a_{n-1}, you are allowed to:
* Reorder the list (i.e. pick a permutation p of \{0,1,...,n - 1\} and change a_i to a_{p_i}), then
* Do the following operation any number of times: pick an index i and change a_i to a_i - 1 or a_i + 1 (i.e. increment or decrement a_i by 1) with a cost of 1.
Find the minimum cost to transform a_0, a_1, ..., a_{n-1} into a power sequence.
Input
The first line contains an integer n (3 ≤ n ≤ 10^5).
The second line contains n integers a_0, a_1, ..., a_{n-1} (1 ≤ a_i ≤ 10^9).
Output
Print the minimum cost to transform a_0, a_1, ..., a_{n-1} into a power sequence.
Examples
Input
3
1 3 2
Output
1
Input
3
1000000000 1000000000 1000000000
Output
1999982505
Note
In the first example, we first reorder \{1, 3, 2\} into \{1, 2, 3\}, then increment a_2 to 4 with cost 1 to get a power sequence \{1, 2, 4\}.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = 10 ** 9 + 7
def gcd(a, b):
while b:
a, b = b, a%b
return a
def isPrime(n):
if (n <= 1):
return False
i = 2
while i ** 2 <= n:
if n % i == 0:
return False
i += 1
return True
def primeFactors(n):
factors = defaultdict(int)
i = 2
while i ** 2 <= n:
while n % i == 0:
factors[i] += 1
n //= i
i += 1
if n > 1:
factors[n] += 1
return factors
def vars():
return map(int, input().split())
def array():
return list(map(int, input().split()))
def a():
n = int(input())
m = defaultdict(int)
for i in range(n):
s = input()
for c in s:
m[c] += 1
res = 'YES'
for c in m:
if m[c] % n != 0:
res = 'NO'
break
print(res)
def b():
n = int(input())
arr = array()
arr.sort()
maxi = arr[-1]
res1 = math.floor(maxi ** (1 / (n - 1)))
res2 = math.ceil(maxi ** (1 / (n - 1)))
ans1 = ans2 = 0
for i in range(n):
ans1 += abs(arr[i] - (res1 ** i))
for i in range(n):
ans2 += abs(arr[i] - (res2 ** i))
print(min(ans1, ans2))
if __name__ == "__main__":
t = 1
# t = int(input())
for _ in range(t):
b()
| python | code_algorithm | [
{
"input": "3\n1 3 2\n",
"output": "1\n"
},
{
"input": "3\n1000000000 1000000000 1000000000\n",
"output": "1999982505\n"
},
{
"input": "3\n377 9052790 230\n",
"output": "4152\n"
},
{
"input": "5\n1 1 912380429 929219321 1000000000\n",
"output": "1839804347\n"
},
{
"input": "3\n1 912387428 4\n",
"output": "45210\n"
},
{
"input": "16\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n",
"output": "14715827883\n"
},
{
"input": "4\n1 3 9 15\n",
"output": "12\n"
},
{
"input": "9\n61 925 605 571 301 250 368 776 404\n",
"output": "3750\n"
},
{
"input": "3\n626908730 747379008 293295878\n",
"output": "920190033\n"
},
{
"input": "16\n293 795 930 120 937 954 791 22 834 136 895 268 502 275 16 186\n",
"output": "7938\n"
},
{
"input": "20\n862 462 758 955 541 169 622 200 328 248 14 272 217 340 461 127 442 258 900 595\n",
"output": "8751\n"
},
{
"input": "33\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1000000000 1000000000 1000000000\n",
"output": "4073741790\n"
},
{
"input": "31\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n",
"output": "29000000001\n"
},
{
"input": "15\n261955 1189957 24178 3154 10626918 6 18311438 700 81716 414491147 328152562 151704786 803178 411 1758\n",
"output": "567739923\n"
},
{
"input": "6\n1695 3441 465 1223 1512410 262484428\n",
"output": "11585865\n"
},
{
"input": "6\n1 1 1 1 1 10\n",
"output": "9\n"
},
{
"input": "4\n1 5 25 125\n",
"output": "0\n"
},
{
"input": "6\n1 2 4 8 16 16\n",
"output": "16\n"
},
{
"input": "32\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1000000000 1000000000\n",
"output": "1221225472\n"
},
{
"input": "20\n51261 11877 300 30936722 84 75814681 352366 23 424 16392314 27267 832 4 562873474 33 516967731 158909407 32148531 66 757\n",
"output": "850575966\n"
},
{
"input": "9\n1 1 1 1 1 1 1 1 1526\n",
"output": "1517\n"
},
{
"input": "16\n13182 7 94 1 299 76 2 1 96952931 33909 12 74 314 2 2204 667982\n",
"output": "89060318\n"
},
{
"input": "11\n1 1 1 1 1 1 1 1 1 1 256\n",
"output": "255\n"
},
{
"input": "9\n1151 825745 7633 17 1959654 131569 111202 148264 131\n",
"output": "1169827\n"
},
{
"input": "10\n8 4676 1229 28377288 1062 7 7483297 521964 56330416 294497\n",
"output": "45935236\n"
},
{
"input": "4\n1 3 3 27\n",
"output": "6\n"
},
{
"input": "31\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912 1000000000\n",
"output": "73741824\n"
},
{
"input": "3\n376 10 4243\n",
"output": "338\n"
},
{
"input": "30\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n",
"output": "28926258177\n"
},
{
"input": "100\n881 479 355 759 257 497 690 598 275 446 439 787 257 326 584 713 322 5 253 781 434 307 164 154 241 381 38 942 680 906 240 11 431 478 628 959 346 74 493 964 455 746 950 41 585 549 892 687 264 41 487 676 63 453 861 980 477 901 80 907 285 506 619 748 773 743 56 925 651 685 845 313 419 504 770 324 2 559 405 851 919 128 318 698 820 409 547 43 777 496 925 918 162 725 481 83 220 203 609 617\n",
"output": "50651\n"
},
{
"input": "30\n1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n",
"output": "27926258178\n"
},
{
"input": "3\n983075578 599509400 331113881\n",
"output": "930594188\n"
},
{
"input": "12\n524 737 575 957 583 292 530 677 398 87 83 559\n",
"output": "4663\n"
},
{
"input": "3\n376 303865958 31262664\n",
"output": "31254273\n"
},
{
"input": "20\n227445 1 2548 49166 282300772 1185 130620 141 206124981 3149 285325717 5723634 836527122 127 702305 9 135225194 79266 2017 2577441\n",
"output": "867268874\n"
},
{
"input": "100\n14 2 16 934 46 169 31 17 1 234 11 335 211 3 4 1 55 84 5 31 1 28 2 5 364 925 240 74 94 248 36 84 12 9 423 8 358 169 1 29 365 263 4 695 1 425 135 48 707 9 18 67 126 849 119 373 8 21 8 14 43 533 8 4 330 24 1 14 1 1 111 6 167 9 168 12 14 268 1 592 181 6 16 2 912 797 12 637 108 7 43 16 5 647 7 278 883 888 118 12\n",
"output": "17351\n"
},
{
"input": "4\n1 2 984127392 912830214\n",
"output": "912788665\n"
},
{
"input": "32\n1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1000000000\n",
"output": "1000000104\n"
},
{
"input": "6\n189022 17862 1599106 1509 963 25932\n",
"output": "325796\n"
},
{
"input": "4\n5 5 5 5\n",
"output": "11\n"
},
{
"input": "3\n2498 14 28\n",
"output": "37\n"
},
{
"input": "4\n1 3 8 64\n",
"output": "9\n"
},
{
"input": "6\n214 932 130 889 13 236\n",
"output": "1233\n"
},
{
"input": "12\n22 4907 2 287 211 1918823 200960457 295009 24176786 10531837 1349 7331\n",
"output": "176878453\n"
},
{
"input": "3\n804992154 25960556 592109135\n",
"output": "618063088\n"
},
{
"input": "3\n959 747 656\n",
"output": "1373\n"
}
] | code_contests | python | 0.2 | 261bb81e96626dd8a742ad21523a17a8 |
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length — an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them — for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.
Input
The first line contains integer n (2 ≤ n ≤ 300) — amount of cities in Berland. Then there follow n lines with n integer numbers each — the matrix of shortest distances. j-th integer in the i-th row — di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.
Next line contains integer k (1 ≤ k ≤ 300) — amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000) — ai and bi — pair of cities, which the road connects, ci — the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.
Output
Output k space-separated integers qi (1 ≤ i ≤ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.
Examples
Input
2
0 5
5 0
1
1 2 3
Output
3
Input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
Output
17 12
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
mat = []
for _ in range(n):
mat.append(list(map(int, input().split())))
q = int(input())
ans= []
for _ in range(q):
x, y, w = map(int, input().split())
x-=1
y-=1
mat[x][y] = mat[y][x] = min(mat[x][y], w)
sum1 = 0
for i in range(n):
for j in range(n):
a = mat[i][y]+mat[x][y]+mat[x][j]
b = mat[i][x]+mat[x][y]+mat[y][j]
c = mat[i][j]
mat[i][j] = min(a, b, c)
sum1 += sum(mat[i])
ans.append(sum1//2)
print(*ans) | python | code_algorithm | [
{
"input": "2\n0 5\n5 0\n1\n1 2 3\n",
"output": "3\n"
},
{
"input": "3\n0 4 5\n4 0 9\n5 9 0\n2\n2 3 8\n1 2 1\n",
"output": "17 12\n"
},
{
"input": "3\n0 983 173\n983 0 810\n173 810 0\n3\n3 2 567\n2 3 767\n1 2 763\n",
"output": "1480 1480 1480\n"
},
{
"input": "5\n0 954 1255 266 751\n954 0 1677 688 1173\n1255 1677 0 989 504\n266 688 989 0 485\n751 1173 504 485 0\n5\n5 2 837\n3 4 692\n3 5 756\n3 1 151\n2 5 262\n",
"output": "8070 7476 7476 6062 5111\n"
},
{
"input": "4\n0 537 1064 656\n537 0 527 119\n1064 527 0 408\n656 119 408 0\n4\n1 4 123\n1 4 344\n3 2 982\n3 2 587\n",
"output": "1950 1950 1950 1950\n"
}
] | code_contests | python | 1 | f010ca4e45ee74d640722b337803d9fb |
Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms.
Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the right one. The robot can consecutively perform the following actions:
1. Take the leftmost item with the left hand and spend wi · l energy units (wi is a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy units;
2. Take the rightmost item with the right hand and spend wj · r energy units (wj is a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy units;
Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.
Input
The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104).
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).
Output
In the single line print a single number — the answer to the problem.
Examples
Input
3 4 4 19 1
42 3 99
Output
576
Input
4 7 2 3 9
1 2 3 4
Output
34
Note
Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units.
The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy units.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
n,L,r,QL,QR=map(int,sys.stdin.readline().split())
W=list(map(int,sys.stdin.readline().split()))
minn=10**10
SumsL=[0]*n
SumsR=[0]*n
s=0
for i in range(n):
s+=W[i]
SumsL[i]=s
for i in range(n-1):
ans=L*SumsL[i]+r*(s-SumsL[i])
if(n-(i+1)>i+1):
ans+=(abs(n-(i+1)-(i+1))-1)*QR
elif(i+1>n-(i+1)):
ans+=(abs(n-(i+1)-(i+1))-1)*QL
if(ans<minn):
minn=ans
if(s*L+(QL*(n-1)) < minn):
minn=s*L+(QL*(n-1))
if(s*r+(QR*(n-1)) < minn):
minn=s*r+(QR*(n-1))
print(minn)
| python | code_algorithm | [
{
"input": "3 4 4 19 1\n42 3 99\n",
"output": "576\n"
},
{
"input": "4 7 2 3 9\n1 2 3 4\n",
"output": "34\n"
},
{
"input": "2 3 4 5 6\n1 2\n",
"output": "11\n"
},
{
"input": "5 1 100 10000 1\n1 2 3 4 5\n",
"output": "906\n"
},
{
"input": "2 100 100 10000 10000\n100 100\n",
"output": "20000\n"
},
{
"input": "7 13 3 978 30\n7 1 5 4 3 2 1\n",
"output": "199\n"
},
{
"input": "7 3 13 30 978\n1 2 3 4 5 1 7\n",
"output": "199\n"
},
{
"input": "5 1 100 1 10000\n1 2 3 4 5\n",
"output": "19\n"
},
{
"input": "1 94 78 369 10000\n93\n",
"output": "7254\n"
},
{
"input": "6 32 47 965 897\n7 4 1 3 5 4\n",
"output": "948\n"
},
{
"input": "5 100 1 10000 1\n1 2 3 4 5\n",
"output": "19\n"
},
{
"input": "5 100 1 1 10000\n1 2 3 4 5\n",
"output": "312\n"
},
{
"input": "1 78 94 369 10000\n93\n",
"output": "7254\n"
}
] | code_contests | python | 0 | 51633be30bf8a0fff9cade921379b4c0 |
Two semifinals have just been in the running tournament. Each semifinal had n participants. There are n participants advancing to the finals, they are chosen as follows: from each semifinal, we choose k people (0 ≤ 2k ≤ n) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top k in their semifinal but got to the n - 2k of the best among the others.
The tournament organizers hasn't yet determined the k value, so the participants want to know who else has any chance to get to the finals and who can go home.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of participants in each semifinal.
Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109) — the results of the i-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences a1, a2, ..., an and b1, b2, ..., bn are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal.
Output
Print two strings consisting of n characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The i-th character in the j-th line should equal "1" if the i-th participant of the j-th semifinal has any chances to advance to the finals, otherwise it should equal a "0".
Examples
Input
4
9840 9920
9860 9980
9930 10020
10040 10090
Output
1110
1100
Input
4
9900 9850
9940 9930
10000 10020
10060 10110
Output
1100
1100
Note
Consider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090.
* If k = 0, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals.
* If k = 1, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds).
* If k = 2, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
sem1 = []
sem2 = []
l = [0,0]
for cont in range(0,n):
l = list(map(int, input().split()))
sem1.append(l[0])
sem2.append(l[1])
kmax = int(n/2)
max1 = 0
max2 = 0
for cont in range(0,n):
if sem1[max1] < sem2[max2]:
max1 += 1
else:
max2 += 1
ris1 = ['1']*(max([max1,kmax]))
ris2 = ['1']*max([max2,kmax])
ris1 = ris1 + ['0']*(n-len(ris1))
ris2 = ris2 + ['0']*(n-len(ris2))
print(''.join(ris1))
print(''.join(ris2)) | python | code_algorithm | [
{
"input": "4\n9840 9920\n9860 9980\n9930 10020\n10040 10090\n",
"output": "1110\n1100\n"
},
{
"input": "4\n9900 9850\n9940 9930\n10000 10020\n10060 10110\n",
"output": "1100\n1100\n"
},
{
"input": "3\n1 3\n2 4\n6 5\n",
"output": "110\n100\n"
},
{
"input": "3\n1 3\n2 5\n4 6\n",
"output": "110\n100\n"
},
{
"input": "4\n1 3\n2 4\n7 5\n8 6\n",
"output": "1100\n1100\n"
},
{
"input": "4\n5 1\n6 2\n7 3\n8 4\n",
"output": "1100\n1111\n"
},
{
"input": "3\n1 2\n4 3\n6 5\n",
"output": "100\n110\n"
},
{
"input": "4\n2 1\n3 4\n6 5\n7 8\n",
"output": "1100\n1100\n"
},
{
"input": "3\n2 1\n4 3\n5 6\n",
"output": "100\n110\n"
},
{
"input": "3\n1 3\n2 4\n5 6\n",
"output": "110\n100\n"
},
{
"input": "3\n1 4\n2 5\n3 6\n",
"output": "111\n100\n"
},
{
"input": "2\n3 1\n4 2\n",
"output": "10\n11\n"
},
{
"input": "3\n1 2\n3 4\n5 6\n",
"output": "110\n100\n"
},
{
"input": "3\n1 2\n3 5\n4 6\n",
"output": "110\n100\n"
},
{
"input": "8\n100 101\n200 201\n300 301\n310 400\n320 500\n330 600\n340 700\n350 800\n",
"output": "11111000\n11110000\n"
},
{
"input": "2\n1 2\n3 4\n",
"output": "10\n10\n"
},
{
"input": "2\n1 2\n4 3\n",
"output": "10\n10\n"
},
{
"input": "3\n2 1\n3 4\n6 5\n",
"output": "110\n100\n"
},
{
"input": "1\n1 2\n",
"output": "1\n0\n"
},
{
"input": "1\n2 1\n",
"output": "0\n1\n"
},
{
"input": "4\n1 4\n2 5\n3 6\n8 7\n",
"output": "1110\n1100\n"
},
{
"input": "3\n2 1\n3 5\n4 6\n",
"output": "110\n100\n"
}
] | code_contests | python | 0.3 | 077a60346d3bce71182b4f5c4ef4d977 |
You can find anything whatsoever in our Galaxy! A cubical planet goes round an icosahedral star. Let us introduce a system of axes so that the edges of the cubical planet are parallel to the coordinate axes and two opposite vertices lay in the points (0, 0, 0) and (1, 1, 1). Two flies live on the planet. At the moment they are sitting on two different vertices of the cubical planet. Your task is to determine whether they see each other or not. The flies see each other when the vertices they occupy lie on the same face of the cube.
Input
The first line contains three space-separated integers (0 or 1) — the coordinates of the first fly, the second line analogously contains the coordinates of the second fly.
Output
Output "YES" (without quotes) if the flies see each other. Otherwise, output "NO".
Examples
Input
0 0 0
0 1 0
Output
YES
Input
1 1 0
0 1 0
Output
YES
Input
0 0 0
1 1 1
Output
NO
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | #دو تا نقطه تو مختصات سه بعدی میده میگه اینا تو یک وجه از مکعل هستند یا نه
print('YES' if any(i == j for i,j in zip(list(input().split()) ,list(input().split()))) else 'NO') | python | code_algorithm | [
{
"input": "0 0 0\n1 1 1\n",
"output": "NO\n"
},
{
"input": "0 0 0\n0 1 0\n",
"output": "YES\n"
},
{
"input": "1 1 0\n0 1 0\n",
"output": "YES\n"
},
{
"input": "1 0 0\n0 0 0\n",
"output": "YES\n"
},
{
"input": "1 1 0\n0 0 1\n",
"output": "NO\n"
},
{
"input": "1 1 1\n0 1 1\n",
"output": "YES\n"
},
{
"input": "0 0 1\n1 0 0\n",
"output": "YES\n"
},
{
"input": "1 1 1\n0 0 0\n",
"output": "NO\n"
},
{
"input": "1 0 0\n0 1 0\n",
"output": "YES\n"
},
{
"input": "1 0 0\n0 1 1\n",
"output": "NO\n"
},
{
"input": "0 0 1\n1 0 1\n",
"output": "YES\n"
},
{
"input": "0 1 0\n0 1 1\n",
"output": "YES\n"
},
{
"input": "1 0 1\n0 1 0\n",
"output": "NO\n"
},
{
"input": "1 0 0\n0 0 1\n",
"output": "YES\n"
},
{
"input": "1 0 1\n0 0 0\n",
"output": "YES\n"
},
{
"input": "0 0 1\n1 1 1\n",
"output": "YES\n"
},
{
"input": "0 1 1\n1 1 1\n",
"output": "YES\n"
},
{
"input": "0 1 1\n1 0 1\n",
"output": "YES\n"
},
{
"input": "0 0 0\n1 1 0\n",
"output": "YES\n"
},
{
"input": "1 1 1\n1 1 0\n",
"output": "YES\n"
},
{
"input": "0 0 1\n0 0 0\n",
"output": "YES\n"
},
{
"input": "1 0 0\n1 1 0\n",
"output": "YES\n"
},
{
"input": "1 1 0\n1 0 1\n",
"output": "YES\n"
},
{
"input": "0 0 1\n0 1 0\n",
"output": "YES\n"
},
{
"input": "1 0 0\n1 1 1\n",
"output": "YES\n"
},
{
"input": "1 1 1\n1 0 1\n",
"output": "YES\n"
},
{
"input": "0 1 0\n1 0 0\n",
"output": "YES\n"
},
{
"input": "1 1 0\n0 1 1\n",
"output": "YES\n"
},
{
"input": "0 1 1\n0 0 1\n",
"output": "YES\n"
},
{
"input": "0 1 1\n1 1 0\n",
"output": "YES\n"
},
{
"input": "0 0 0\n1 0 1\n",
"output": "YES\n"
},
{
"input": "0 0 0\n1 0 0\n",
"output": "YES\n"
},
{
"input": "1 1 1\n0 1 0\n",
"output": "YES\n"
},
{
"input": "1 1 0\n0 0 0\n",
"output": "YES\n"
},
{
"input": "1 0 0\n1 0 1\n",
"output": "YES\n"
},
{
"input": "0 1 0\n0 0 1\n",
"output": "YES\n"
},
{
"input": "1 1 0\n1 0 0\n",
"output": "YES\n"
},
{
"input": "1 1 0\n1 1 1\n",
"output": "YES\n"
},
{
"input": "0 1 0\n1 1 1\n",
"output": "YES\n"
},
{
"input": "0 1 1\n1 0 0\n",
"output": "NO\n"
},
{
"input": "1 0 1\n1 1 0\n",
"output": "YES\n"
},
{
"input": "0 0 1\n0 1 1\n",
"output": "YES\n"
},
{
"input": "0 1 0\n0 0 0\n",
"output": "YES\n"
},
{
"input": "0 1 1\n0 0 0\n",
"output": "YES\n"
},
{
"input": "0 0 1\n1 1 0\n",
"output": "NO\n"
},
{
"input": "1 0 1\n1 1 1\n",
"output": "YES\n"
},
{
"input": "1 0 1\n0 0 1\n",
"output": "YES\n"
},
{
"input": "0 0 0\n0 1 1\n",
"output": "YES\n"
},
{
"input": "0 0 0\n0 0 1\n",
"output": "YES\n"
},
{
"input": "1 0 1\n0 1 1\n",
"output": "YES\n"
},
{
"input": "1 0 1\n1 0 0\n",
"output": "YES\n"
},
{
"input": "1 1 1\n0 0 1\n",
"output": "YES\n"
},
{
"input": "1 1 1\n1 0 0\n",
"output": "YES\n"
},
{
"input": "0 1 0\n1 1 0\n",
"output": "YES\n"
},
{
"input": "0 1 1\n0 1 0\n",
"output": "YES\n"
},
{
"input": "0 1 0\n1 0 1\n",
"output": "NO\n"
}
] | code_contests | python | 0.5 | 21be1975c4ece48ed1048b985fde2581 |
Vasya is studying in the last class of school and soon he will take exams. He decided to study polynomials. Polynomial is a function P(x) = a0 + a1x1 + ... + anxn. Numbers ai are called coefficients of a polynomial, non-negative integer n is called a degree of a polynomial.
Vasya has made a bet with his friends that he can solve any problem with polynomials. They suggested him the problem: "Determine how many polynomials P(x) exist with integer non-negative coefficients so that <image>, and <image>, where <image> and b are given positive integers"?
Vasya does not like losing bets, but he has no idea how to solve this task, so please help him to solve the problem.
Input
The input contains three integer positive numbers <image> no greater than 1018.
Output
If there is an infinite number of such polynomials, then print "inf" without quotes, otherwise print the reminder of an answer modulo 109 + 7.
Examples
Input
2 2 2
Output
2
Input
2 3 3
Output
1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | t,a,b=map(int,input().split())
if t==2 and a==3 and b>10000: res=0
elif a==t: res=('inf' if a==1 else 2) if a==b else 0
else: res=0 if (a-b)%(t-a) else (1 if t != b else 0)
print(res)
| python | code_algorithm | [
{
"input": "2 3 3\n",
"output": "1\n"
},
{
"input": "2 2 2\n",
"output": "2\n"
},
{
"input": "1 2 5\n",
"output": "1\n"
},
{
"input": "1000000000000000000 1000000000000000000 1000000000000000000\n",
"output": "2\n"
},
{
"input": "135645 1 365333453\n",
"output": "0\n"
},
{
"input": "1 12 1728\n",
"output": "1\n"
},
{
"input": "110 1000 998\n",
"output": "0\n"
},
{
"input": "1 5 5\n",
"output": "1\n"
},
{
"input": "1 3 6561\n",
"output": "1\n"
},
{
"input": "563236 135645 356563\n",
"output": "0\n"
},
{
"input": "1 10 999999999999999999\n",
"output": "0\n"
},
{
"input": "5 5 4\n",
"output": "0\n"
},
{
"input": "1 999999999 1000000000000000000\n",
"output": "0\n"
},
{
"input": "2 999999999999999999 1000000000000000000\n",
"output": "0\n"
},
{
"input": "7 8 9\n",
"output": "1\n"
},
{
"input": "1 4 288230376151711744\n",
"output": "1\n"
},
{
"input": "3 5 10\n",
"output": "0\n"
},
{
"input": "3 2 2\n",
"output": "1\n"
},
{
"input": "1 2 2\n",
"output": "1\n"
},
{
"input": "3 3 3\n",
"output": "2\n"
},
{
"input": "1 10 1000000000000000000\n",
"output": "1\n"
},
{
"input": "1000000000000 1000000000000000 1000000000000000000\n",
"output": "1\n"
},
{
"input": "2 3 1000000000000000000\n",
"output": "0\n"
},
{
"input": "1 2 65536\n",
"output": "1\n"
},
{
"input": "8 10 11\n",
"output": "0\n"
},
{
"input": "1 1 1\n",
"output": "inf\n"
},
{
"input": "5 30 930\n",
"output": "1\n"
},
{
"input": "1 2 4\n",
"output": "1\n"
},
{
"input": "2 2 10\n",
"output": "0\n"
},
{
"input": "5 2 2\n",
"output": "1\n"
},
{
"input": "12365 1 1\n",
"output": "1\n"
},
{
"input": "3 6 5\n",
"output": "0\n"
},
{
"input": "1 1000000000000000000 1000000000000000000\n",
"output": "1\n"
},
{
"input": "1 5 625\n",
"output": "1\n"
},
{
"input": "6 1 1\n",
"output": "1\n"
},
{
"input": "1 1 12345678901234567\n",
"output": "0\n"
},
{
"input": "110 115 114\n",
"output": "0\n"
},
{
"input": "1 2 128\n",
"output": "1\n"
},
{
"input": "1 7 1\n",
"output": "0\n"
},
{
"input": "1 125 15625\n",
"output": "1\n"
}
] | code_contests | python | 0 | ac6439212f8a6682c85982dbed3d816d |
In some country there are exactly n cities and m bidirectional roads connecting the cities. Cities are numbered with integers from 1 to n. If cities a and b are connected by a road, then in an hour you can go along this road either from city a to city b, or from city b to city a. The road network is such that from any city you can get to any other one by moving along the roads.
You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to city t1 in at most l1 hours and get from city s2 to city t2 in at most l2 hours.
Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.
Input
The first line contains two integers n, m (1 ≤ n ≤ 3000, <image>) — the number of cities and roads in the country, respectively.
Next m lines contain the descriptions of the roads as pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.
The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2, respectively (1 ≤ si, ti ≤ n, 0 ≤ li ≤ n).
Output
Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.
Examples
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
Output
0
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
Output
1
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
Output
-1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from itertools import combinations_with_replacement
from collections import deque
#sys.stdin = open("input_py.txt","r")
n, m = map(int, input().split())
G = [ [] for i in range(n)]
for i in range(m):
x, y = map(int, input().split())
x-=1; y-=1
G[x].append(y)
G[y].append(x)
def BFS(s):
dist = [-1 for i in range(n)]
dist[s] = 0
Q = deque()
Q.append(s)
while len(Q) > 0:
v = Q.popleft()
for to in G[v]:
if dist[to] < 0:
dist[to] = dist[v] + 1
Q.append(to)
return dist
Dist = [BFS(i) for i in range(n)]
s1, t1, l1 = map(int, input(). split())
s2, t2, l2 = map(int, input(). split())
s1-=1; t1-=1; s2-=1; t2-=1
if Dist[s1][t1] > l1 or Dist[s2][t2] > l2:
print(-1)
exit(0)
rest = Dist[s1][t1] + Dist[s2][t2]
for i in range(n):
for j in range(n):
if Dist[i][s1] + Dist[i][j] + Dist[j][t1] <= l1 and Dist[i][s2] + Dist[i][j] + Dist[j][t2] <= l2 :
rest = min(rest, Dist[i][j] + Dist[i][s1] + Dist[i][s2] + Dist[j][t1] + Dist[j][t2])
if Dist[i][s1] + Dist[i][j] + Dist[j][t1] <= l1 and Dist[j][s2] + Dist[i][j] + Dist[i][t2] <= l2 :
rest = min(rest, Dist[i][j] + Dist[j][t1] + Dist[j][s2] + Dist[i][s1] + Dist[i][t2])
print(m-rest) | python | code_algorithm | [
{
"input": "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 1\n",
"output": "-1\n"
},
{
"input": "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 2\n",
"output": "0\n"
},
{
"input": "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n2 4 2\n",
"output": "1\n"
},
{
"input": "2 1\n1 2\n1 1 0\n1 2 0\n",
"output": "-1\n"
},
{
"input": "1 0\n1 1 0\n1 1 0\n",
"output": "0\n"
},
{
"input": "2 1\n1 2\n1 1 0\n1 2 1\n",
"output": "0\n"
},
{
"input": "6 5\n1 2\n2 3\n3 4\n3 5\n2 6\n1 4 3\n5 6 3\n",
"output": "0\n"
},
{
"input": "9 9\n1 2\n2 3\n2 4\n4 5\n5 7\n5 6\n3 8\n8 9\n9 6\n1 7 4\n3 6 3\n",
"output": "2\n"
},
{
"input": "10 11\n1 3\n2 3\n3 4\n4 5\n4 6\n3 7\n3 8\n4 9\n4 10\n7 9\n8 10\n1 5 3\n6 2 3\n",
"output": "6\n"
},
{
"input": "6 5\n1 3\n2 3\n3 4\n4 5\n4 6\n1 6 3\n5 2 3\n",
"output": "0\n"
},
{
"input": "9 9\n1 2\n2 3\n2 4\n4 5\n5 7\n5 6\n3 8\n8 9\n9 6\n1 7 4\n3 6 4\n",
"output": "3\n"
},
{
"input": "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n4 2 2\n",
"output": "1\n"
}
] | code_contests | python | 0 | 8fb52aec285c2f45c1450b7a9fe86220 |
You are given three sticks with positive integer lengths of a, b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick.
Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them.
Input
The single line contains 4 integers a, b, c, l (1 ≤ a, b, c ≤ 3·105, 0 ≤ l ≤ 3·105).
Output
Print a single integer — the number of ways to increase the sizes of the sticks by the total of at most l centimeters, so that you can make a non-degenerate triangle from it.
Examples
Input
1 1 1 2
Output
4
Input
1 2 3 1
Output
2
Input
10 2 1 7
Output
0
Note
In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter.
In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def f(a, b, c, l):
k = min(l, a - b - c)
return 0 if a < b + c else (k + 1) * (k + 2) // 2
solve = lambda i: f(a + i, b, c, l - i) + f(b + i, c, a, l - i) + f(c + i, a, b, l - i)
a, b, c, l = map(int, input().split())
ans = (l + 1) * (l + 2) * (l + 3) // 6 - sum(solve(i) for i in range(l + 1))
print(ans) | python | code_algorithm | [
{
"input": "1 1 1 2\n",
"output": "4\n"
},
{
"input": "1 2 3 1\n",
"output": "2\n"
},
{
"input": "10 2 1 7\n",
"output": "0\n"
},
{
"input": "53553 262850 271957 182759\n",
"output": "834977070873802\n"
},
{
"input": "5276 8562 1074 8453\n",
"output": "49093268246\n"
},
{
"input": "5 7 30 100\n",
"output": "71696\n"
},
{
"input": "3 5 7 300000\n",
"output": "1125157499050009\n"
},
{
"input": "17 28 19 5558\n",
"output": "7396315389\n"
},
{
"input": "300000 200000 100000 0\n",
"output": "0\n"
},
{
"input": "2958 4133 233463 259655\n",
"output": "65797591388150\n"
},
{
"input": "100000 300000 100000 100100\n",
"output": "255131325\n"
},
{
"input": "100000 300000 100000 100003\n",
"output": "400012\n"
},
{
"input": "4 2 5 28\n",
"output": "1893\n"
},
{
"input": "100000 300000 100000 100002\n",
"output": "200005\n"
},
{
"input": "85 50 17 89\n",
"output": "68620\n"
},
{
"input": "552 250082 77579 278985\n",
"output": "596240712378446\n"
},
{
"input": "100000 300000 100000 100000\n",
"output": "0\n"
},
{
"input": "5 5 5 10000\n",
"output": "41841675001\n"
},
{
"input": "300000 300000 300000 300000\n",
"output": "4500090000549998\n"
},
{
"input": "100000 300000 200001 0\n",
"output": "1\n"
},
{
"input": "63 5 52 78\n",
"output": "46502\n"
},
{
"input": "10 15 17 10\n",
"output": "281\n"
},
{
"input": "9133 7818 3682 82004\n",
"output": "38306048676255\n"
},
{
"input": "100000 300000 199999 0\n",
"output": "0\n"
},
{
"input": "1 2 1 5\n",
"output": "20\n"
},
{
"input": "2 7 8 4\n",
"output": "25\n"
},
{
"input": "300000 200000 100000 1\n",
"output": "2\n"
},
{
"input": "5 5 5 300000\n",
"output": "1125157500250001\n"
},
{
"input": "61 100 3 8502\n",
"output": "27050809786\n"
},
{
"input": "100000 300000 100000 100010\n",
"output": "3000195\n"
},
{
"input": "98406 37723 3 257918\n",
"output": "1154347569149860\n"
},
{
"input": "100000 300000 100000 100001\n",
"output": "100002\n"
},
{
"input": "183808 8 8 294771\n",
"output": "622921327009564\n"
},
{
"input": "30 918 702 591\n",
"output": "14315560\n"
},
{
"input": "1 1 300000 300000\n",
"output": "599999\n"
},
{
"input": "81780 54799 231699 808\n",
"output": "0\n"
},
{
"input": "300000 300000 1 24234\n",
"output": "1186319275394\n"
},
{
"input": "300000 300000 1 300000\n",
"output": "2250045000350001\n"
},
{
"input": "2 42 49 93\n",
"output": "72542\n"
},
{
"input": "3 1 29 1\n",
"output": "0\n"
},
{
"input": "1 1 1 300000\n",
"output": "1125022500250001\n"
}
] | code_contests | python | 0 | 4d9787137d429e35b65b6311fc95099e |
Well, the series which Stepan watched for a very long time, ended. In total, the series had n episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not.
Stepan's dissatisfaction is the maximum number of consecutive series that Stepan did not watch.
Your task is to determine according to Stepan's memories if his dissatisfaction could be exactly equal to k.
Input
The first line contains two integers n and k (1 ≤ n ≤ 100, 0 ≤ k ≤ n) — the number of episodes in the series and the dissatisfaction which should be checked.
The second line contains the sequence which consists of n symbols "Y", "N" and "?". If the i-th symbol equals "Y", Stepan remembers that he has watched the episode number i. If the i-th symbol equals "N", Stepan remembers that he hasn't watched the epizode number i. If the i-th symbol equals "?", Stepan doesn't exactly remember if he has watched the episode number i or not.
Output
If Stepan's dissatisfaction can be exactly equal to k, then print "YES" (without qoutes). Otherwise print "NO" (without qoutes).
Examples
Input
5 2
NYNNY
Output
YES
Input
6 1
????NN
Output
NO
Note
In the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row — the episode number 3 and the episode number 4. The answer is "YES", because k = 2.
In the second test k = 1, Stepan's dissatisfaction is greater than or equal to 2 (because he remembers that he hasn't watch at least two episodes in a row — number 5 and number 6), even if he has watched the episodes from the first to the fourth, inclusive.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def process(S, k):
n = len(S)
if k > n:
return 'NO'
current = 0
for i in range(n):
if S[i]=='N':
current+=1
if current > k:
return 'NO'
else:
current = 0
start_work = True
for i in range(k):
if S[i]=='Y':
start_work = False
if start_work and (k==n or k < n and S[k] != 'N'):
return 'YES'
if n==1:
if k==1 and S[0] in ['?N']:
return 'YES'
if k==0 and S[0] in ['?Y']:
return 'YES'
return 'NO'
if k==n:
if 'Y' in S:
return 'NO'
return 'YES'
Y_count = 0
p1 = 0
for i in range(1, k+1):
if S[i]=='Y':
Y_count+=1
p2 = i
for i in range(k+1, n):
# print(p1, Y_count, p2)
if Y_count==0 and S[p1] != 'N' and (p2==n-1 or S[p2+1] != 'N'):
return 'YES'
p1+=1
p2+=1
if p2 < n and S[p2]=='Y':
Y_count+=1
if p1 < n and S[p1]=='Y':
Y_count-=1
# print(p1, Y_count, p2)
if Y_count==0 and p1 < n and S[p1] != 'N':
return 'YES'
return 'NO'
n, k = [int(x) for x in input().split()]
S = input()
print(process(S, k))
| python | code_algorithm | [
{
"input": "6 1\n????NN\n",
"output": "NO\n"
},
{
"input": "5 2\nNYNNY\n",
"output": "YES\n"
},
{
"input": "100 1\nYYYYYYYYY??YYN?YYNYYYYYYYNYYYYYYYYYYY?YN?YYYYY?YYYYYYYYYYYYY?YYYYYYYYYYYYN?YYYYYYYY?YYYYY?YYNYYYYYNY\n",
"output": "YES\n"
},
{
"input": "100 3\n?YNNYYNYYYYYYNYYYYYNY?NNYYYYNYY??NYYNYNYYYY?YYNYYNYYYYYYYYYYNYYYYNYYYYNYYYYNYYNYYYYYYNYNYNYYYYYYNNYY\n",
"output": "YES\n"
},
{
"input": "100 74\nNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN?NNNNNNNNNNNN?NNNNNNNNNNNNNN\n",
"output": "NO\n"
},
{
"input": "100 100\n????????????????????????????????????????????????????????????????????????????????????????????????????\n",
"output": "YES\n"
},
{
"input": "6 3\nNNYYN?\n",
"output": "NO\n"
},
{
"input": "1 0\nY\n",
"output": "YES\n"
},
{
"input": "3 2\n?Y?\n",
"output": "NO\n"
},
{
"input": "100 1\n???Y??????????????????????????????????????Y?????????N???Y????????Y?????Y???????Y??Y??????????YY?????\n",
"output": "YES\n"
},
{
"input": "51 1\nYYYNYNYNNYYNNY?YNYYYYYYNNYNYN??NYNYYNYYYYYYNNYNNNYY\n",
"output": "NO\n"
},
{
"input": "90 15\nYNNNNN?NNYNNYNNNN?NNNNYNNY?NNNNNNN?NNNNNNYN?NNYNNNNNN?NNYYNNYN?NNN??NNNNYNNN?YN?NNNNYNN?NY\n",
"output": "YES\n"
},
{
"input": "99 2\nYNYYYYYYYYYYYN?YYNYYYYYYYYYYYYYY?YYYNYYYYYYYYYYYYYNYYYYYYNY?YYYYYNNYYYNYNYYYYNYYYYYYYYYYYNYY?NYYYYY\n",
"output": "YES\n"
},
{
"input": "100 4\nYYNNNNYYYNNNNNNYNYYYNYYNYYNNYYNNNNNNNYNYYNYYNNYNNNNNYN?YNYYYNNYNNNNNYNNNNYYNYYYYYNYNNNNYYNNNNYNNNNYY\n",
"output": "NO\n"
},
{
"input": "6 4\nNN??NN\n",
"output": "NO\n"
},
{
"input": "100 3\nYYYYYYYYNNNYNYNYYYYNY?YYYYYYNYYYNYYYYYYYYYYYYNNYYYYYNYNYYNYYYYYYYYYYYYYYYYYYY?YYNNYYNNYYYNYYYYYYYYYY\n",
"output": "YES\n"
},
{
"input": "90 18\nNNNN?NNNNNYNYNYNNY?NNNNNNNNNNNNNNYNNNNNNYYNYYNNNNYNNNNNNNNNNNNNNNNNNNYNNYYNYNNNNNNNYNNNNYN\n",
"output": "NO\n"
},
{
"input": "1 1\nY\n",
"output": "NO\n"
},
{
"input": "1 1\nN\n",
"output": "YES\n"
},
{
"input": "85 18\nNNNNNNN??Y???NN?YNNNNNNNN???YNNNNNN??Y?N?YNYYNN?NNNNNNNNNNNNNN????NNY??NNNN?NN??NNNNN\n",
"output": "YES\n"
},
{
"input": "1 0\nN\n",
"output": "NO\n"
},
{
"input": "100 6\n????????????????????????????????????????????????????????????????????????????????????????????????????\n",
"output": "YES\n"
},
{
"input": "10 1\nNY???NY?Y?\n",
"output": "YES\n"
},
{
"input": "51 5\nY??N????????Y??N?????N???N???YN?N?Y?N??Y?Y??Y???NN?\n",
"output": "YES\n"
},
{
"input": "24 4\nY?NYYNYYYNYYN?NNN?N?Y?Y?\n",
"output": "NO\n"
},
{
"input": "70 3\nY?N?Y???NN?NY?N?YY?Y????YNYY?Y?N??Y????YY??N????NY?NYY?YY?YYYY?YY?N?Y?\n",
"output": "YES\n"
},
{
"input": "30 2\n??????????????????????????????\n",
"output": "YES\n"
},
{
"input": "20 8\nNNNYY?????NN???N?YN?\n",
"output": "YES\n"
},
{
"input": "100 21\n?NNNNNYNN??NNN?N????N?NN?N??NN?NNNY?NN?NY?NN?NNN?NN?N?NNNNNNY?NYNN??N??NYNN?NN?NNNN?N???NN?NN?Y?NYNY\n",
"output": "YES\n"
},
{
"input": "99 1\nYYYYYYYNYYY??YY??YYYYYYY????NYY?YYY?Y??YYYY????YY?YY?YYY?YY??YYY?Y??NYYYY?YNYY??Y??YYYYY?YYY????YYY\n",
"output": "YES\n"
},
{
"input": "7 3\nN?YY???\n",
"output": "YES\n"
},
{
"input": "100 8\nNYNNY?YNNNNNN?NNNNNYNY?YYNYNN?NNNY??NNYNYNNNYNNNYNNNNNNNNY?NNNYNYN?NNNY?YY?NNYNN?NNNYNNYNNYN?NNYNYNN\n",
"output": "YES\n"
},
{
"input": "1 1\n?\n",
"output": "YES\n"
},
{
"input": "85 10\nYNNYNNNNNYNNNNNNNNNNNYNYYNNYNNNYYYNNNYYNNNNYNNNYNNNYNNNNNNNNNNNNN?NNNNYNNYYNNNNNNYNNN\n",
"output": "NO\n"
},
{
"input": "1 0\n?\n",
"output": "YES\n"
},
{
"input": "100 19\nYYNN?NNNNNNNNNNNYNYYNYNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNYNNNNNNYNNYYNNNYNNNYNYNNYNNNYYNNNYNNN?NNNNN?YNN\n",
"output": "NO\n"
},
{
"input": "100 26\nNNYNNNNNNNNNNNNN?NNNNNNNNNNNNNYNNNNYNNNNNNNNNNNNYNNNNNN?NNNYNNNNNNNNNNYYNNNNNNNNYNNNNNNNNYYYNNNNYYNY\n",
"output": "NO\n"
},
{
"input": "100 34\n?NNNN??N???NNNN?NNN?N???N?N????NNNNNNN?N??N???NNNN???N?N?NN?NNNNN?NNN???N??NN??Y??NNN??N?NNN???NN?NN\n",
"output": "YES\n"
},
{
"input": "40 14\nNNNNNNNNNNNNNNNNNYNNNNYNNYNNNNNNYNNNNNNN\n",
"output": "NO\n"
},
{
"input": "100 10\nN?NNYYYNNNNNNYYNNYYNNNNNNNNYYNNNYYNNYNYNY?NNNNNNNNNYYNNNNYNNNNYNNNYNNYNNN?NNY?NNNNNNNNN?NYNYNNNNNNNN\n",
"output": "YES\n"
},
{
"input": "100 2\nYYYYYYYYYYYNYYYYYYYYYYYYYYYYYYYYYYYYYNYY?YYYYYYYYYYYYYYYNYYYYYYYYYYYYNNYYYYYYYYYNYYYYYYYYYYNYYYYYYYY\n",
"output": "YES\n"
},
{
"input": "100 10\nNNNNYNNNYNNNNNNNNYNYNYNNNNNYNNNNNYNNNNNNNNNNNYNYYNNNNNNNYYNNYNYNNYYNNNNYNNNNNYNNNNYNNNNYNNY??YNNNNYY\n",
"output": "NO\n"
},
{
"input": "20 7\nN?N??NNN?NNN?Y???Y??\n",
"output": "YES\n"
},
{
"input": "3 3\n?Y?\n",
"output": "NO\n"
},
{
"input": "100 2\nYYNNYYYNNYYYYYYYYYYYYYYYNYYYNYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYNYNYYYYYYNNYYYNYYNNYYNYYYYNYNYYYYYYNYYY\n",
"output": "YES\n"
},
{
"input": "100 3\nNYNNYYYYYYNYNNYYYYYYNYYNYNYYYYYNYYYYYNNNYYYYYNYNYYNYYNYYNYNNNYYNYYYYYNYYYYYYNNYYNYNNYYNYYYY?YYNNYYNN\n",
"output": "YES\n"
},
{
"input": "70 3\nYNNNYYYNY?YYNYYNYYN?NYYYYYYYYYYYYYNYYNNYYYYYYYNYYNNNY??YYNYYYYYYYYNYYN\n",
"output": "YES\n"
},
{
"input": "30 1\nNYYYNYYY?Y?YY?YYYYYYYYYYYYYNYY\n",
"output": "YES\n"
},
{
"input": "40 17\nNNNNNNNNNNNNNNNNNYNNNNYNNYNNNNNNYNNNNNNN\n",
"output": "YES\n"
}
] | code_contests | python | 0 | 3ecc96e75eee7a70cd239f72f1cff7b2 |
Hideo Kojima has just quit his job at Konami. Now he is going to find a new place to work. Despite being such a well-known person, he still needs a CV to apply for a job.
During all his career Hideo has produced n games. Some of them were successful, some were not. Hideo wants to remove several of them (possibly zero) from his CV to make a better impression on employers. As a result there should be no unsuccessful game which comes right after successful one in his CV.
More formally, you are given an array s1, s2, ..., sn of zeros and ones. Zero corresponds to an unsuccessful game, one — to a successful one. Games are given in order they were produced, and Hideo can't swap these values. He should remove some elements from this array in such a way that no zero comes right after one.
Besides that, Hideo still wants to mention as much games in his CV as possible. Help this genius of a man determine the maximum number of games he can leave in his CV.
Input
The first line contains one integer number n (1 ≤ n ≤ 100).
The second line contains n space-separated integer numbers s1, s2, ..., sn (0 ≤ si ≤ 1). 0 corresponds to an unsuccessful game, 1 — to a successful one.
Output
Print one integer — the maximum number of games Hideo can leave in his CV so that no unsuccessful game comes after a successful one.
Examples
Input
4
1 1 0 1
Output
3
Input
6
0 1 0 0 1 0
Output
4
Input
1
0
Output
1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def main():
n = int(input())
games = list(map(int, input().split(' ')))
result = max([games[:i].count(0) + games[i:].count(1) for i in range(n+1)])
print(result)
if __name__ == "__main__":
main()
| python | code_algorithm | [
{
"input": "6\n0 1 0 0 1 0\n",
"output": "4\n"
},
{
"input": "1\n0\n",
"output": "1\n"
},
{
"input": "4\n1 1 0 1\n",
"output": "3\n"
},
{
"input": "4\n1 0 0 1\n",
"output": "3\n"
},
{
"input": "100\n1 1 0 1 1 0 0 0 0 1 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 0 1 1 0 0 1 1 1 0 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 1 1 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 1 1 0 1 1 0 1 1 1 0 0 1 1 0 0 0 0 1\n",
"output": "53\n"
},
{
"input": "1\n1\n",
"output": "1\n"
},
{
"input": "3\n1 0 0\n",
"output": "2\n"
},
{
"input": "3\n1 0 1\n",
"output": "2\n"
},
{
"input": "100\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
"output": "100\n"
},
{
"input": "2\n0 1\n",
"output": "2\n"
},
{
"input": "16\n1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 1\n",
"output": "9\n"
},
{
"input": "100\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "100\n"
},
{
"input": "90\n1 0 0 1 1 0 0 1 0 0 0 0 1 1 1 0 1 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 1 1 0 1 0 1 0 0 0 0 1 0 0 1 1 1 1 1 0 0 0 1 0\n",
"output": "52\n"
},
{
"input": "5\n0 1 0 0 1\n",
"output": "4\n"
},
{
"input": "78\n0 0 1 0 1 0 1 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 1 0 1 0 0 0 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0\n",
"output": "42\n"
},
{
"input": "10\n1 1 0 0 0 1 1 0 0 0\n",
"output": "6\n"
},
{
"input": "100\n0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
"output": "80\n"
},
{
"input": "3\n1 1 0\n",
"output": "2\n"
}
] | code_contests | python | 0.2 | a832c0484a251c910eace4ef7f1d92dd |
It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly S slices.
It is known that the i-th contestant will eat si slices of pizza, and gain ai happiness for each slice of type 1 pizza they eat, and bi happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved?
Input
The first line of input will contain integers N and S (1 ≤ N ≤ 105, 1 ≤ S ≤ 105), the number of contestants and the number of slices per pizza, respectively. N lines follow.
The i-th such line contains integers si, ai, and bi (1 ≤ si ≤ 105, 1 ≤ ai ≤ 105, 1 ≤ bi ≤ 105), the number of slices the i-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively.
Output
Print the maximum total happiness that can be achieved.
Examples
Input
3 12
3 5 7
4 6 7
5 9 5
Output
84
Input
6 10
7 4 7
5 8 8
12 5 8
6 11 6
3 3 7
5 9 6
Output
314
Note
In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5 + 4·6 + 5·9 = 84, and if you buy a type 2 pizza, the total happiness will be 3·7 + 4·7 + 5·5 = 74.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def cns(ts,s):
if ts/s==int(ts/s):
return ts
else:
return (int(ts/s)+1)*s
n,spp=[int(i) for i in input().split()]
tsr=0
da=[[] for i in range(100005)]
db=[[] for i in range(100005)]
sl=[]
for i in range(n):
sl.append([int(j) for j in input().split()])
tsr+=sl[i][0]
if sl[i][1]>sl[i][2]:
da[sl[i][1]-sl[i][2]].append(i)
else:
db[sl[i][2]-sl[i][1]].append(i)
tsa=cns(tsr,spp)
a1=0
c1=0
for i in range(100000,-1,-1):
for j in da[i]:
a1+=sl[j][0]*sl[j][1]
c1+=sl[j][0]
c1r=cns(c1,spp)-c1
c2r=tsa-cns(c1,spp)
for i in range(100000,-1,-1):
for j in db[i]:
if sl[j][0]>c2r:
a1+=c2r*sl[j][2]
a1+=(sl[j][0]-c2r)*sl[j][1]
c2r=0
else:
a1+=sl[j][0]*sl[j][2]
c2r-=sl[j][0]
a2=0
c2=0
for i in range(100000,-1,-1):
for j in db[i]:
a2+=sl[j][0]*sl[j][2]
c2+=sl[j][0]
c2r=cns(c2,spp)-c2
c1r=tsa-cns(c2,spp)
for i in range(100000,-1,-1):
for j in da[i]:
if sl[j][0]>c1r:
a2+=c1r*sl[j][1]
a2+=(sl[j][0]-c1r)*sl[j][2]
c1r=0
else:
a2+=sl[j][0]*sl[j][1]
c1r-=sl[j][0]
print(max(a1,a2))
| python | code_algorithm | [
{
"input": "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6\n",
"output": "314\n"
},
{
"input": "3 12\n3 5 7\n4 6 7\n5 9 5\n",
"output": "84\n"
},
{
"input": "25 6\n1 10 5\n1 8 4\n1 8 2\n4 8 9\n3 2 8\n1 9 5\n2 10 10\n3 9 6\n3 5 4\n2 7 8\n2 3 2\n2 6 8\n3 7 8\n4 3 7\n1 8 1\n3 6 4\n3 2 8\n2 2 1\n4 8 8\n4 8 4\n3 10 2\n3 6 6\n2 2 5\n1 6 2\n4 1 5\n",
"output": "449\n"
},
{
"input": "3 5\n6 4 5\n6 5 5\n8 7 5\n",
"output": "116\n"
},
{
"input": "2 9\n6 1 7\n6 7 1\n",
"output": "84\n"
},
{
"input": "2 10\n7 2 1\n7 1 2\n",
"output": "28\n"
},
{
"input": "2 3\n2 10 1\n2 1 10\n",
"output": "40\n"
},
{
"input": "3 10\n10 3 4\n5 1 100\n5 100 1\n",
"output": "1035\n"
},
{
"input": "2 3\n5 10 5\n5 5 10\n",
"output": "100\n"
},
{
"input": "3 3\n6 5 6\n2 5 4\n2 4 5\n",
"output": "56\n"
},
{
"input": "10 8\n7 1 4\n4 8 9\n3 4 10\n5 5 9\n1 5 6\n1 8 5\n5 7 4\n5 4 6\n10 5 7\n9 7 3\n",
"output": "351\n"
},
{
"input": "1 100\n97065 97644 98402\n",
"output": "9551390130\n"
},
{
"input": "3 4\n2 1 10\n1 2 1\n1 3 1\n",
"output": "22\n"
},
{
"input": "2 3\n5 5 10\n5 10 5\n",
"output": "100\n"
},
{
"input": "2 100000\n50000 1 100000\n50000 100000 1\n",
"output": "5000050000\n"
},
{
"input": "1 100000\n1 82372 5587\n",
"output": "82372\n"
},
{
"input": "3 5\n2 7 4\n6 5 9\n6 5 6\n",
"output": "102\n"
},
{
"input": "2 10\n9 1 2\n9 2 1\n",
"output": "36\n"
}
] | code_contests | python | 0 | e65283b3e357216a5a54261e1220b87f |
The cities of Byteland and Berland are located on the axis Ox. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line Ox there are three types of cities:
* the cities of Byteland,
* the cities of Berland,
* disputed cities.
Recently, the project BNET has been launched — a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.
The countries agreed to connect the pairs of cities with BNET cables in such a way that:
* If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables,
* If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables.
Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.
The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.
Each city is a point on the line Ox. It is technically possible to connect the cities a and b with a cable so that the city c (a < c < b) is not connected to this cable, where a, b and c are simultaneously coordinates of the cities a, b and c.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^{5}) — the number of cities.
The following n lines contains an integer x_i and the letter c_i (-10^{9} ≤ x_i ≤ 10^{9}) — the coordinate of the city and its type. If the city belongs to Byteland, c_i equals to 'B'. If the city belongs to Berland, c_i equals to «R». If the city is disputed, c_i equals to 'P'.
All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.
Output
Print the minimal total length of such set of cables, that if we delete all Berland cities (c_i='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities (c_i='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.
Examples
Input
4
-5 R
0 P
3 P
7 B
Output
12
Input
5
10 R
14 B
16 B
21 R
32 R
Output
24
Note
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be 5 + 3 + 4 = 12.
In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates 10, 21, 32, so to connect them you need two cables of length 11 and 11. The cities of Byteland have coordinates 14 and 16, so to connect them you need one cable of length 2. Thus, the total length of all cables is 11 + 11 + 2 = 24.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def solve(length, cities):
result = 0
lastP = None
lastB = None
lastR = None
maxB = 0
maxR = 0
for idx, city in enumerate(cities):
i, code = city
if(code == 'B'):
if(lastB != None):
result += abs(i - lastB)
maxB = max(maxB, abs(i - lastB))
lastB = i
if(code == 'R'):
if(lastR != None):
result += abs(i - lastR)
maxR = max(maxR, abs(i - lastR))
lastR = i
if(code == 'P'):
# B case
if(lastB != None):
result += abs(i - lastB)
maxB = max(maxB, abs(i - lastB))
# R case
if(lastR != None):
result += abs(i - lastR)
maxR = max(maxR, abs(i - lastR))
if(lastP != None):
result += min(0, abs(i - lastP) - maxB - maxR)
maxB = 0
maxR = 0
lastB = i
lastR = i
lastP = i
return result
if __name__ == '__main__':
length = int(input())
cities = []
for i in range(length):
data = input().split(" ")
cities.append((int(data[0]), data[1]))
result = solve(length, cities)
print(result)
| python | code_algorithm | [
{
"input": "5\n10 R\n14 B\n16 B\n21 R\n32 R\n",
"output": "24\n"
},
{
"input": "4\n-5 R\n0 P\n3 P\n7 B\n",
"output": "12\n"
},
{
"input": "2\n1 R\n2 R\n",
"output": "1\n"
},
{
"input": "6\n0 B\n3 P\n7 B\n9 B\n11 P\n13 B\n",
"output": "17\n"
},
{
"input": "9\n-105 R\n-81 B\n-47 P\n-25 R\n-23 B\n55 P\n57 R\n67 B\n76 P\n",
"output": "272\n"
},
{
"input": "2\n-1000000000 P\n1000000000 P\n",
"output": "2000000000\n"
},
{
"input": "6\n-8401 R\n-5558 P\n-3457 P\n-2361 R\n6966 P\n8140 B\n",
"output": "17637\n"
},
{
"input": "6\n-13 R\n-10 P\n-6 R\n-1 P\n4 R\n10 P\n",
"output": "32\n"
},
{
"input": "10\n61 R\n64 R\n68 R\n71 R\n72 R\n73 R\n74 P\n86 P\n87 B\n90 B\n",
"output": "29\n"
},
{
"input": "2\n-1000000000 B\n1000000000 P\n",
"output": "2000000000\n"
},
{
"input": "10\n66 R\n67 R\n72 R\n73 R\n76 R\n78 B\n79 B\n83 B\n84 B\n85 P\n",
"output": "26\n"
},
{
"input": "8\n-12 P\n-9 B\n-2 R\n-1 R\n2 B\n8 B\n9 R\n15 P\n",
"output": "54\n"
},
{
"input": "8\n-839 P\n-820 P\n-488 P\n-334 R\n-83 B\n187 R\n380 B\n804 P\n",
"output": "2935\n"
},
{
"input": "15\n-9518 R\n-6858 P\n-6726 B\n-6486 R\n-4496 P\n-4191 P\n-772 B\n-258 R\n-194 P\n1035 R\n2297 P\n4816 B\n5779 R\n9342 B\n9713 B\n",
"output": "25088\n"
},
{
"input": "2\n-1000000000 B\n1000000000 R\n",
"output": "0\n"
}
] | code_contests | python | 0 | 966dc7be0e945c4f1a1cbe45d3250b6b |
Berland Football Cup starts really soon! Commentators from all over the world come to the event.
Organizers have already built n commentary boxes. m regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.
If n is not divisible by m, it is impossible to distribute the boxes to the delegations at the moment.
Organizers can build a new commentary box paying a burles and demolish a commentary box paying b burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.
What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by m)?
Input
The only line contains four integer numbers n, m, a and b (1 ≤ n, m ≤ 10^{12}, 1 ≤ a, b ≤ 100), where n is the initial number of the commentary boxes, m is the number of delegations to come, a is the fee to build a box and b is the fee to demolish a box.
Output
Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by m). It is allowed that the final number of the boxes is equal to 0.
Examples
Input
9 7 3 8
Output
15
Input
2 7 3 7
Output
14
Input
30 6 17 19
Output
0
Note
In the first example organizers can build 5 boxes to make the total of 14 paying 3 burles for the each of them.
In the second example organizers can demolish 2 boxes to make the total of 0 paying 7 burles for the each of them.
In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 5 boxes.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n, m, a, b = list(map(int, input().split()))
k = n%m
print(min(k*b, (m - k)*a))
| python | code_algorithm | [
{
"input": "30 6 17 19\n",
"output": "0\n"
},
{
"input": "2 7 3 7\n",
"output": "14\n"
},
{
"input": "9 7 3 8\n",
"output": "15\n"
},
{
"input": "29 6 1 2\n",
"output": "1\n"
},
{
"input": "100000000000 3 1 1\n",
"output": "1\n"
},
{
"input": "13 4 1 10\n",
"output": "3\n"
},
{
"input": "16 7 3 2\n",
"output": "4\n"
},
{
"input": "7 2 3 7\n",
"output": "3\n"
},
{
"input": "999 2 1 1\n",
"output": "1\n"
},
{
"input": "6 7 41 42\n",
"output": "41\n"
},
{
"input": "999999999999 2 1 1\n",
"output": "1\n"
},
{
"input": "7 3 100 10\n",
"output": "10\n"
},
{
"input": "500000000001 1000000000000 100 100\n",
"output": "49999999999900\n"
},
{
"input": "9 2 1 1\n",
"output": "1\n"
},
{
"input": "10000 3 1 1\n",
"output": "1\n"
},
{
"input": "1144108931 470211273 45 79\n",
"output": "11993619960\n"
},
{
"input": "804289377 846930887 78 16\n",
"output": "3326037780\n"
},
{
"input": "8 3 1 5\n",
"output": "1\n"
},
{
"input": "3205261341 718648876 58 11\n",
"output": "3637324207\n"
},
{
"input": "13 4 1 2\n",
"output": "2\n"
},
{
"input": "99999999999 2 1 1\n",
"output": "1\n"
},
{
"input": "100000000000 1 1 1\n",
"output": "0\n"
},
{
"input": "15 4 4 10\n",
"output": "4\n"
},
{
"input": "7 2 100 5\n",
"output": "5\n"
},
{
"input": "1 1 1 1\n",
"output": "0\n"
},
{
"input": "1000000000000 1 20 20\n",
"output": "0\n"
},
{
"input": "25 7 100 1\n",
"output": "4\n"
},
{
"input": "5 5 2 3\n",
"output": "0\n"
},
{
"input": "99999999999 6 100 100\n",
"output": "300\n"
},
{
"input": "20 3 3 3\n",
"output": "3\n"
},
{
"input": "18 7 100 1\n",
"output": "4\n"
},
{
"input": "100 3 1 3\n",
"output": "2\n"
},
{
"input": "1 2 5 1\n",
"output": "1\n"
},
{
"input": "10 12 2 1\n",
"output": "4\n"
},
{
"input": "3982258181 1589052704 87 20\n",
"output": "16083055460\n"
},
{
"input": "16 3 1 100\n",
"output": "2\n"
},
{
"input": "1000000000000 1 23 33\n",
"output": "0\n"
},
{
"input": "23 3 100 1\n",
"output": "2\n"
},
{
"input": "22 7 3 8\n",
"output": "8\n"
},
{
"input": "2 3 3 4\n",
"output": "3\n"
},
{
"input": "10 4 2 1\n",
"output": "2\n"
},
{
"input": "7 3 100 1\n",
"output": "1\n"
},
{
"input": "14 3 1 100\n",
"output": "1\n"
},
{
"input": "7 2 1 1\n",
"output": "1\n"
},
{
"input": "1 1 3 4\n",
"output": "0\n"
},
{
"input": "50 4 5 100\n",
"output": "10\n"
},
{
"input": "70 3 10 10\n",
"output": "10\n"
},
{
"input": "1000000000000 9 55 55\n",
"output": "55\n"
},
{
"input": "16 3 100 1\n",
"output": "1\n"
},
{
"input": "70 4 1 1\n",
"output": "2\n"
},
{
"input": "7 3 1 100\n",
"output": "2\n"
},
{
"input": "10003 4 1 100\n",
"output": "1\n"
},
{
"input": "100 3 2 5\n",
"output": "4\n"
},
{
"input": "100 33 100 1\n",
"output": "1\n"
},
{
"input": "9999999999 2 1 100\n",
"output": "1\n"
},
{
"input": "10 3 10 1\n",
"output": "1\n"
},
{
"input": "1000000000000 2 1 1\n",
"output": "0\n"
},
{
"input": "10 4 5 5\n",
"output": "10\n"
},
{
"input": "22 7 1 8\n",
"output": "6\n"
},
{
"input": "5 2 5 5\n",
"output": "5\n"
},
{
"input": "10000000000 1 1 1\n",
"output": "0\n"
},
{
"input": "999999999 2 1 1\n",
"output": "1\n"
},
{
"input": "999999999999 2 10 10\n",
"output": "10\n"
},
{
"input": "1000000000000 3 1 1\n",
"output": "1\n"
},
{
"input": "15 7 1 1\n",
"output": "1\n"
},
{
"input": "100 3 1 1\n",
"output": "1\n"
},
{
"input": "702 7 3 2\n",
"output": "4\n"
},
{
"input": "42 1 1 1\n",
"output": "0\n"
},
{
"input": "1000000000000 3 99 99\n",
"output": "99\n"
},
{
"input": "2 3 4 3\n",
"output": "4\n"
},
{
"input": "30 7 1 1\n",
"output": "2\n"
},
{
"input": "99999 2 1 1\n",
"output": "1\n"
},
{
"input": "90001 300 100 1\n",
"output": "1\n"
},
{
"input": "7 3 1 1\n",
"output": "1\n"
},
{
"input": "5 2 2 2\n",
"output": "2\n"
},
{
"input": "10 4 1 1\n",
"output": "2\n"
},
{
"input": "100 3 5 2\n",
"output": "2\n"
},
{
"input": "6 4 2 2\n",
"output": "4\n"
},
{
"input": "1 2 1 1\n",
"output": "1\n"
},
{
"input": "999999999999 2 100 100\n",
"output": "100\n"
},
{
"input": "15 7 1 5\n",
"output": "5\n"
},
{
"input": "100 3 3 8\n",
"output": "6\n"
},
{
"input": "17 4 5 5\n",
"output": "5\n"
},
{
"input": "1000000000000 3 100 100\n",
"output": "100\n"
},
{
"input": "100 34 1 100\n",
"output": "2\n"
},
{
"input": "1000000000000 6 1 3\n",
"output": "2\n"
},
{
"input": "957747787 424238336 87 93\n",
"output": "10162213695\n"
},
{
"input": "100 33 1 1\n",
"output": "1\n"
},
{
"input": "10000000001 2 1 1\n",
"output": "1\n"
},
{
"input": "99 19 1 7\n",
"output": "15\n"
},
{
"input": "3 2 5 2\n",
"output": "2\n"
},
{
"input": "100 9 1 2\n",
"output": "2\n"
},
{
"input": "1000000000000 750000000001 10 100\n",
"output": "5000000000020\n"
},
{
"input": "1 2 3 4\n",
"output": "3\n"
},
{
"input": "1 1000000000000 1 100\n",
"output": "100\n"
},
{
"input": "22 7 1 6\n",
"output": "6\n"
},
{
"input": "999999999999 10000000007 100 100\n",
"output": "70100\n"
},
{
"input": "100 7 1 1\n",
"output": "2\n"
},
{
"input": "29 5 4 9\n",
"output": "4\n"
},
{
"input": "17 4 2 1\n",
"output": "1\n"
},
{
"input": "1000000000000 7 3 8\n",
"output": "8\n"
},
{
"input": "10 3 1 2\n",
"output": "2\n"
},
{
"input": "7 2 1 2\n",
"output": "1\n"
},
{
"input": "1000000000000 750000000001 100 10\n",
"output": "2499999999990\n"
},
{
"input": "25 6 1 2\n",
"output": "2\n"
},
{
"input": "1000000000000 1 1 1\n",
"output": "0\n"
},
{
"input": "1000000000000 13 10 17\n",
"output": "17\n"
},
{
"input": "8 3 100 1\n",
"output": "2\n"
},
{
"input": "19 10 100 100\n",
"output": "100\n"
}
] | code_contests | python | 0.6 | b41c63d0530593de12ee41ff95142065 |
Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute — at the point 2 and so on).
There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).
There is also exactly one standing train which occupies all the points from l to r inclusive.
Vova can see the lantern at the point p if p is divisible by v and there is no standing train at this position (p not∈ [l; r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.
Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t different conferences, so you should answer t independent queries.
Input
The first line of the input contains one integer t (1 ≤ t ≤ 10^4) — the number of queries.
Then t lines follow. The i-th line contains four integers L_i, v_i, l_i, r_i (1 ≤ L, v ≤ 10^9, 1 ≤ l ≤ r ≤ L) — destination point of the i-th path, the period of the lantern appearance and the segment occupied by the standing train.
Output
Print t lines. The i-th line should contain one integer — the answer for the i-th query.
Example
Input
4
10 2 3 7
100 51 51 51
1234 1 100 199
1000000000 1 1 1000000000
Output
3
0
1134
0
Note
For the first example query, the answer is 3. There are lanterns at positions 2, 4, 6, 8 and 10, but Vova didn't see the lanterns at positions 4 and 6 because of the standing train.
For the second example query, the answer is 0 because the only lantern is at the point 51 and there is also a standing train at this point.
For the third example query, the answer is 1134 because there are 1234 lanterns, but Vova didn't see the lanterns from the position 100 to the position 199 inclusive.
For the fourth example query, the answer is 0 because the standing train covers the whole path.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` |
if __name__ == '__main__':
t = input()
t = int(t)
while t:
e, v, l, r = input().split()
e = int(e)
v = int(v)
l = int(l)
r = int(r)
res = e//v
if l % v != 0:
st = l//v + 1
else:
st = l//v
en = r//v
print(res-(en-st+1))
t=t-1
| python | code_algorithm | [
{
"input": "4\n10 2 3 7\n100 51 51 51\n1234 1 100 199\n1000000000 1 1 1000000000\n",
"output": "3\n0\n1134\n0\n"
},
{
"input": "1\n12599 3 1 2\n",
"output": "4199\n"
},
{
"input": "1\n2 5 1 1\n",
"output": "0\n"
},
{
"input": "10\n10 1 1 1\n10 1 1 2\n10 1 1 3\n10 1 1 4\n10 1 1 5\n10 1 1 6\n10 1 1 7\n10 1 1 8\n10 1 1 9\n10 1 1 10\n",
"output": "9\n8\n7\n6\n5\n4\n3\n2\n1\n0\n"
},
{
"input": "1\n100 2 61 62\n",
"output": "49\n"
},
{
"input": "1\n3 4 3 3\n",
"output": "0\n"
},
{
"input": "1\n10 1 9 10\n",
"output": "8\n"
},
{
"input": "10\n10 2 1 1\n10 2 1 2\n10 2 1 3\n10 2 1 4\n10 2 1 5\n10 2 1 6\n10 2 1 7\n10 2 1 8\n10 2 1 9\n10 2 1 10\n",
"output": "5\n4\n4\n3\n3\n2\n2\n1\n1\n0\n"
},
{
"input": "1\n8 5 1 5\n",
"output": "0\n"
},
{
"input": "3\n1 1 1 1\n2 3 1 2\n4 4 3 4\n",
"output": "0\n0\n0\n"
}
] | code_contests | python | 1 | 53c1c6091455ccf877bcb86a920a928f |
Let's write all the positive integer numbers one after another from 1 without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...
Your task is to print the k-th digit of this sequence.
Input
The first and only line contains integer k (1 ≤ k ≤ 10^{12}) — the position to process (1-based index).
Output
Print the k-th digit of the resulting infinite sequence.
Examples
Input
7
Output
7
Input
21
Output
5
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | #import sys
#digit = int(sys.argv[1])
digit = int(input())
if int(digit) <= 9:
print(digit)
exit()
start_range = 1
end_range = 9
power = 1
digit_count = 2
while not (start_range <= digit and digit <= end_range):
start_range = end_range + 1
end_range = 9 * 10**power * digit_count + start_range - 1
power += 1
digit_count += 1
offset_number = (digit - start_range) // (digit_count - 1)
#print(f"{digit} - {start_range} mod {digit_count-1} = {offset_number}")
number = str(10**(power - 1) + offset_number)
#print(f"10^ {power - 1} + {offset_number} = {number}")
offset_digit = (digit - start_range) % (digit_count - 1)
#print(f"{digit} - {start_range} mod {digit_count - 1 } = {offset_digit}")
#print(f"{number} {number[-offset_digit]}")
print(f"{number[offset_digit]}")
| python | code_algorithm | [
{
"input": "21\n",
"output": "5\n"
},
{
"input": "7\n",
"output": "7\n"
},
{
"input": "511\n",
"output": "2\n"
},
{
"input": "2879\n",
"output": "9\n"
},
{
"input": "4\n",
"output": "4\n"
},
{
"input": "2900\n",
"output": "0\n"
},
{
"input": "100000000083\n",
"output": "0\n"
},
{
"input": "417\n",
"output": "5\n"
},
{
"input": "68888890\n",
"output": "1\n"
},
{
"input": "9993\n",
"output": "5\n"
},
{
"input": "2889\n",
"output": "9\n"
},
{
"input": "9992\n",
"output": "7\n"
},
{
"input": "99999999996\n",
"output": "1\n"
},
{
"input": "6\n",
"output": "6\n"
},
{
"input": "300\n",
"output": "6\n"
},
{
"input": "8888888888\n",
"output": "9\n"
},
{
"input": "521\n",
"output": "1\n"
},
{
"input": "2883\n",
"output": "7\n"
},
{
"input": "99999999997\n",
"output": "0\n"
},
{
"input": "8888888893\n",
"output": "0\n"
},
{
"input": "196\n",
"output": "1\n"
},
{
"input": "125312355\n",
"output": "7\n"
},
{
"input": "2901\n",
"output": "2\n"
},
{
"input": "2880\n",
"output": "6\n"
},
{
"input": "523452345325\n",
"output": "8\n"
},
{
"input": "8000\n",
"output": "7\n"
},
{
"input": "2887\n",
"output": "9\n"
},
{
"input": "8888888891\n",
"output": "0\n"
},
{
"input": "8888888889\n",
"output": "9\n"
},
{
"input": "2882\n",
"output": "9\n"
},
{
"input": "2884\n",
"output": "9\n"
},
{
"input": "68888885\n",
"output": "9\n"
},
{
"input": "2899\n",
"output": "0\n"
},
{
"input": "4000\n",
"output": "7\n"
},
{
"input": "6000\n",
"output": "7\n"
},
{
"input": "190\n",
"output": "1\n"
},
{
"input": "2897\n",
"output": "1\n"
},
{
"input": "79437383\n",
"output": "5\n"
},
{
"input": "8\n",
"output": "8\n"
},
{
"input": "2878\n",
"output": "9\n"
},
{
"input": "100000\n",
"output": "2\n"
},
{
"input": "193\n",
"output": "1\n"
},
{
"input": "10000000000\n",
"output": "1\n"
},
{
"input": "8888888887\n",
"output": "9\n"
},
{
"input": "2893\n",
"output": "0\n"
},
{
"input": "68888887\n",
"output": "9\n"
},
{
"input": "9999\n",
"output": "7\n"
},
{
"input": "9\n",
"output": "9\n"
},
{
"input": "788888890\n",
"output": "1\n"
},
{
"input": "2898\n",
"output": "1\n"
},
{
"input": "1000000000000\n",
"output": "1\n"
},
{
"input": "999999999999\n",
"output": "9\n"
},
{
"input": "2888\n",
"output": "9\n"
},
{
"input": "9998\n",
"output": "2\n"
},
{
"input": "68888888\n",
"output": "9\n"
},
{
"input": "3\n",
"output": "3\n"
},
{
"input": "8888888892\n",
"output": "0\n"
},
{
"input": "9996\n",
"output": "7\n"
},
{
"input": "100000000004\n",
"output": "0\n"
},
{
"input": "9900\n",
"output": "5\n"
},
{
"input": "1000000000\n",
"output": "1\n"
},
{
"input": "99999999999\n",
"output": "0\n"
},
{
"input": "8888888890\n",
"output": "1\n"
},
{
"input": "2886\n",
"output": "8\n"
},
{
"input": "197\n",
"output": "0\n"
},
{
"input": "2891\n",
"output": "0\n"
},
{
"input": "9997\n",
"output": "6\n"
},
{
"input": "68888893\n",
"output": "0\n"
},
{
"input": "2896\n",
"output": "0\n"
},
{
"input": "9994\n",
"output": "2\n"
},
{
"input": "788888888\n",
"output": "9\n"
},
{
"input": "5000\n",
"output": "2\n"
},
{
"input": "68888892\n",
"output": "0\n"
},
{
"input": "68888889\n",
"output": "9\n"
},
{
"input": "9000\n",
"output": "2\n"
},
{
"input": "191\n",
"output": "0\n"
},
{
"input": "188\n",
"output": "9\n"
},
{
"input": "213412341\n",
"output": "6\n"
},
{
"input": "198\n",
"output": "2\n"
},
{
"input": "100000000\n",
"output": "8\n"
},
{
"input": "9991\n",
"output": "7\n"
},
{
"input": "10000000\n",
"output": "7\n"
},
{
"input": "99999999995\n",
"output": "0\n"
},
{
"input": "12\n",
"output": "1\n"
},
{
"input": "788888896\n",
"output": "0\n"
},
{
"input": "400\n",
"output": "1\n"
},
{
"input": "134613461346\n",
"output": "3\n"
},
{
"input": "788888889\n",
"output": "9\n"
},
{
"input": "2892\n",
"output": "0\n"
},
{
"input": "68888884\n",
"output": "9\n"
},
{
"input": "68888891\n",
"output": "0\n"
},
{
"input": "95863555435\n",
"output": "6\n"
},
{
"input": "9990\n",
"output": "2\n"
},
{
"input": "2890\n",
"output": "1\n"
},
{
"input": "199\n",
"output": "1\n"
},
{
"input": "5\n",
"output": "5\n"
},
{
"input": "200\n",
"output": "0\n"
},
{
"input": "7000\n",
"output": "2\n"
},
{
"input": "100000000000\n",
"output": "0\n"
},
{
"input": "1000000\n",
"output": "1\n"
},
{
"input": "10000\n",
"output": "7\n"
},
{
"input": "195\n",
"output": "1\n"
},
{
"input": "8888888894\n",
"output": "0\n"
},
{
"input": "9995\n",
"output": "7\n"
},
{
"input": "189\n",
"output": "9\n"
},
{
"input": "68888895\n",
"output": "0\n"
},
{
"input": "8888888895\n",
"output": "0\n"
},
{
"input": "2894\n",
"output": "1\n"
},
{
"input": "2881\n",
"output": "9\n"
},
{
"input": "192\n",
"output": "0\n"
},
{
"input": "10\n",
"output": "1\n"
},
{
"input": "3000\n",
"output": "2\n"
},
{
"input": "2895\n",
"output": "0\n"
},
{
"input": "194\n",
"output": "0\n"
},
{
"input": "1\n",
"output": "1\n"
},
{
"input": "2885\n",
"output": "9\n"
},
{
"input": "68888894\n",
"output": "0\n"
},
{
"input": "8888888896\n",
"output": "0\n"
},
{
"input": "99999999998\n",
"output": "1\n"
},
{
"input": "68888886\n",
"output": "9\n"
},
{
"input": "2\n",
"output": "2\n"
}
] | code_contests | python | 0.5 | a284c9fb345fbbe6506ae3385d123c0b |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6].
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from sys import stdin, gettrace
if not gettrace():
def input():
return next(stdin)[:-1]
def main():
n = int(input())
bb = [int(a) for a in input().split()]
seq = {}
for i, b in enumerate(bb):
if b - i in seq:
seq[b-i] += b
else:
seq[b-i] = b
print(max(seq.values()))
if __name__ == "__main__":
main() | python | code_algorithm | [
{
"input": "7\n8 9 26 11 12 29 14\n",
"output": "55\n"
},
{
"input": "6\n10 7 1 9 10 15\n",
"output": "26\n"
},
{
"input": "1\n400000\n",
"output": "400000\n"
},
{
"input": "5\n2 2 4 5 5\n",
"output": "11\n"
},
{
"input": "6\n1 3 3 5 6 8\n",
"output": "14\n"
},
{
"input": "5\n2 2 4 4 8\n",
"output": "8\n"
},
{
"input": "6\n3 2 5 6 8 10\n",
"output": "14\n"
},
{
"input": "5\n2 4 4 4 5\n",
"output": "9\n"
},
{
"input": "4\n1 3 3 5\n",
"output": "8\n"
},
{
"input": "6\n4 4 4 8 6 8\n",
"output": "12\n"
},
{
"input": "2\n2 2\n",
"output": "2\n"
},
{
"input": "9\n1 3 1 2 6 3 4 5 9\n",
"output": "12\n"
},
{
"input": "10\n10 60 12 13 14 15 16 66 18 130\n",
"output": "130\n"
},
{
"input": "6\n2 3 4 8 8 9\n",
"output": "17\n"
},
{
"input": "2\n399999 400000\n",
"output": "799999\n"
},
{
"input": "4\n1 2 4 4\n",
"output": "7\n"
},
{
"input": "5\n1 3 3 4 5\n",
"output": "13\n"
},
{
"input": "8\n1 1 2 2 3 3 4 4\n",
"output": "7\n"
},
{
"input": "6\n5 7 8 9 10 11\n",
"output": "45\n"
},
{
"input": "10\n1 1 1 1 1 1 1 1 1 3\n",
"output": "4\n"
},
{
"input": "5\n5 3 5 8 7\n",
"output": "13\n"
},
{
"input": "5\n2 3 3 8 5\n",
"output": "8\n"
},
{
"input": "5\n5 2 6 5 7\n",
"output": "7\n"
},
{
"input": "3\n1 4 5\n",
"output": "9\n"
},
{
"input": "8\n1 1 1 1 2 3 4 5\n",
"output": "15\n"
},
{
"input": "6\n1 2 3 4 5 7\n",
"output": "15\n"
},
{
"input": "4\n3 5 5 5\n",
"output": "8\n"
},
{
"input": "3\n3 6 5\n",
"output": "8\n"
},
{
"input": "6\n3 6 5 5 8 11\n",
"output": "11\n"
},
{
"input": "5\n4 6 5 5 8\n",
"output": "12\n"
},
{
"input": "6\n6 7 8 9 10 11\n",
"output": "51\n"
},
{
"input": "10\n1 1 1 1 1 5 6 7 8 9\n",
"output": "36\n"
},
{
"input": "6\n4 5 5 7 7 8\n",
"output": "20\n"
},
{
"input": "2\n1 2\n",
"output": "3\n"
},
{
"input": "3\n2 3 4\n",
"output": "9\n"
},
{
"input": "6\n6 2 5 8 9 10\n",
"output": "27\n"
},
{
"input": "6\n4 2 4 4 6 9\n",
"output": "13\n"
},
{
"input": "7\n1 1 2 3 4 5 6\n",
"output": "21\n"
},
{
"input": "5\n2 3 4 5 6\n",
"output": "20\n"
},
{
"input": "5\n4 3 5 5 5\n",
"output": "8\n"
},
{
"input": "6\n5 2 4 8 8 6\n",
"output": "13\n"
},
{
"input": "8\n1 2 3 4 5 7 8 9\n",
"output": "24\n"
},
{
"input": "3\n1 1 2\n",
"output": "3\n"
},
{
"input": "9\n1 1 1 5 5 5 9 9 9\n",
"output": "15\n"
},
{
"input": "4\n4 4 6 5\n",
"output": "10\n"
},
{
"input": "5\n1 3 4 5 8\n",
"output": "12\n"
},
{
"input": "5\n1 2 4 4 7\n",
"output": "7\n"
},
{
"input": "5\n1 2 4 5 6\n",
"output": "15\n"
},
{
"input": "5\n5 4 5 8 9\n",
"output": "22\n"
},
{
"input": "5\n3 5 6 5 7\n",
"output": "11\n"
},
{
"input": "6\n4 5 5 7 5 8\n",
"output": "16\n"
},
{
"input": "4\n2 3 4 5\n",
"output": "14\n"
},
{
"input": "3\n1 2 4\n",
"output": "4\n"
},
{
"input": "5\n2 2 3 5 5\n",
"output": "10\n"
},
{
"input": "3\n1 4 3\n",
"output": "4\n"
},
{
"input": "6\n3 6 5 8 7 10\n",
"output": "24\n"
},
{
"input": "6\n4 5 6 7 8 9\n",
"output": "39\n"
},
{
"input": "6\n1 2 4 6 5 6\n",
"output": "14\n"
},
{
"input": "6\n4 2 6 7 8 6\n",
"output": "25\n"
},
{
"input": "6\n1 3 5 5 7 7\n",
"output": "15\n"
},
{
"input": "2\n2 1\n",
"output": "2\n"
},
{
"input": "6\n3 4 4 5 7 6\n",
"output": "14\n"
},
{
"input": "9\n4 10 21 7 23 9 15 11 12\n",
"output": "44\n"
},
{
"input": "6\n2 6 4 6 7 9\n",
"output": "13\n"
},
{
"input": "5\n1 4 5 6 7\n",
"output": "22\n"
},
{
"input": "3\n1 2 3\n",
"output": "6\n"
},
{
"input": "6\n3 5 4 7 8 8\n",
"output": "20\n"
},
{
"input": "6\n2 2 4 4 6 7\n",
"output": "19\n"
},
{
"input": "5\n1 2 3 4 5\n",
"output": "15\n"
},
{
"input": "6\n2 7 4 4 10 11\n",
"output": "28\n"
},
{
"input": "6\n4 3 6 5 8 7\n",
"output": "18\n"
},
{
"input": "5\n2 1 2 3 4\n",
"output": "10\n"
},
{
"input": "4\n1 2 3 6\n",
"output": "6\n"
},
{
"input": "2\n1 1\n",
"output": "1\n"
},
{
"input": "4\n3 3 3 4\n",
"output": "7\n"
},
{
"input": "3\n3 3 4\n",
"output": "7\n"
},
{
"input": "4\n1 1 1 2\n",
"output": "3\n"
},
{
"input": "4\n3 5 4 6\n",
"output": "9\n"
},
{
"input": "6\n5 5 1 2 3 4\n",
"output": "10\n"
},
{
"input": "4\n2 2 3 6\n",
"output": "6\n"
},
{
"input": "6\n2 3 4 4 8 8\n",
"output": "9\n"
},
{
"input": "3\n2 2 4\n",
"output": "6\n"
},
{
"input": "5\n1 3 3 5 6\n",
"output": "14\n"
},
{
"input": "6\n6 4 5 6 6 7\n",
"output": "15\n"
},
{
"input": "5\n1 5 6 4 5\n",
"output": "11\n"
},
{
"input": "6\n2 5 6 5 9 8\n",
"output": "11\n"
},
{
"input": "4\n2 4 3 6\n",
"output": "10\n"
},
{
"input": "9\n18 11 24 7 18 10 1 20 17\n",
"output": "24\n"
},
{
"input": "6\n3 5 5 5 8 7\n",
"output": "13\n"
},
{
"input": "5\n4 4 5 4 7\n",
"output": "16\n"
},
{
"input": "6\n6 6 1 2 3 4\n",
"output": "10\n"
},
{
"input": "6\n1 2 3 4 5 6\n",
"output": "21\n"
},
{
"input": "7\n2 1 4 3 4 5 8\n",
"output": "14\n"
},
{
"input": "9\n1 3 1 2 6 2 3 4 9\n",
"output": "10\n"
},
{
"input": "6\n5 5 8 7 8 7\n",
"output": "20\n"
},
{
"input": "6\n1 3 4 5 5 7\n",
"output": "19\n"
},
{
"input": "4\n2 2 3 5\n",
"output": "7\n"
},
{
"input": "5\n4 5 4 7 8\n",
"output": "24\n"
},
{
"input": "4\n1 2 3 4\n",
"output": "10\n"
},
{
"input": "6\n3 5 5 7 5 8\n",
"output": "16\n"
},
{
"input": "4\n3 3 5 5\n",
"output": "8\n"
},
{
"input": "10\n1 1 1 1 1 1 1 1 3 1\n",
"output": "4\n"
},
{
"input": "5\n1 2 3 4 4\n",
"output": "10\n"
},
{
"input": "5\n5 1 2 3 4\n",
"output": "10\n"
},
{
"input": "5\n3 4 5 6 7\n",
"output": "25\n"
},
{
"input": "6\n3 4 3 8 7 8\n",
"output": "22\n"
}
] | code_contests | python | 0.9 | ea204f04276a982b3e7174f526e1aae9 |
Vasya is developing his own programming language VPL (Vasya Programming Language). Right now he is busy making the system of exceptions. He thinks that the system of exceptions must function like that.
The exceptions are processed by try-catch-blocks. There are two operators that work with the blocks:
1. The try operator. It opens a new try-catch-block.
2. The catch(<exception_type>, <message>) operator. It closes the try-catch-block that was started last and haven't yet been closed. This block can be activated only via exception of type <exception_type>. When we activate this block, the screen displays the <message>. If at the given moment there is no open try-catch-block, then we can't use the catch operator.
The exceptions can occur in the program in only one case: when we use the throw operator. The throw(<exception_type>) operator creates the exception of the given type.
Let's suggest that as a result of using some throw operator the program created an exception of type a. In this case a try-catch-block is activated, such that this block's try operator was described in the program earlier than the used throw operator. Also, this block's catch operator was given an exception type a as a parameter and this block's catch operator is described later that the used throw operator. If there are several such try-catch-blocks, then the system activates the block whose catch operator occurs earlier than others. If no try-catch-block was activated, then the screen displays message "Unhandled Exception".
To test the system, Vasya wrote a program that contains only try, catch and throw operators, one line contains no more than one operator, the whole program contains exactly one throw operator.
Your task is: given a program in VPL, determine, what message will be displayed on the screen.
Input
The first line contains a single integer: n (1 ≤ n ≤ 105) the number of lines in the program. Next n lines contain the program in language VPL. Each line contains no more than one operator. It means that input file can contain empty lines and lines, consisting only of spaces.
The program contains only operators try, catch and throw. It is guaranteed that the program is correct. It means that each started try-catch-block was closed, the catch operators aren't used unless there is an open try-catch-block. The program has exactly one throw operator. The program may have spaces at the beginning of a line, at the end of a line, before and after a bracket, a comma or a quote mark.
The exception type is a nonempty string, that consists only of upper and lower case english letters. The length of the string does not exceed 20 symbols. Message is a nonempty string, that consists only of upper and lower case english letters, digits and spaces. Message is surrounded with quote marks. Quote marks shouldn't be printed. The length of the string does not exceed 20 symbols.
Length of any line in the input file does not exceed 50 symbols.
Output
Print the message the screen will show after the given program is executed.
Examples
Input
8
try
try
throw ( AE )
catch ( BE, "BE in line 3")
try
catch(AE, "AE in line 5")
catch(AE,"AE somewhere")
Output
AE somewhere
Input
8
try
try
throw ( AE )
catch ( AE, "AE in line 3")
try
catch(BE, "BE in line 5")
catch(AE,"AE somewhere")
Output
AE in line 3
Input
8
try
try
throw ( CE )
catch ( BE, "BE in line 3")
try
catch(AE, "AE in line 5")
catch(AE,"AE somewhere")
Output
Unhandled Exception
Note
In the first sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(BE,"BE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so the second block will be activated, because operator catch(AE,"AE somewhere") has exception type AE as parameter and operator catch(BE,"BE in line 3") has exception type BE.
In the second sample there are 2 try-catch-blocks such that try operator is described earlier than throw operator and catch operator is described later than throw operator: try-catch(AE,"AE in line 3") and try-catch(AE,"AE somewhere"). Exception type is AE, so both blocks can be activated, but only the first one will be activated, because operator catch(AE,"AE in line 3") is described earlier than catch(AE,"AE somewhere")
In the third sample there is no blocks that can be activated by an exception of type CE.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
import math
MAXNUM = math.inf
MINNUM = -1 * math.inf
ASCIILOWER = 97
ASCIIUPPER = 65
def getInt():
return int(sys.stdin.readline().rstrip())
def getInts():
return map(int, sys.stdin.readline().rstrip().split(" "))
def getString():
return sys.stdin.readline().rstrip()
def printOutput(ans):
sys.stdout.write()
pass
def solve(parsedprog):
stack = []
for op in parsedprog:
# print(stack)
# print(op)
if op and op[0] in ("try", "throw"):
stack.append(op)
elif op and op[0] == "catch":
k = stack.pop()
if k[0] == "throw":
if op[1] == k[1]:
return op[2]
else:
stack.append(k)
return "Unhandled Exception"
def multisplit(string):
"""splits at a bracket, comma, quote, or text"""
"""does not split within quotes."""
parsedstring = []
curstring = []
insidequotes = False
for i in string:
if insidequotes:
if i == '"':
insidequotes = False
parsedstring.append("".join(curstring))
curstring = []
else:
curstring.append(i)
elif i in ("(", ")", ",", " "):
if curstring:
parsedstring.append("".join(curstring))
curstring = []
elif i == '"':
insidequotes = True
if curstring:
parsedstring.append("".join(curstring))
curstring = []
else:
curstring.append(i)
if curstring:
parsedstring.append("".join(curstring))
return parsedstring
def readinput():
proglen = getInt()
parsedprog = []
for _ in range(proglen):
parsedprog.append(multisplit(getString()))
print(solve(parsedprog))
readinput()
| python | code_algorithm | [
{
"input": "8\ntry\n try\n throw ( AE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n",
"output": "AE somewhere\n"
},
{
"input": "8\ntry\n try\n throw ( AE ) \n catch ( AE, \"AE in line 3\")\n\n try\n catch(BE, \"BE in line 5\") \ncatch(AE,\"AE somewhere\")\n",
"output": "AE in line 3\n"
},
{
"input": "8\ntry\n try\n throw ( CE ) \n catch ( BE, \"BE in line 3\")\n\n try\n catch(AE, \"AE in line 5\") \ncatch(AE,\"AE somewhere\")\n",
"output": "Unhandled Exception\n"
},
{
"input": "3\ntry\nthrow ( X )\ncatch ( X, \"try again\")\n",
"output": "try again\n"
},
{
"input": "3\ntry\nthrow ( try )\ncatch ( try, \"try again\")\n",
"output": "try again\n"
},
{
"input": "11\ntry\n try\n catch (B, \"b\")\n \n try\n throw ( U )\n catch (U, \"try\")\n \n try\n catch (C, \"c\")\ncatch (A, \"a\")\n",
"output": "try\n"
},
{
"input": "3\ntry\n throw(try)\ncatch(try,\"asd\")\n",
"output": "asd\n"
},
{
"input": "5\n try \n try \n catch ( gnAEZNTt, \"i5 tAC8ktUdeX\") \n throw( gnAEZNTt ) \ncatch ( gnAEZNTt, \"g1cN\" ) \n",
"output": "g1cN\n"
},
{
"input": "21\n try \n try \n try \n try \n try \n try \n try \n try \n try \n try \n throw( qtSMze) \ncatch(LY,\"x3 j\")\ncatch(hgSAFgbMGx,\"moByu\")\ncatch(LmydVQgv,\"hbZl\")\ncatch(oK,\"B6OZx qy\")\ncatch(rrtnRQB,\"7VFkQMv\")\ncatch(CASqQXaz,\"d9oci1Kx\")\ncatch(CTCzsdD,\"u\")\ncatch(xqqMxbEs,\"Mdu\")\ncatch(sOWgTPbRp,\"fVH6\")\ncatch(qtSMze,\"ZRnNzz\")\n",
"output": "ZRnNzz\n"
},
{
"input": "5\ntry \n throw( egdCZzrKRLBcqDl )\n catch ( egdCZzrKRLBcqDl ,\"o\" )\n try \n catch (egdCZzrKRLBcqDl , \"oM62EJIirV D0\" ) \n",
"output": "o\n"
},
{
"input": "5\n throw ( ouB ) \n try \ncatch(ouB, \"bTJZV\" )\n try \ncatch( ouB , \"DUniE dDhpiN\") \n",
"output": "Unhandled Exception\n"
},
{
"input": "3\ntry\nthrow(A)\ncatch(A, \"A cought\")\n",
"output": "A cought\n"
},
{
"input": "3\ntry\n throw(try)\ncatch(try,\"haha\")\n",
"output": "haha\n"
},
{
"input": "5\n try \n catch(UqWpIpGKiMqFnKox , \"bp9h8dfeNLhk9Wea\" ) \nthrow ( uaBRmgAAQyWTCzaaQMlZ ) \n try \ncatch( UqWpIpGKiMqFnKox,\"0OvVhsVWzDyqwo\" )\n",
"output": "Unhandled Exception\n"
},
{
"input": "3\ntry\nthrow(tryC)\ncatch(tryC, \"bad boy\")\n",
"output": "bad boy\n"
},
{
"input": "7\ntry\ncatch(A,\"try A\")\ntry\n throw(A)\ncatch(A,\"try B\")\ntry\ncatch(A,\"try C\")\n",
"output": "try B\n"
},
{
"input": "10\n \n\n \n\nthrow (ProgramException)\n \n \n\n\n \n",
"output": "Unhandled Exception\n"
}
] | code_contests | python | 0 | 8b9ef6383338f0be9ca6e44b58a5dd43 |
Manao has invented a new mathematical term — a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions:
1. The coordinates of each point in the set are integers.
2. For any two points from the set, the distance between them is a non-integer.
Consider all points (x, y) which satisfy the inequations: 0 ≤ x ≤ n; 0 ≤ y ≤ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points.
Input
The single line contains two space-separated integers n and m (1 ≤ n, m ≤ 100).
Output
In the first line print a single integer — the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers — the x- and y- coordinates, respectively, of a point from the set.
If there are several optimal solutions, you may print any of them.
Examples
Input
2 2
Output
3
0 1
1 2
2 0
Input
4 3
Output
4
0 3
2 1
3 0
4 2
Note
Consider the first sample. The distance between points (0, 1) and (1, 2) equals <image>, between (0, 1) and (2, 0) — <image>, between (1, 2) and (2, 0) — <image>. Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | if __name__ == '__main__':
nums = input().split()
n = int(nums[0])
m = int(nums[1])
k = min(m, n) + 1
print(k)
for i in range(k):
print(str(i) + " " + str(k-1-i)) | python | code_algorithm | [
{
"input": "2 2\n",
"output": "3\n0 2\n1 1\n2 0\n"
},
{
"input": "4 3\n",
"output": "4\n0 3\n1 2\n2 1\n3 0\n"
},
{
"input": "9 6\n",
"output": "7\n0 6\n1 5\n2 4\n3 3\n4 2\n5 1\n6 0\n"
},
{
"input": "3 4\n",
"output": "4\n0 3\n1 2\n2 1\n3 0\n"
},
{
"input": "6 6\n",
"output": "7\n0 6\n1 5\n2 4\n3 3\n4 2\n5 1\n6 0\n"
},
{
"input": "85 48\n",
"output": "49\n0 48\n1 47\n2 46\n3 45\n4 44\n5 43\n6 42\n7 41\n8 40\n9 39\n10 38\n11 37\n12 36\n13 35\n14 34\n15 33\n16 32\n17 31\n18 30\n19 29\n20 28\n21 27\n22 26\n23 25\n24 24\n25 23\n26 22\n27 21\n28 20\n29 19\n30 18\n31 17\n32 16\n33 15\n34 14\n35 13\n36 12\n37 11\n38 10\n39 9\n40 8\n41 7\n42 6\n43 5\n44 4\n45 3\n46 2\n47 1\n48 0\n"
},
{
"input": "1 5\n",
"output": "2\n0 1\n1 0\n"
},
{
"input": "30 40\n",
"output": "31\n0 30\n1 29\n2 28\n3 27\n4 26\n5 25\n6 24\n7 23\n8 22\n9 21\n10 20\n11 19\n12 18\n13 17\n14 16\n15 15\n16 14\n17 13\n18 12\n19 11\n20 10\n21 9\n22 8\n23 7\n24 6\n25 5\n26 4\n27 3\n28 2\n29 1\n30 0\n"
},
{
"input": "88 88\n",
"output": "89\n0 88\n1 87\n2 86\n3 85\n4 84\n5 83\n6 82\n7 81\n8 80\n9 79\n10 78\n11 77\n12 76\n13 75\n14 74\n15 73\n16 72\n17 71\n18 70\n19 69\n20 68\n21 67\n22 66\n23 65\n24 64\n25 63\n26 62\n27 61\n28 60\n29 59\n30 58\n31 57\n32 56\n33 55\n34 54\n35 53\n36 52\n37 51\n38 50\n39 49\n40 48\n41 47\n42 46\n43 45\n44 44\n45 43\n46 42\n47 41\n48 40\n49 39\n50 38\n51 37\n52 36\n53 35\n54 34\n55 33\n56 32\n57 31\n58 30\n59 29\n60 28\n61 27\n62 26\n63 25\n64 24\n65 23\n66 22\n67 21\n68 20\n69 19\n70 18\n71 17\n72 16\n73 15\n74 14\n75 13\n76 12\n77 11\n78 10\n79 9\n80 8\n81 7\n82 6\n83 5\n84 4\n85 3\n86 2\n87 1\n88 0\n"
},
{
"input": "100 11\n",
"output": "12\n0 11\n1 10\n2 9\n3 8\n4 7\n5 6\n6 5\n7 4\n8 3\n9 2\n10 1\n11 0\n"
},
{
"input": "10 6\n",
"output": "7\n0 6\n1 5\n2 4\n3 3\n4 2\n5 1\n6 0\n"
},
{
"input": "6 4\n",
"output": "5\n0 4\n1 3\n2 2\n3 1\n4 0\n"
},
{
"input": "21 21\n",
"output": "22\n0 21\n1 20\n2 19\n3 18\n4 17\n5 16\n6 15\n7 14\n8 13\n9 12\n10 11\n11 10\n12 9\n13 8\n14 7\n15 6\n16 5\n17 4\n18 3\n19 2\n20 1\n21 0\n"
},
{
"input": "10 100\n",
"output": "11\n0 10\n1 9\n2 8\n3 7\n4 6\n5 5\n6 4\n7 3\n8 2\n9 1\n10 0\n"
},
{
"input": "80 91\n",
"output": "81\n0 80\n1 79\n2 78\n3 77\n4 76\n5 75\n6 74\n7 73\n8 72\n9 71\n10 70\n11 69\n12 68\n13 67\n14 66\n15 65\n16 64\n17 63\n18 62\n19 61\n20 60\n21 59\n22 58\n23 57\n24 56\n25 55\n26 54\n27 53\n28 52\n29 51\n30 50\n31 49\n32 48\n33 47\n34 46\n35 45\n36 44\n37 43\n38 42\n39 41\n40 40\n41 39\n42 38\n43 37\n44 36\n45 35\n46 34\n47 33\n48 32\n49 31\n50 30\n51 29\n52 28\n53 27\n54 26\n55 25\n56 24\n57 23\n58 22\n59 21\n60 20\n61 19\n62 18\n63 17\n64 16\n65 15\n66 14\n67 13\n68 12\n69 11\n70 10\n71 9\n72 8\n73 7\n74 6\n75 5\n76 4\n77 3\n78 2\n79 1\n80 0\n"
},
{
"input": "77 77\n",
"output": "78\n0 77\n1 76\n2 75\n3 74\n4 73\n5 72\n6 71\n7 70\n8 69\n9 68\n10 67\n11 66\n12 65\n13 64\n14 63\n15 62\n16 61\n17 60\n18 59\n19 58\n20 57\n21 56\n22 55\n23 54\n24 53\n25 52\n26 51\n27 50\n28 49\n29 48\n30 47\n31 46\n32 45\n33 44\n34 43\n35 42\n36 41\n37 40\n38 39\n39 38\n40 37\n41 36\n42 35\n43 34\n44 33\n45 32\n46 31\n47 30\n48 29\n49 28\n50 27\n51 26\n52 25\n53 24\n54 23\n55 22\n56 21\n57 20\n58 19\n59 18\n60 17\n61 16\n62 15\n63 14\n64 13\n65 12\n66 11\n67 10\n68 9\n69 8\n70 7\n71 6\n72 5\n73 4\n74 3\n75 2\n76 1\n77 0\n"
},
{
"input": "96 96\n",
"output": "97\n0 96\n1 95\n2 94\n3 93\n4 92\n5 91\n6 90\n7 89\n8 88\n9 87\n10 86\n11 85\n12 84\n13 83\n14 82\n15 81\n16 80\n17 79\n18 78\n19 77\n20 76\n21 75\n22 74\n23 73\n24 72\n25 71\n26 70\n27 69\n28 68\n29 67\n30 66\n31 65\n32 64\n33 63\n34 62\n35 61\n36 60\n37 59\n38 58\n39 57\n40 56\n41 55\n42 54\n43 53\n44 52\n45 51\n46 50\n47 49\n48 48\n49 47\n50 46\n51 45\n52 44\n53 43\n54 42\n55 41\n56 40\n57 39\n58 38\n59 37\n60 36\n61 35\n62 34\n63 33\n64 32\n65 31\n66 30\n67 29\n68 28\n69 27\n70 26\n71 25\n72 24\n73 23\n74 22\n75 21\n76 20\n77 19\n78 18\n79 17\n80 16\n81 15\n82 14\n83 13\n84 12\n85 11\n86 10\n87 9\n88 8\n89 7\n90 6\n91 5\n92 4\n93 3\n94 2\n95 1\n96 0\n"
},
{
"input": "5 100\n",
"output": "6\n0 5\n1 4\n2 3\n3 2\n4 1\n5 0\n"
},
{
"input": "3 19\n",
"output": "4\n0 3\n1 2\n2 1\n3 0\n"
},
{
"input": "5 5\n",
"output": "6\n0 5\n1 4\n2 3\n3 2\n4 1\n5 0\n"
},
{
"input": "16 55\n",
"output": "17\n0 16\n1 15\n2 14\n3 13\n4 12\n5 11\n6 10\n7 9\n8 8\n9 7\n10 6\n11 5\n12 4\n13 3\n14 2\n15 1\n16 0\n"
},
{
"input": "2 10\n",
"output": "3\n0 2\n1 1\n2 0\n"
},
{
"input": "13 13\n",
"output": "14\n0 13\n1 12\n2 11\n3 10\n4 9\n5 8\n6 7\n7 6\n8 5\n9 4\n10 3\n11 2\n12 1\n13 0\n"
},
{
"input": "67 59\n",
"output": "60\n0 59\n1 58\n2 57\n3 56\n4 55\n5 54\n6 53\n7 52\n8 51\n9 50\n10 49\n11 48\n12 47\n13 46\n14 45\n15 44\n16 43\n17 42\n18 41\n19 40\n20 39\n21 38\n22 37\n23 36\n24 35\n25 34\n26 33\n27 32\n28 31\n29 30\n30 29\n31 28\n32 27\n33 26\n34 25\n35 24\n36 23\n37 22\n38 21\n39 20\n40 19\n41 18\n42 17\n43 16\n44 15\n45 14\n46 13\n47 12\n48 11\n49 10\n50 9\n51 8\n52 7\n53 6\n54 5\n55 4\n56 3\n57 2\n58 1\n59 0\n"
},
{
"input": "1 4\n",
"output": "2\n0 1\n1 0\n"
},
{
"input": "3 7\n",
"output": "4\n0 3\n1 2\n2 1\n3 0\n"
},
{
"input": "4 6\n",
"output": "5\n0 4\n1 3\n2 2\n3 1\n4 0\n"
},
{
"input": "10 10\n",
"output": "11\n0 10\n1 9\n2 8\n3 7\n4 6\n5 5\n6 4\n7 3\n8 2\n9 1\n10 0\n"
},
{
"input": "98 76\n",
"output": "77\n0 76\n1 75\n2 74\n3 73\n4 72\n5 71\n6 70\n7 69\n8 68\n9 67\n10 66\n11 65\n12 64\n13 63\n14 62\n15 61\n16 60\n17 59\n18 58\n19 57\n20 56\n21 55\n22 54\n23 53\n24 52\n25 51\n26 50\n27 49\n28 48\n29 47\n30 46\n31 45\n32 44\n33 43\n34 42\n35 41\n36 40\n37 39\n38 38\n39 37\n40 36\n41 35\n42 34\n43 33\n44 32\n45 31\n46 30\n47 29\n48 28\n49 27\n50 26\n51 25\n52 24\n53 23\n54 22\n55 21\n56 20\n57 19\n58 18\n59 17\n60 16\n61 15\n62 14\n63 13\n64 12\n65 11\n66 10\n67 9\n68 8\n69 7\n70 6\n71 5\n72 4\n73 3\n74 2\n75 1\n76 0\n"
},
{
"input": "4 4\n",
"output": "5\n0 4\n1 3\n2 2\n3 1\n4 0\n"
},
{
"input": "1 1\n",
"output": "2\n0 1\n1 0\n"
},
{
"input": "67 58\n",
"output": "59\n0 58\n1 57\n2 56\n3 55\n4 54\n5 53\n6 52\n7 51\n8 50\n9 49\n10 48\n11 47\n12 46\n13 45\n14 44\n15 43\n16 42\n17 41\n18 40\n19 39\n20 38\n21 37\n22 36\n23 35\n24 34\n25 33\n26 32\n27 31\n28 30\n29 29\n30 28\n31 27\n32 26\n33 25\n34 24\n35 23\n36 22\n37 21\n38 20\n39 19\n40 18\n41 17\n42 16\n43 15\n44 14\n45 13\n46 12\n47 11\n48 10\n49 9\n50 8\n51 7\n52 6\n53 5\n54 4\n55 3\n56 2\n57 1\n58 0\n"
},
{
"input": "99 100\n",
"output": "100\n0 99\n1 98\n2 97\n3 96\n4 95\n5 94\n6 93\n7 92\n8 91\n9 90\n10 89\n11 88\n12 87\n13 86\n14 85\n15 84\n16 83\n17 82\n18 81\n19 80\n20 79\n21 78\n22 77\n23 76\n24 75\n25 74\n26 73\n27 72\n28 71\n29 70\n30 69\n31 68\n32 67\n33 66\n34 65\n35 64\n36 63\n37 62\n38 61\n39 60\n40 59\n41 58\n42 57\n43 56\n44 55\n45 54\n46 53\n47 52\n48 51\n49 50\n50 49\n51 48\n52 47\n53 46\n54 45\n55 44\n56 43\n57 42\n58 41\n59 40\n60 39\n61 38\n62 37\n63 36\n64 35\n65 34\n66 33\n67 32\n68 31\n69 30\n70 29\n71 28\n72 27\n73 26\n74 25\n75 24\n76 23\n77 22\n78 21\n79 20\n80 19\n81 18\n82 17\n83 16\n84 15\n85 14\n86 13\n87 12\n88 11\n89 10\n90 9\n91 8\n92 7\n93 6\n94 5\n95 4\n96 3\n97 2\n98 1\n99 0\n"
},
{
"input": "7 6\n",
"output": "7\n0 6\n1 5\n2 4\n3 3\n4 2\n5 1\n6 0\n"
},
{
"input": "95 99\n",
"output": "96\n0 95\n1 94\n2 93\n3 92\n4 91\n5 90\n6 89\n7 88\n8 87\n9 86\n10 85\n11 84\n12 83\n13 82\n14 81\n15 80\n16 79\n17 78\n18 77\n19 76\n20 75\n21 74\n22 73\n23 72\n24 71\n25 70\n26 69\n27 68\n28 67\n29 66\n30 65\n31 64\n32 63\n33 62\n34 61\n35 60\n36 59\n37 58\n38 57\n39 56\n40 55\n41 54\n42 53\n43 52\n44 51\n45 50\n46 49\n47 48\n48 47\n49 46\n50 45\n51 44\n52 43\n53 42\n54 41\n55 40\n56 39\n57 38\n58 37\n59 36\n60 35\n61 34\n62 33\n63 32\n64 31\n65 30\n66 29\n67 28\n68 27\n69 26\n70 25\n71 24\n72 23\n73 22\n74 21\n75 20\n76 19\n77 18\n78 17\n79 16\n80 15\n81 14\n82 13\n83 12\n84 11\n85 10\n86 9\n87 8\n88 7\n89 6\n90 5\n91 4\n92 3\n93 2\n94 1\n95 0\n"
},
{
"input": "100 100\n",
"output": "101\n0 100\n1 99\n2 98\n3 97\n4 96\n5 95\n6 94\n7 93\n8 92\n9 91\n10 90\n11 89\n12 88\n13 87\n14 86\n15 85\n16 84\n17 83\n18 82\n19 81\n20 80\n21 79\n22 78\n23 77\n24 76\n25 75\n26 74\n27 73\n28 72\n29 71\n30 70\n31 69\n32 68\n33 67\n34 66\n35 65\n36 64\n37 63\n38 62\n39 61\n40 60\n41 59\n42 58\n43 57\n44 56\n45 55\n46 54\n47 53\n48 52\n49 51\n50 50\n51 49\n52 48\n53 47\n54 46\n55 45\n56 44\n57 43\n58 42\n59 41\n60 40\n61 39\n62 38\n63 37\n64 36\n65 35\n66 34\n67 33\n68 32\n69 31\n70 30\n71 29\n72 28\n73 27\n74 26\n75 25\n76 24\n77 23\n78 22\n79 21\n80 20\n81 19\n82 18\n83 17\n84 16\n85 15\n86 14\n87 13\n88 12\n89 11\n90 10\n91 9\n92 8\n93 7\n94 6\n95 5\n96 4\n97 3\n98 2\n99 1\n100 0\n"
},
{
"input": "13 18\n",
"output": "14\n0 13\n1 12\n2 11\n3 10\n4 9\n5 8\n6 7\n7 6\n8 5\n9 4\n10 3\n11 2\n12 1\n13 0\n"
},
{
"input": "93 70\n",
"output": "71\n0 70\n1 69\n2 68\n3 67\n4 66\n5 65\n6 64\n7 63\n8 62\n9 61\n10 60\n11 59\n12 58\n13 57\n14 56\n15 55\n16 54\n17 53\n18 52\n19 51\n20 50\n21 49\n22 48\n23 47\n24 46\n25 45\n26 44\n27 43\n28 42\n29 41\n30 40\n31 39\n32 38\n33 37\n34 36\n35 35\n36 34\n37 33\n38 32\n39 31\n40 30\n41 29\n42 28\n43 27\n44 26\n45 25\n46 24\n47 23\n48 22\n49 21\n50 20\n51 19\n52 18\n53 17\n54 16\n55 15\n56 14\n57 13\n58 12\n59 11\n60 10\n61 9\n62 8\n63 7\n64 6\n65 5\n66 4\n67 3\n68 2\n69 1\n70 0\n"
},
{
"input": "10 1\n",
"output": "2\n0 1\n1 0\n"
},
{
"input": "2 6\n",
"output": "3\n0 2\n1 1\n2 0\n"
},
{
"input": "6 7\n",
"output": "7\n0 6\n1 5\n2 4\n3 3\n4 2\n5 1\n6 0\n"
},
{
"input": "16 5\n",
"output": "6\n0 5\n1 4\n2 3\n3 2\n4 1\n5 0\n"
},
{
"input": "13 71\n",
"output": "14\n0 13\n1 12\n2 11\n3 10\n4 9\n5 8\n6 7\n7 6\n8 5\n9 4\n10 3\n11 2\n12 1\n13 0\n"
},
{
"input": "11 9\n",
"output": "10\n0 9\n1 8\n2 7\n3 6\n4 5\n5 4\n6 3\n7 2\n8 1\n9 0\n"
},
{
"input": "37 42\n",
"output": "38\n0 37\n1 36\n2 35\n3 34\n4 33\n5 32\n6 31\n7 30\n8 29\n9 28\n10 27\n11 26\n12 25\n13 24\n14 23\n15 22\n16 21\n17 20\n18 19\n19 18\n20 17\n21 16\n22 15\n23 14\n24 13\n25 12\n26 11\n27 10\n28 9\n29 8\n30 7\n31 6\n32 5\n33 4\n34 3\n35 2\n36 1\n37 0\n"
},
{
"input": "28 28\n",
"output": "29\n0 28\n1 27\n2 26\n3 25\n4 24\n5 23\n6 22\n7 21\n8 20\n9 19\n10 18\n11 17\n12 16\n13 15\n14 14\n15 13\n16 12\n17 11\n18 10\n19 9\n20 8\n21 7\n22 6\n23 5\n24 4\n25 3\n26 2\n27 1\n28 0\n"
},
{
"input": "6 10\n",
"output": "7\n0 6\n1 5\n2 4\n3 3\n4 2\n5 1\n6 0\n"
}
] | code_contests | python | 0 | 38d2520a817904d8585b39597948e598 |
Recently, the bear started studying data structures and faced the following problem.
You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: <image>, where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).
Help the bear cope with the problem.
Input
The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct.
The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.
Output
Print m integers — the answers to the queries on the order the queries appear in the input.
Examples
Input
6
5 5 7 10 14 15
3
2 11
3 12
4 4
Output
9
7
0
Input
7
2 3 5 7 11 4 8
2
8 10
2 123
Output
0
7
Note
Consider the first sample. Overall, the first sample has 3 queries.
1. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9.
2. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7.
3. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math, sys
input = sys.stdin.buffer.readline
def ints():
return map(int, input().split())
n = int(input())
x = list(ints())
MAX = max(x) + 1
freq = [0] * MAX
for i in x:
freq[i] += 1
sieve = [False] * MAX
f = [0] * MAX
for i in range(2, MAX):
if sieve[i]:
continue
for j in range(i, MAX, i):
sieve[j] = True
f[i] += freq[j]
for i in range(2, MAX):
f[i] += f[i-1]
m = int(input())
for i in range(m):
l, r = ints()
if l >= MAX:
print(0)
elif r >= MAX:
print(f[-1] - f[l-1])
else:
print(f[r] - f[l-1]) | python | code_algorithm | [
{
"input": "7\n2 3 5 7 11 4 8\n2\n8 10\n2 123\n",
"output": "0\n7\n"
},
{
"input": "6\n5 5 7 10 14 15\n3\n2 11\n3 12\n4 4\n",
"output": "9\n7\n0\n"
},
{
"input": "1\n6\n1\n2 3\n",
"output": "2\n"
},
{
"input": "9\n9999991 9999943 9999883 4658161 4657997 2315407 2315263 1000003 1000033\n13\n9999991 9999991\n9999943 9999943\n9999883 9999883\n4658161 4658161\n4657997 4657997\n2315407 2315407\n2315263 2315263\n1000003 1000003\n1000033 1000033\n2 2000000000\n2000000000 2000000000\n9999992 2000000000\n1000033 9999990\n",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n9\n0\n0\n7\n"
},
{
"input": "1\n10000000\n1\n2000000000 2000000000\n",
"output": "0\n"
},
{
"input": "9\n50 50 50 50 50 50 50 50 50\n7\n20 20\n8 13\n13 13\n6 14\n3 5\n15 17\n341 1792\n",
"output": "0\n0\n0\n0\n9\n0\n0\n"
},
{
"input": "12\n2 4 8 16 32 64 128 256 512 1024 2048 4096\n14\n2 2\n2 2000000000\n4 4\n8 8\n16 16\n32 32\n64 64\n128 128\n256 256\n512 512\n1024 1024\n2048 2048\n4096 4096\n3 2000000000\n",
"output": "12\n12\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n"
}
] | code_contests | python | 0 | a138602c74834b1e93e368ea5659c75a |
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?
Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
Input
The single line contains three integers r, g and b (0 ≤ r, g, b ≤ 2·109) — the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
Output
Print a single integer t — the maximum number of tables that can be decorated in the required manner.
Examples
Input
5 4 3
Output
4
Input
1 1 1
Output
1
Input
2 3 3
Output
2
Note
In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | a = sorted(list(map(int, input().split())))
a[2] = min(a[2], 2 * (a[0] + a[1]))
print(sum(a) // 3)
| python | code_algorithm | [
{
"input": "1 1 1\n",
"output": "1\n"
},
{
"input": "5 4 3\n",
"output": "4\n"
},
{
"input": "2 3 3\n",
"output": "2\n"
},
{
"input": "3 5 2\n",
"output": "3\n"
},
{
"input": "0 1 0\n",
"output": "0\n"
},
{
"input": "500000002 2000000000 500000001\n",
"output": "1000000001\n"
},
{
"input": "0 0 0\n",
"output": "0\n"
},
{
"input": "1 1 9\n",
"output": "2\n"
},
{
"input": "500000000 1000000000 500000000\n",
"output": "666666666\n"
},
{
"input": "2000000000 1999999999 1999999999\n",
"output": "1999999999\n"
},
{
"input": "1000000000 1000000000 1000000000\n",
"output": "1000000000\n"
},
{
"input": "1000000000 2000000000 1000000000\n",
"output": "1333333333\n"
},
{
"input": "3 3 6\n",
"output": "4\n"
},
{
"input": "0 3 3\n",
"output": "2\n"
},
{
"input": "100 99 56\n",
"output": "85\n"
},
{
"input": "10000 7500 7500\n",
"output": "8333\n"
},
{
"input": "45 33 76\n",
"output": "51\n"
},
{
"input": "99 5747 5298\n",
"output": "3714\n"
},
{
"input": "7511 7512 7513\n",
"output": "7512\n"
},
{
"input": "500000000 2000000000 500000000\n",
"output": "1000000000\n"
},
{
"input": "82728372 939848 100139442\n",
"output": "61269220\n"
},
{
"input": "1234567890 123456789 987654321\n",
"output": "781893000\n"
},
{
"input": "999288131 55884921 109298382\n",
"output": "165183303\n"
},
{
"input": "1999999999 500000000 500000000\n",
"output": "999999999\n"
},
{
"input": "520000000 1000000033 501000000\n",
"output": "673666677\n"
},
{
"input": "150000 75000 75000\n",
"output": "100000\n"
},
{
"input": "1938 8999 1882\n",
"output": "3820\n"
},
{
"input": "1585222789 1889821127 2000000000\n",
"output": "1825014638\n"
},
{
"input": "6 1 1\n",
"output": "2\n"
},
{
"input": "100500 100500 3\n",
"output": "67001\n"
},
{
"input": "1463615122 1988383731 837331500\n",
"output": "1429776784\n"
},
{
"input": "1 2000000000 1000000000\n",
"output": "1000000000\n"
},
{
"input": "0 1 1000000000\n",
"output": "1\n"
},
{
"input": "3 0 0\n",
"output": "0\n"
},
{
"input": "4 0 4\n",
"output": "2\n"
},
{
"input": "10000 1000 100000\n",
"output": "11000\n"
},
{
"input": "2000000000 500000000 499999999\n",
"output": "999999999\n"
},
{
"input": "3 4 9\n",
"output": "5\n"
},
{
"input": "2000000000 2000000000 2000000000\n",
"output": "2000000000\n"
},
{
"input": "100000 1 2\n",
"output": "3\n"
},
{
"input": "198488 50 18\n",
"output": "68\n"
},
{
"input": "1000 1000 1002\n",
"output": "1000\n"
}
] | code_contests | python | 0.2 | 474ffdedd50cf8a8540b9376220a99ce |
Celebrating the new year, many people post videos of falling dominoes; Here's a list of them: https://www.youtube.com/results?search_query=New+Years+Dominos
User ainta, who lives in a 2D world, is going to post a video as well.
There are n dominoes on a 2D Cartesian plane. i-th domino (1 ≤ i ≤ n) can be represented as a line segment which is parallel to the y-axis and whose length is li. The lower point of the domino is on the x-axis. Let's denote the x-coordinate of the i-th domino as pi. Dominoes are placed one after another, so p1 < p2 < ... < pn - 1 < pn holds.
User ainta wants to take a video of falling dominoes. To make dominoes fall, he can push a single domino to the right. Then, the domino will fall down drawing a circle-shaped orbit until the line segment totally overlaps with the x-axis.
<image>
Also, if the s-th domino touches the t-th domino while falling down, the t-th domino will also fall down towards the right, following the same procedure above. Domino s touches domino t if and only if the segment representing s and t intersects.
<image>
See the picture above. If he pushes the leftmost domino to the right, it falls down, touching dominoes (A), (B) and (C). As a result, dominoes (A), (B), (C) will also fall towards the right. However, domino (D) won't be affected by pushing the leftmost domino, but eventually it will fall because it is touched by domino (C) for the first time.
<image>
The picture above is an example of falling dominoes. Each red circle denotes a touch of two dominoes.
User ainta has q plans of posting the video. j-th of them starts with pushing the xj-th domino, and lasts until the yj-th domino falls. But sometimes, it could be impossible to achieve such plan, so he has to lengthen some dominoes. It costs one dollar to increase the length of a single domino by 1. User ainta wants to know, for each plan, the minimum cost needed to achieve it. Plans are processed independently, i. e. if domino's length is increased in some plan, it doesn't affect its length in other plans. Set of dominos that will fall except xj-th domino and yj-th domino doesn't matter, but the initial push should be on domino xj.
Input
The first line contains an integer n (2 ≤ n ≤ 2 × 105)— the number of dominoes.
Next n lines describe the dominoes. The i-th line (1 ≤ i ≤ n) contains two space-separated integers pi, li (1 ≤ pi, li ≤ 109)— the x-coordinate and the length of the i-th domino. It is guaranteed that p1 < p2 < ... < pn - 1 < pn.
The next line contains an integer q (1 ≤ q ≤ 2 × 105) — the number of plans.
Next q lines describe the plans. The j-th line (1 ≤ j ≤ q) contains two space-separated integers xj, yj (1 ≤ xj < yj ≤ n). It means the j-th plan is, to push the xj-th domino, and shoot a video until the yj-th domino falls.
Output
For each plan, print a line containing the minimum cost needed to achieve it. If no cost is needed, print 0.
Examples
Input
6
1 5
3 3
4 4
9 2
10 1
12 1
4
1 2
2 4
2 5
2 6
Output
0
1
1
2
Note
Consider the example. The dominoes are set like the picture below.
<image>
Let's take a look at the 4th plan. To make the 6th domino fall by pushing the 2nd domino, the length of the 3rd domino (whose x-coordinate is 4) should be increased by 1, and the 5th domino (whose x-coordinate is 9) should be increased by 1 (other option is to increase 4th domino instead of 5th also by 1). Then, the dominoes will fall like in the picture below. Each cross denotes a touch between two dominoes.
<image> <image> <image> <image> <image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | '''
from bisect import bisect,bisect_left
from collections import *
from math import gcd,ceil,sqrt,floor,inf
from heapq import *
from itertools import *
from operator import add,mul,sub,xor,truediv,floordiv
from functools import *'''
#------------------------------------------------------------------------
import os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#------------------------------------------------------------------------
def RL(): return map(int, sys.stdin.readline().split())
def RLL(): return list(map(int, sys.stdin.readline().split()))
def N(): return int(input())
def A(n):return [0]*n
def AI(n,x): return [x]*n
def A2(n,m): return [[0]*m for i in range(n)]
def G(n): return [[] for i in range(n)]
def GP(it): return [[ch,len(list(g))] for ch,g in groupby(it)]
#------------------------------------------------------------------------
from types import GeneratorType
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
mod=10**9+7
farr=[1]
ifa=[]
def fact(x,mod=0):
if mod:
while x>=len(farr):
farr.append(farr[-1]*len(farr)%mod)
else:
while x>=len(farr):
farr.append(farr[-1]*len(farr))
return farr[x]
def ifact(x,mod):
global ifa
fact(x,mod)
ifa.append(pow(farr[-1],mod-2,mod))
for i in range(x,0,-1):
ifa.append(ifa[-1]*i%mod)
ifa.reverse()
def per(i,j,mod=0):
if i<j: return 0
if not mod:
return fact(i)//fact(i-j)
return farr[i]*ifa[i-j]%mod
def com(i,j,mod=0):
if i<j: return 0
if not mod:
return per(i,j)//fact(j)
return per(i,j,mod)*ifa[j]%mod
def catalan(n):
return com(2*n,n)//(n+1)
def isprime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def floorsum(a,b,c,n):#sum((a*i+b)//c for i in range(n+1))
if a==0:return b//c*(n+1)
if a>=c or b>=c: return floorsum(a%c,b%c,c,n)+b//c*(n+1)+a//c*n*(n+1)//2
m=(a*n+b)//c
return n*m-floorsum(c,c-b-1,a,m-1)
def inverse(a,m):
a%=m
if a<=1: return a
return ((1-inverse(m,a)*m)//a)%m
def lowbit(n):
return n&-n
class BIT:
def __init__(self,arr):
self.arr=arr
self.n=len(arr)-1
def update(self,x,v):
while x<=self.n:
self.arr[x]+=v
x+=x&-x
def query(self,x):
ans=0
while x:
ans+=self.arr[x]
x&=x-1
return ans
class ST:
def __init__(self,arr):#n!=0
n=len(arr)
mx=n.bit_length()#取不到
self.st=[[0]*mx for i in range(n)]
for i in range(n):
self.st[i][0]=arr[i]
for j in range(1,mx):
for i in range(n-(1<<j)+1):
self.st[i][j]=max(self.st[i][j-1],self.st[i+(1<<j-1)][j-1])
def query(self,l,r):
if l>r:return -inf
s=(r+1-l).bit_length()-1
return max(self.st[l][s],self.st[r-(1<<s)+1][s])
class DSU:#容量+路径压缩
def __init__(self,n):
self.c=[-1]*n
def same(self,x,y):
return self.find(x)==self.find(y)
def find(self,x):
if self.c[x]<0:
return x
self.c[x]=self.find(self.c[x])
return self.c[x]
def union(self,u,v):
u,v=self.find(u),self.find(v)
if u==v:
return False
if self.c[u]>self.c[v]:
u,v=v,u
self.c[u]+=self.c[v]
self.c[v]=u
return True
def size(self,x): return -self.c[self.find(x)]
class UFS:#秩+路径
def __init__(self,n):
self.parent=[i for i in range(n)]
self.ranks=[0]*n
def find(self,x):
if x!=self.parent[x]:
self.parent[x]=self.find(self.parent[x])
return self.parent[x]
def union(self,u,v):
pu,pv=self.find(u),self.find(v)
if pu==pv:
return False
if self.ranks[pu]>=self.ranks[pv]:
self.parent[pv]=pu
if self.ranks[pv]==self.ranks[pu]:
self.ranks[pu]+=1
else:
self.parent[pu]=pv
def Prime(n):
c=0
prime=[]
flag=[0]*(n+1)
for i in range(2,n+1):
if not flag[i]:
prime.append(i)
c+=1
for j in range(c):
if i*prime[j]>n: break
flag[i*prime[j]]=prime[j]
if i%prime[j]==0: break
return flag
def dij(s,graph):
d={}
d[s]=0
heap=[(0,s)]
seen=set()
while heap:
dis,u=heappop(heap)
if u in seen:
continue
seen.add(u)
for v,w in graph[u]:
if v not in d or d[v]>d[u]+w:
d[v]=d[u]+w
heappush(heap,(d[v],v))
return d
def bell(s,g):#bellman-Ford
dis=AI(n,inf)
dis[s]=0
for i in range(n-1):
for u,v,w in edge:
if dis[v]>dis[u]+w:
dis[v]=dis[u]+w
change=A(n)
for i in range(n):
for u,v,w in edge:
if dis[v]>dis[u]+w:
dis[v]=dis[u]+w
change[v]=1
return dis
def lcm(a,b): return a*b//gcd(a,b)
def lis(nums):
res=[]
for k in nums:
i=bisect.bisect_left(res,k)
if i==len(res):
res.append(k)
else:
res[i]=k
return len(res)
def RP(nums):#逆序对
n = len(nums)
s=set(nums)
d={}
for i,k in enumerate(sorted(s),1):
d[k]=i
bi=BIT([0]*(len(s)+1))
ans=0
for i in range(n-1,-1,-1):
ans+=bi.query(d[nums[i]]-1)
bi.update(d[nums[i]],1)
return ans
class DLN:
def __init__(self,val):
self.val=val
self.pre=None
self.next=None
def nb(i,j,n,m):
for ni,nj in [[i+1,j],[i-1,j],[i,j-1],[i,j+1]]:
if 0<=ni<n and 0<=nj<m:
yield ni,nj
def topo(n):
q=deque()
res=[]
for i in range(1,n+1):
if ind[i]==0:
q.append(i)
res.append(i)
while q:
u=q.popleft()
for v in g[u]:
ind[v]-=1
if ind[v]==0:
q.append(v)
res.append(v)
return res
@bootstrap
def gdfs(r,p):
for ch in g[r]:
if ch!=p:
yield gdfs(ch,r)
yield None
class SMT:
def __init__(self,n,query_fn,I):
self.h = 1
while 1 << self.h < n:
self.h += 1
self.n=1<<self.h
self.qf=query_fn
self.tree = [I] * (self.n<<1)
self.lazy = [0] * self.n
def build(self, v):
for k in range(len(v)):
self.tree[k + self.n] = v[k]
for k in range(self.n - 1, 0, -1):
self.tree[k] = self.qf(self.tree[2*k],self.tree[2 * k + 1])
def apply(self, x, val):
self.tree[x] = val
if x < self.n:
self.lazy[x] = 1
def pushup(self, x):
self.tree[x]=self.qf(self.tree[2*x],self.tree[2*x+1])
def push(self, x):#pushdown
if self.lazy[x]:
self.apply(x * 2,0)
self.apply(x * 2+ 1,0)
self.lazy[x] = 0
def set(self,p,x):
p+=self.n
for i in range(self.h,0,-1):self.push(p>>i)
self.tree[p]=x
for i in range(1,self.h+1):self.pushup(p>>i)
def get(self,p):
p+=self.n
for i in range(self.h,0,-1):self.push(p>>i)
return self.tree[p]
def update(self, l, r, h):#左闭右闭
l += self.n
r += self.n
l0=l
r0=r
for i in range(self.h,0,-1):
if (((l>>i)<<i)!=l):self.push(l>>i)
if ((((r+1)>>i)<<i)!=r+1):self.push(r>>i)
while l <= r:
if l & 1:
self.apply(l, h)
l += 1
if not r & 1:
self.apply(r, h)
r -= 1
l>>=1
r>>=1
for i in range(1,self.h+1):
if (((l0>>i)<<i)!=l0):self.pushup(l0>>i)
if ((((r0+1)>>i)<<i)!=r0+1):
self.pushup(r0>>i)
def query(self, l, r):
l += self.n
r += self.n
for i in range(self.h,0,-1):
if (((l>>i)<<i)!=l):self.push(l>>i)
if ((((r+1)>>i)<<i)!=r+1):self.push(r>>i)
ans = 0
while l <= r:
if l & 1:
ans = self.qf(ans, self.tree[l])
l += 1
if not r & 1:
ans = self.qf(ans, self.tree[r])
r -= 1
l >>= 1; r >>= 1
return ans
def add(x,y):
return x+y
t=1
for i in range(t):
n=N()
s=set()
x=A(n)
y=A(n)
for i in range(n):
p,l=RL()
x[i]=p
y[i]=p+l
s.add(p)
s.add(y[i])
s=sorted(s)
d={x:i for i,x in enumerate(s)}
k=len(s)-1
arr=[s[i]-s[i-1] for i in range(1,k+1)]
smt=SMT(k,add,0)
smt.build(arr)
g=G(n)
q=N()
ans=A(q)
for i in range(q):
a,b=RL()
a-=1
b-=1
g[a].append((i,b))
for i in range(n-1,-1,-1):
l=d[x[i]]
r=d[y[i]]
smt.update(l,r-1,0)
for j,cur in g[i]:
cur=d[x[cur]]
ans[j]=smt.query(l,cur-1)
for x in ans:
print(x)
'''
sys.setrecursionlimit(200000)
import threading
threading.stack_size(10**8)
t=threading.Thr
ead(target=main)
t.start()
t.join()
'''
| python | code_algorithm | [
{
"input": "6\n1 5\n3 3\n4 4\n9 2\n10 1\n12 1\n4\n1 2\n2 4\n2 5\n2 6\n",
"output": "0\n1\n1\n2\n"
},
{
"input": "3\n1 1000000000\n999999999 1000000000\n1000000000 1000000000\n4\n1 2\n1 2\n2 3\n1 3\n",
"output": "0\n0\n0\n0\n"
},
{
"input": "2\n304 54\n88203 83\n1\n1 2\n",
"output": "87845\n"
},
{
"input": "20\n4524 38\n14370 10\n22402 37\n34650 78\n50164 57\n51744 30\n55372 55\n56064 77\n57255 57\n58862 64\n59830 38\n60130 68\n66176 20\n67502 39\n67927 84\n68149 63\n71392 62\n74005 14\n76084 74\n86623 91\n5\n19 20\n13 18\n11 14\n7 8\n16 17\n",
"output": "10465\n7561\n7546\n637\n3180\n"
},
{
"input": "2\n1 1000000000\n1000000000 1000000000\n2\n1 2\n1 2\n",
"output": "0\n0\n"
},
{
"input": "10\n142699629 183732682\n229190264 203769218\n582937498 331846994\n637776490 872587100\n646511567 582113351\n708368481 242093919\n785185417 665206490\n827596004 933089845\n905276008 416253629\n916536536 583835690\n5\n9 10\n4 9\n1 10\n6 10\n4 5\n",
"output": "0\n0\n149978016\n0\n0\n"
},
{
"input": "3\n1 1\n500000000 100\n1000000000 1000000000\n5\n1 2\n1 3\n2 3\n1 3\n1 2\n",
"output": "499999998\n999999898\n499999900\n999999898\n499999998\n"
},
{
"input": "2\n1 1\n1000000000 1000000000\n3\n1 2\n1 2\n1 2\n",
"output": "999999998\n999999998\n999999998\n"
},
{
"input": "2\n399580054 931100304\n652588203 507603581\n1\n1 2\n",
"output": "0\n"
},
{
"input": "10\n11567 8\n13351 90\n32682 29\n36536 45\n37498 64\n46994 29\n69218 81\n85417 90\n87100 90\n96004 86\n5\n8 9\n4 5\n9 10\n8 10\n1 3\n",
"output": "1593\n917\n8814\n10407\n21017\n"
}
] | code_contests | python | 0 | 76932f649afa513374a562fc0f32d43e |
We all know that GukiZ often plays with arrays.
Now he is thinking about this problem: how many arrays a, of length n, with non-negative elements strictly less then 2l meet the following condition: <image>? Here operation <image> means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation <image> means bitwise OR (in Pascal it is equivalent to <image>, in C/C++/Java/Python it is equivalent to |).
Because the answer can be quite large, calculate it modulo m. This time GukiZ hasn't come up with solution, and needs you to help him!
Input
First and the only line of input contains four integers n, k, l, m (2 ≤ n ≤ 1018, 0 ≤ k ≤ 1018, 0 ≤ l ≤ 64, 1 ≤ m ≤ 109 + 7).
Output
In the single line print the number of arrays satisfying the condition above modulo m.
Examples
Input
2 1 2 10
Output
3
Input
2 1 1 3
Output
1
Input
3 3 2 10
Output
9
Note
In the first sample, satisfying arrays are {1, 1}, {3, 1}, {1, 3}.
In the second sample, only satisfying array is {1, 1}.
In the third sample, satisfying arrays are {0, 3, 3}, {1, 3, 2}, {1, 3, 3}, {2, 3, 1}, {2, 3, 3}, {3, 3, 0}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | # -*- coding: utf-8 -*-
from collections import deque
def calc(n, m):
if n == 1:
return [[1, 0], [0, 1]]
a = calc(n // 2, m)
if n % 2 == 0:
res00 = (a[0][0] * a[0][0]) % m
res00 = (res00 + a[0][0] * a[1][0]) % m
res00 = (res00 + a[0][1] * a[0][0]) % m
res01 = (a[0][0] * a[0][1]) % m
res01 = (res01 + a[0][0] * a[1][1]) % m
res01 = (res01 + a[0][1] * a[0][1]) % m
res10 = (a[1][0] * a[0][0]) % m
res10 = (res10 + a[1][0] * a[1][0]) % m
res10 = (res10 + a[1][1] * a[0][0]) % m
res11 = (a[1][0] * a[0][1]) % m
res11 = (res11 + a[1][0] * a[1][1]) % m
res11 = (res11 + a[1][1] * a[0][1]) % m
return [[res00, res01], [res10, res11]]
else:
res00 = (a[0][0] * a[0][0] * 2) % m
res00 = (res00 + a[0][0] * a[1][0]) % m
res00 = (res00 + a[0][1] * a[0][0]) % m
res00 = (res00 + a[0][1] * a[1][0]) % m
res01 = (a[0][0] * a[0][1] * 2) % m
res01 = (res01 + a[0][0] * a[1][1]) % m
res01 = (res01 + a[0][1] * a[0][1]) % m
res01 = (res01 + a[0][1] * a[1][1]) % m
res10 = (a[1][0] * a[0][0] * 2) % m
res10 = (res10 + a[1][0] * a[1][0]) % m
res10 = (res10 + a[1][1] * a[0][0]) % m
res10 = (res10 + a[1][1] * a[1][0]) % m
res11 = (a[1][0] * a[0][1] * 2) % m
res11 = (res11 + a[1][0] * a[1][1]) % m
res11 = (res11 + a[1][1] * a[0][1]) % m
res11 = (res11 + a[1][1] * a[1][1]) % m
return [[res00, res01], [res10, res11]]
def binpow(a, p, m):
if p == 0:
return 1 % m
if p == 1:
return a % m
ans = binpow(a, p // 2, m)
ans = (ans * ans) % m
if p % 2 == 1:
ans = (ans * a) % m
return ans
n, k, l, m = map(int, input().split())
ans = [0, 0]
x = calc(n, m)
ans[0] = (x[0][0] + x[0][1] + x[1][0] + x[1][1]) % m
ans[1] = ((binpow(2, n, m) - ans[0]) % m + m) % m
res = 1
for i in range(l):
res = (res * ans[k & 1]) % m
k >>= 1
if k > 0:
res = 0
print(res % m) | python | code_algorithm | [
{
"input": "2 1 2 10\n",
"output": "3\n"
},
{
"input": "2 1 1 3\n",
"output": "1\n"
},
{
"input": "3 3 2 10\n",
"output": "9\n"
},
{
"input": "131231231 35435 63 153459\n",
"output": "9232\n"
},
{
"input": "165467464 5416516 45 364545697\n",
"output": "28484610\n"
},
{
"input": "64 64 64 64\n",
"output": "0\n"
},
{
"input": "1000000000000000000 1 64 911\n",
"output": "868\n"
},
{
"input": "999999999999999999 999999999999999999 63 3\n",
"output": "1\n"
},
{
"input": "154165741654 154168764 54 546546\n",
"output": "347802\n"
},
{
"input": "13713712721 13458749846 38 546863217\n",
"output": "202473723\n"
},
{
"input": "6 0 0 1\n",
"output": "0\n"
},
{
"input": "2 0 0 1000000007\n",
"output": "1\n"
},
{
"input": "987408898498 1233432432 15 15\n",
"output": "0\n"
},
{
"input": "2 0 1 3\n",
"output": "0\n"
},
{
"input": "68706870687 984089409849 59 156465748\n",
"output": "51245777\n"
},
{
"input": "564654151 123131 38 654654\n",
"output": "113542\n"
},
{
"input": "32132321412 2132134124 34 2321321\n",
"output": "145556\n"
},
{
"input": "101 102 13 104\n",
"output": "37\n"
},
{
"input": "1001 1 0 4\n",
"output": "0\n"
},
{
"input": "2 0 0 1\n",
"output": "0\n"
},
{
"input": "4 10003242 2 99999991\n",
"output": "0\n"
},
{
"input": "2595952145 564654654 64 471571451\n",
"output": "25322553\n"
},
{
"input": "2 0 64 1000000007\n",
"output": "767713261\n"
},
{
"input": "5165151 3545344 49 354354\n",
"output": "269269\n"
},
{
"input": "4 4 4 4\n",
"output": "0\n"
},
{
"input": "6544433213 3232321 63 2121232\n",
"output": "1767237\n"
},
{
"input": "131354 1354534 51 1534354\n",
"output": "319559\n"
},
{
"input": "9992121323332 32133312321 58 2\n",
"output": "0\n"
},
{
"input": "1000000000 100000000077789 58 864405117\n",
"output": "21891069\n"
},
{
"input": "1000000001 1000000002 37 1000000007\n",
"output": "472514342\n"
},
{
"input": "21 21 21 21\n",
"output": "1\n"
},
{
"input": "2 1 64 1\n",
"output": "0\n"
},
{
"input": "1928374655 1111 25 1231237\n",
"output": "221684\n"
},
{
"input": "1000000000000000000 1000000000000000000 64 1000000007\n",
"output": "818137911\n"
},
{
"input": "1000000000000000000 1000000000000000000 64 1\n",
"output": "0\n"
},
{
"input": "1114 7 3 1005\n",
"output": "193\n"
},
{
"input": "717273747576 1213141516 59 123456789\n",
"output": "91290627\n"
},
{
"input": "2 0 0 5\n",
"output": "1\n"
},
{
"input": "35413210 444444 44 4444447\n",
"output": "2415375\n"
},
{
"input": "45305640 6540640606 51 5406546\n",
"output": "891777\n"
},
{
"input": "1211199887 77665544 64 123123\n",
"output": "25216\n"
},
{
"input": "321456987 654789321 50 4564569\n",
"output": "4487490\n"
},
{
"input": "111111111111 111111111111 41 11\n",
"output": "0\n"
},
{
"input": "5 6 29 108\n",
"output": "37\n"
},
{
"input": "2 1000000000000000000 0 1\n",
"output": "0\n"
},
{
"input": "3461827346 97649324 33 1324157\n",
"output": "1172060\n"
},
{
"input": "85689952135646564 456465135103154 61 554556465\n",
"output": "526174733\n"
},
{
"input": "61546535 168465146 13 354354\n",
"output": "0\n"
},
{
"input": "432532 321312 47 32323\n",
"output": "21923\n"
},
{
"input": "951892365 123481283597 32 123456\n",
"output": "0\n"
},
{
"input": "100 10 10 100\n",
"output": "0\n"
},
{
"input": "1000000000000000000 1 64 1\n",
"output": "0\n"
},
{
"input": "2 1 0 1\n",
"output": "0\n"
},
{
"input": "8894681 35135435 15 1351351\n",
"output": "0\n"
},
{
"input": "1353513 6545341 54 5454547\n",
"output": "4341376\n"
},
{
"input": "1564 153 12 1000000007\n",
"output": "360373699\n"
},
{
"input": "14 14 14 1414\n",
"output": "1043\n"
},
{
"input": "241531351 230321 59 7412135\n",
"output": "1413850\n"
},
{
"input": "2 1000000000000000000 64 1\n",
"output": "0\n"
},
{
"input": "5135 42542 15 4354\n",
"output": "0\n"
},
{
"input": "467513 453754 15 15555\n",
"output": "0\n"
},
{
"input": "453403154 354134 12 354354\n",
"output": "0\n"
},
{
"input": "16 16 4 98218222\n",
"output": "0\n"
},
{
"input": "1111111 1212121 21 1212199\n",
"output": "1058809\n"
},
{
"input": "978464816 78484331 42 654534\n",
"output": "234981\n"
},
{
"input": "987654321 123456789 64 65406468\n",
"output": "62322669\n"
},
{
"input": "15153153351 21243512 61 534354199\n",
"output": "529706284\n"
},
{
"input": "10321324 13213413210 55 1351\n",
"output": "1196\n"
},
{
"input": "2 1 63 1000000007\n",
"output": "529745921\n"
},
{
"input": "351351354 5464487 64 484848484\n",
"output": "32687413\n"
}
] | code_contests | python | 0 | 12eb2b6076fa19feb0ccc99b1c0cb8e6 |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | string = input()
n = len(string)
stack = []
mapping = [0]*n
# First we create an array where the array called mapping where the index of array
# gives the index of start of opening bracket in string and the value at that index
# gives the index of closing bracket for corresponding opening bracket.
for idx, char in enumerate(string):
if char == '(':
# We add idx of ( to stack which will be used as idx in mapping
stack.append(idx)
# Map only those ')' which have an opening brace, empty stack signifies
# closing brace has no opening brace.
elif char == ')' and stack:
mapping[stack.pop()] = idx
# Once we have generated the mapping we scan the array the difference between the index and its
# value gives us the len of current regular expression. Now we take the value at that index and
# icrement it to see if there is some non zero value at that location signifing that the regular
# expression can be expanded. We continue this till no more expansion or we have some current length
# more than previous expansions.
i = 0
mx = 0
cnt = 1
tmp_max = 0
prev_join_idx = 0
while i < n:
# If value present at mapping
if mapping[i]:
curr_len = mapping[i] - i + 1
# if we have some previous regx
if i == prev_join_idx + 1:
tmp_max += curr_len
else:
tmp_max = curr_len
# We have found new regx whose len is more than all prev scanned regx
if tmp_max > mx:
mx = tmp_max
cnt = 1
elif tmp_max == mx:
cnt += 1
prev_join_idx = mapping[i]
i = mapping[i] + 1
else:
i +=1
print(mx, cnt) | python | code_algorithm | [
{
"input": ")((())))(()())\n",
"output": "6 2\n"
},
{
"input": "))(\n",
"output": "0 1\n"
},
{
"input": "())(((((())())((((()))(())))())())(((()(()()()())(())()))((()(())())()()))()(()())))))(()))((())((((\n",
"output": "80 1\n"
},
{
"input": "))))()())))\n",
"output": "4 1\n"
},
{
"input": ")(()(((((())))))(((()))))))((()(((((((())())()))))((((((((())))((((((())))))(()((((()))))(()))))))))\n",
"output": "68 1\n"
},
{
"input": "))(((((((()((((())(((((((((()()))))(()())()((())())))((((()))(()()()))))(()()))())))))((())())()))((\n",
"output": "92 1\n"
},
{
"input": "(()())()(())()()())())()((()(()(())()()())((()(())()(()()()()))()(())()(((()())()(()((())()(())(()))\n",
"output": "28 1\n"
},
{
"input": "((((()(((\n",
"output": "2 1\n"
},
{
"input": "()(())()\n",
"output": "8 1\n"
}
] | code_contests | python | 0.6 | ca4f51c34687217261a413a10947d0c4 |
Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5.... The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Note that the sequence contains numbers, not digits. For example number 10 first appears in the sequence in position 55 (the elements are numerated from one).
Find the number on the n-th position of the sequence.
Input
The only line contains integer n (1 ≤ n ≤ 1014) — the position of the number to find.
Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.
Output
Print the element in the n-th position of the sequence (the elements are numerated from one).
Examples
Input
3
Output
2
Input
5
Output
2
Input
10
Output
4
Input
55
Output
10
Input
56
Output
1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
from math import sqrt
# Find max m such that m(m+1)/2 <= n
m = int((-1 + sqrt(1 + 8*n))/2)
# l is the previous range
l = m * (m + 1) // 2
print(m if n == l else n - l) | python | code_algorithm | [
{
"input": "56\n",
"output": "1\n"
},
{
"input": "5\n",
"output": "2\n"
},
{
"input": "55\n",
"output": "10\n"
},
{
"input": "3\n",
"output": "2\n"
},
{
"input": "10\n",
"output": "4\n"
},
{
"input": "1000000000000\n",
"output": "88209\n"
},
{
"input": "99994\n",
"output": "313\n"
},
{
"input": "99993\n",
"output": "312\n"
},
{
"input": "99996\n",
"output": "315\n"
},
{
"input": "100\n",
"output": "9\n"
},
{
"input": "99999998250180\n",
"output": "14142135\n"
},
{
"input": "99998\n",
"output": "317\n"
},
{
"input": "99999\n",
"output": "318\n"
},
{
"input": "99990\n",
"output": "309\n"
},
{
"input": "1021\n",
"output": "31\n"
},
{
"input": "99995\n",
"output": "314\n"
},
{
"input": "6\n",
"output": "3\n"
},
{
"input": "847194127849\n",
"output": "255708\n"
},
{
"input": "99991\n",
"output": "310\n"
},
{
"input": "4\n",
"output": "1\n"
},
{
"input": "101\n",
"output": "10\n"
},
{
"input": "99939\n",
"output": "258\n"
},
{
"input": "99997\n",
"output": "316\n"
},
{
"input": "23\n",
"output": "2\n"
},
{
"input": "9994\n",
"output": "124\n"
},
{
"input": "99992\n",
"output": "311\n"
},
{
"input": "35\n",
"output": "7\n"
},
{
"input": "102\n",
"output": "11\n"
},
{
"input": "999999911792\n",
"output": "1\n"
},
{
"input": "1\n",
"output": "1\n"
},
{
"input": "294719472984\n",
"output": "593358\n"
},
{
"input": "103\n",
"output": "12\n"
},
{
"input": "8\n",
"output": "2\n"
},
{
"input": "10101010\n",
"output": "745\n"
},
{
"input": "2\n",
"output": "1\n"
},
{
"input": "100000000000000\n",
"output": "1749820\n"
},
{
"input": "999999911791\n",
"output": "1414213\n"
}
] | code_contests | python | 0.2 | 2107e2742cdbf444e981c723cc9d80b2 |
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.
Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
Examples
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n, k = input().split(" ")
n, k = [int(n), int(k)]
list1 = list(map(int, input().split(" ")))
list2 = list(map(int, input().split(" ")))
low = 0
high = 2*(10**9)
while low < high:
if high - low % 2 != 0:
mid = low + (high - low) // 2 + 1
else:
mid = low + (high - low)//2
d = k
list6 = []
for i in range(n):
if list1[i]*mid <= list2[i]:
continue
else:
c = list1[i] * mid - list2[i]
if d >= c:
d = d - c
continue
else:
high = mid -1
break
if high != mid - 1:
low = mid
print(low)
| python | code_algorithm | [
{
"input": "4 3\n4 3 5 6\n11 12 14 20\n",
"output": "3\n"
},
{
"input": "3 1\n2 1 4\n11 3 16\n",
"output": "4\n"
},
{
"input": "60 735\n3 1 4 7 1 7 3 1 1 5 4 7 3 3 3 2 5 3 1 2 3 6 1 1 1 1 1 2 5 3 2 1 3 5 2 1 2 2 2 2 1 3 3 3 6 4 3 5 1 3 2 2 1 3 1 1 1 7 1 2\n596 968 975 493 665 571 598 834 948 941 737 649 923 848 950 907 929 865 227 836 956 796 861 801 746 667 539 807 405 355 501 879 994 890 573 848 597 873 130 985 924 426 999 550 586 924 601 807 994 878 410 817 922 898 982 525 611 685 806 847\n",
"output": "103\n"
},
{
"input": "30 300\n1 4 2 1 2 5 6 4 1 3 2 1 1 1 1 1 2 3 1 3 4 2 2 3 2 2 2 1 1 1\n997 817 767 860 835 809 817 565 630 804 586 953 977 356 905 890 958 916 740 583 902 945 313 956 871 729 976 707 516 788\n",
"output": "164\n"
},
{
"input": "1 1\n1\n1\n",
"output": "2\n"
},
{
"input": "2 1\n2 2\n1 1\n",
"output": "0\n"
},
{
"input": "2 6\n1 3\n6 2\n",
"output": "2\n"
},
{
"input": "1 1000\n1000\n1000\n",
"output": "2\n"
},
{
"input": "1 1\n4\n6\n",
"output": "1\n"
},
{
"input": "10 926\n5 6 8 1 2 5 1 8 4 4\n351 739 998 725 953 970 906 691 707 1000\n",
"output": "137\n"
},
{
"input": "20 925\n7 3 1 2 1 3 1 3 1 2 3 1 5 8 1 3 7 3 4 2\n837 898 965 807 786 670 626 873 968 745 878 359 760 781 829 882 777 740 907 779\n",
"output": "150\n"
},
{
"input": "1 1\n10\n2\n",
"output": "0\n"
},
{
"input": "70 130\n2 1 2 2 3 3 2 5 2 2 3 3 3 1 1 4 3 5 3 2 1 3 7 1 2 7 5 2 1 6 3 4 1 2 1 1 1 1 3 6 4 2 2 8 2 2 4 1 4 2 1 4 4 3 5 1 1 1 1 1 2 3 1 5 1 3 3 4 2 2\n473 311 758 768 797 572 656 898 991 534 989 702 934 767 777 799 1000 655 806 727 718 948 834 965 832 778 706 861 799 874 745 970 772 967 984 886 835 795 832 837 950 952 475 891 947 952 903 929 689 478 725 945 585 943 771 631 729 887 557 738 824 758 999 786 669 992 918 762 964 941\n",
"output": "119\n"
},
{
"input": "80 979\n2 1 1 1 2 1 1 1 3 1 4 4 2 1 1 3 1 1 2 1 4 1 1 2 5 4 8 1 3 6 5 7 2 3 4 1 2 2 6 1 2 2 4 1 1 2 3 2 8 1 1 3 3 4 1 1 2 1 4 4 1 4 3 2 6 5 2 1 4 1 2 3 2 1 3 3 1 2 1 3\n498 976 513 869 917 914 664 656 957 893 981 947 985 693 576 958 987 822 981 718 884 729 295 683 485 998 730 894 731 975 739 854 906 740 987 976 606 689 990 775 522 994 920 893 529 651 989 799 643 215 946 987 297 868 425 810 694 908 736 903 970 751 625 904 955 945 839 777 977 974 905 900 666 680 799 873 565 919 536 686\n",
"output": "128\n"
},
{
"input": "50 530\n2 3 3 1 1 1 3 4 4 2 4 2 5 1 3 1 2 6 1 1 2 5 3 2 1 5 1 3 3 2 1 1 1 1 2 1 1 2 2 1 4 2 1 3 1 2 1 1 4 2\n959 972 201 990 675 679 972 268 976 886 488 924 795 959 647 994 969 862 898 646 763 797 978 763 995 641 923 856 829 921 934 883 904 986 728 980 1000 775 716 745 833 832 999 651 571 626 827 456 636 795\n",
"output": "133\n"
},
{
"input": "40 538\n1 3 3 1 4 1 1 1 1 5 3 3 4 1 4 2 7 1 4 1 1 2 2 1 1 1 1 4 1 4 2 3 3 3 1 3 4 1 3 5\n975 635 795 835 982 965 639 787 688 796 988 779 839 942 491 696 396 995 718 810 796 879 957 783 844 765 968 783 647 214 995 868 318 453 989 889 504 962 945 925\n",
"output": "104\n"
},
{
"input": "1 1000\n1\n1000\n",
"output": "2000\n"
}
] | code_contests | python | 0 | 4c6e98cb3d7ad6caea7c89dfcd29fc1e |
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 ≤ n ≤ 2·105, 1 ≤ s ≤ n) — the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | f = lambda: map(int, input().split())
n, s = f()
c = [0] * n
t = list(f())
for i in t: c[i] += 1
k = t[s - 1]
c[k] -= 1
d = c[0]
c += [d]
d += k > 0
i, j = 1, n
while i < j:
if c[i]: i += 1
elif c[j]:
c[j] -= 1
i += 1
d += j < n
else: j -= 1
print(d) | python | code_algorithm | [
{
"input": "5 3\n1 0 0 4 1\n",
"output": "2\n"
},
{
"input": "3 2\n2 0 2\n",
"output": "1\n"
},
{
"input": "3 2\n2 1 1\n",
"output": "1\n"
},
{
"input": "3 3\n2 1 2\n",
"output": "1\n"
},
{
"input": "7 1\n4 4 6 6 6 6 5\n",
"output": "4\n"
},
{
"input": "3 2\n2 0 1\n",
"output": "0\n"
},
{
"input": "3 1\n2 1 1\n",
"output": "1\n"
},
{
"input": "3 1\n0 1 2\n",
"output": "0\n"
},
{
"input": "10 6\n3 0 0 0 0 0 0 1 0 0\n",
"output": "7\n"
},
{
"input": "5 5\n0 1 1 0 0\n",
"output": "2\n"
},
{
"input": "3 2\n2 2 1\n",
"output": "1\n"
},
{
"input": "5 1\n0 0 1 3 4\n",
"output": "1\n"
},
{
"input": "2 1\n1 1\n",
"output": "1\n"
},
{
"input": "9 1\n0 0 0 2 5 5 5 5 5\n",
"output": "3\n"
},
{
"input": "6 1\n0 1 2 2 0 0\n",
"output": "2\n"
},
{
"input": "2 2\n1 0\n",
"output": "0\n"
},
{
"input": "9 1\n0 1 1 1 1 1 6 7 8\n",
"output": "3\n"
},
{
"input": "2 2\n1 1\n",
"output": "1\n"
},
{
"input": "3 3\n1 1 0\n",
"output": "0\n"
},
{
"input": "3 2\n2 2 2\n",
"output": "2\n"
},
{
"input": "2 1\n0 0\n",
"output": "1\n"
},
{
"input": "1 1\n0\n",
"output": "0\n"
},
{
"input": "3 1\n0 0 2\n",
"output": "1\n"
},
{
"input": "6 1\n5 2 1 3 3 1\n",
"output": "1\n"
},
{
"input": "2 1\n1 0\n",
"output": "2\n"
},
{
"input": "9 1\n0 1 1 1 1 5 6 7 8\n",
"output": "3\n"
},
{
"input": "3 3\n2 1 0\n",
"output": "0\n"
},
{
"input": "2 2\n0 1\n",
"output": "2\n"
},
{
"input": "2 2\n0 0\n",
"output": "1\n"
},
{
"input": "2 1\n0 1\n",
"output": "0\n"
},
{
"input": "3 1\n1 2 2\n",
"output": "2\n"
}
] | code_contests | python | 0 | 1ece88b57d253567d43de88b08d0a0d4 |
Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is ai meters high.
Vladimir has just planted n bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing.
Vladimir wants to check the bamboos each d days (i.e. d days after he planted, then after 2d days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than k meters.
What is the maximum value d he can choose so that he can achieve what he wants without cutting off more than k meters of bamboo?
Input
The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1011) — the number of bamboos and the maximum total length of cut parts, in meters.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the required heights of bamboos, in meters.
Output
Print a single integer — the maximum value of d such that Vladimir can reach his goal.
Examples
Input
3 4
1 3 5
Output
3
Input
3 40
10 30 50
Output
32
Note
In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import itertools
unfold = itertools.chain.from_iterable
def jumps(a):
d = speedup
while d < a - 1:
c = (a + d - 1) // d
d = (a + c - 2) // (c - 1)
yield d
def calc(d):
return sum(d - 1 - (i - 1) % d for i in a)
def ans():
for d, pd in zip(D, D[1:]):
d -= 1
cd = calc(d)
if cd <= k:
return d
if d == pd:
continue
cpd = calc(pd)
if d - pd >= (cd - k) * (d - pd) / (cd - cpd):
return d - (cd - k) * (d - pd) / (cd - cpd)
return 1
n, k = map(int, input().split())
a = list(map(int, input().split()))
speedup = int(max(a) ** 0.5)
D = sorted(set(range(1, speedup + 1)).union([max(a) + k + 1]).union(set(
unfold(map(jumps, a)))), reverse=True)
print('%d' % ans()) | python | code_algorithm | [
{
"input": "3 40\n10 30 50\n",
"output": "32\n"
},
{
"input": "3 4\n1 3 5\n",
"output": "3\n"
},
{
"input": "27 56379627\n67793612 34811850 20130370 79625926 35488291 62695111 76809609 2652596 18057723 61935027 62863641 43354418 50508660 29330027 28838758 19040655 19092404 56094994 69200145 36483441 18502131 77853701 20991083 67819468 32956589 80681963 41432161\n",
"output": "5275618\n"
},
{
"input": "100 5\n5 2 4 5 4 4 4 4 2 5 3 4 2 4 4 1 1 5 3 2 2 1 3 3 2 5 3 4 5 1 3 5 4 4 4 3 1 4 4 3 4 5 2 5 4 2 1 2 2 3 5 5 5 1 4 5 3 1 4 2 2 5 1 5 3 4 1 5 1 2 2 3 5 1 3 2 4 2 4 2 2 4 1 3 5 2 2 2 3 3 4 3 2 2 5 5 4 2 5 4\n",
"output": "1\n"
},
{
"input": "10 99\n62 10 47 53 9 83 33 15 24 28\n",
"output": "21\n"
},
{
"input": "1 1215752192\n1000000000\n",
"output": "2215752192\n"
},
{
"input": "1 100000000000\n1\n",
"output": "100000000001\n"
},
{
"input": "100 2619\n1207 5971 951 8250 6594 219 9403 9560 2368 289 3502 6626 1009 828 2378 615 3092 3751 841 7454 8001 1345 1644 1021 7088 7083 2805 1971 7456 6494 3805 9279 8737 8994 2975 2566 6490 1720 3595 8721 8500 4850 9716 2076 9532 1077 8370 2809 2885 1838 6081 667 900 9786 5600 3707 8818 7580 6067 2215 1726 2171 2338 462 7592 9281 4104 1733 7223 6557 372 4197 9956 2098 6875 3226 9997 4214 8644 1237 259 5252 2379 1115 7452 9338 8430 5013 7906 7804 8339 4782 5329 4607 7632 2664 1437 7518 2674 8519\n",
"output": "57\n"
},
{
"input": "2 948507270\n461613425 139535653\n",
"output": "774828174\n"
},
{
"input": "100 7\n4 5 5 10 10 5 8 5 7 4 5 4 6 8 8 2 6 3 3 10 7 10 8 6 2 7 3 9 7 7 2 4 5 2 4 9 5 10 1 10 5 10 4 1 3 4 2 6 9 9 9 10 6 2 5 6 1 8 10 4 10 3 4 10 5 5 4 10 4 5 3 7 10 2 7 3 6 9 6 1 6 5 5 4 6 6 4 4 1 5 1 6 6 6 8 8 6 2 6 3\n",
"output": "1\n"
},
{
"input": "100 82\n51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91 64\n",
"output": "2\n"
},
{
"input": "4 1\n999999998 999999998 1000000000 1000000000\n",
"output": "2\n"
},
{
"input": "42 54763468991\n628145517 376140463 883515281 186969586 762888636 326402540 98152103 158176573 61402893 127860890 9580639 570870045 646139320 178509023 484027667 61263305 841082556 558212775 940563716 26389630 579113529 496148000 925801173 837151741 70301174 656585276 285845006 902071051 403573724 727123763 9467291 296231663 631265401 899374334 520690250 798571511 491747710 799313373 643215696 789204467 614608449 162733265\n",
"output": "1791188095\n"
},
{
"input": "1 78110679371\n570497240\n",
"output": "78681176611\n"
},
{
"input": "1 100000000000\n1000000000\n",
"output": "101000000000\n"
},
{
"input": "100 1\n73 83 99 13 69 2 60 3 68 46 1 87 58 39 21 84 7 82 55 80 33 40 4 16 20 50 78 5 62 94 95 66 75 42 65 63 72 76 11 45 56 37 93 91 98 52 74 71 49 90 8 53 38 85 86 27 100 92 97 32 59 34 14 22 19 79 51 89 18 64 31 44 6 29 47 10 41 30 70 81 24 9 26 35 15 17 36 43 25 61 77 57 88 96 12 54 28 48 23 67\n",
"output": "1\n"
},
{
"input": "20 53\n32 64 20 41 97 50 20 66 68 22 60 74 61 97 54 80 30 72 59 18\n",
"output": "6\n"
},
{
"input": "100 1215752192\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "12157522\n"
},
{
"input": "100 100\n87 37 55 61 98 38 79 11 32 56 23 27 83 36 41 9 94 82 57 7 39 19 1 29 77 59 78 35 70 50 30 26 64 21 46 5 18 60 93 74 3 73 34 14 22 91 8 69 12 10 58 89 17 65 80 63 71 15 31 33 66 45 90 20 86 76 96 81 40 6 85 25 42 44 2 100 16 51 95 49 62 47 4 52 43 28 99 97 53 48 13 72 68 88 54 75 84 67 24 92\n",
"output": "2\n"
},
{
"input": "100 1215752192\n33 37 57 77 50 53 88 83 76 89 79 28 73 25 5 15 22 29 97 7 46 69 51 52 81 14 9 35 27 74 66 2 84 59 10 1 19 39 40 99 78 82 100 95 3 21 44 75 62 12 58 36 72 31 26 98 64 34 63 41 65 4 86 85 32 96 71 11 94 60 56 45 8 13 47 61 80 43 67 49 87 68 55 48 16 18 54 90 42 20 70 92 38 30 17 24 91 6 93 23\n",
"output": "12157572\n"
},
{
"input": "39 33087783\n70600647 2266901 11966839 31198350 24855193 11526437 976383 74744419 100554597 48347342 72742435 1886535 15699879 12775143 3554161 31308764 25824153 31740293 25001473 15377604 90766535 81246786 35305728 88961314 70878298 47024065 96680069 38135882 80553110 18049023 63601987 81673677 39237071 79565855 13467611 66174846 75022463 63762145 3355796\n",
"output": "2290215\n"
},
{
"input": "2 1215752192\n999999999 1000000000\n",
"output": "1607876095\n"
}
] | code_contests | python | 0 | 15b57fd73b6977d1e61037b47c1d05eb |
Alice and Bob are playing a game on a line with n cells. There are n cells labeled from 1 through n. For each i from 1 to n-1, cells i and i+1 are adjacent.
Alice initially has a token on some cell on the line, and Bob tries to guess where it is.
Bob guesses a sequence of line cell numbers x_1, x_2, …, x_k in order. In the i-th question, Bob asks Alice if her token is currently on cell x_i. That is, Alice can answer either "YES" or "NO" to each Bob's question.
At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.
Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.
You are given n and Bob's questions x_1, …, x_k. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Let (a,b) denote a scenario where Alice starts at cell a and ends at cell b. Two scenarios (a_i, b_i) and (a_j, b_j) are different if a_i ≠ a_j or b_i ≠ b_j.
Input
The first line contains two integers n and k (1 ≤ n,k ≤ 10^5) — the number of cells and the number of questions Bob asked.
The second line contains k integers x_1, x_2, …, x_k (1 ≤ x_i ≤ n) — Bob's questions.
Output
Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.
Examples
Input
5 3
5 1 4
Output
9
Input
4 8
1 2 3 4 4 3 2 1
Output
0
Input
100000 1
42
Output
299997
Note
The notation (i,j) denotes a scenario where Alice starts at cell i and ends at cell j.
In the first example, the valid scenarios are (1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5). For example, (3,4) is valid since Alice can start at cell 3, stay there for the first three questions, then move to cell 4 after the last question.
(4,5) is valid since Alice can start at cell 4, stay there for the first question, the move to cell 5 for the next two questions. Note that (4,5) is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.
In the second example, Alice has no valid scenarios.
In the last example, all (i,j) where |i-j| ≤ 1 except for (42, 42) are valid scenarios.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from collections import Counter
nums, num_questions = map(int, input().split())
questions = list(map(int, input().split()))
exist = set()
things = dict(Counter(questions))
nums = nums * 3 - 2 - len(things)
for i in questions:
if i not in exist:
exist.add(i)
if things.get(i - 1):
nums -= 1
if things.get(i + 1):
nums -= 1
things[i] -= 1
print(nums)
| python | code_algorithm | [
{
"input": "5 3\n5 1 4\n",
"output": "9\n"
},
{
"input": "100000 1\n42\n",
"output": "299997\n"
},
{
"input": "4 8\n1 2 3 4 4 3 2 1\n",
"output": "0\n"
},
{
"input": "50 75\n2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50\n",
"output": "0\n"
},
{
"input": "300 20\n283 157 216 37 287 120 17 154 90 138 96 222 171 273 269 230 198 247 87 299\n",
"output": "878\n"
},
{
"input": "50000 30\n33549 17601 44000 7481 38819 15862 27683 21020 24720 399 14593 35601 41380 25049 46665 32822 24640 11058 26495 34522 49913 18477 12333 4947 30203 26721 3805 7259 42643 4522\n",
"output": "149968\n"
},
{
"input": "1 2\n1 1\n",
"output": "0\n"
}
] | code_contests | python | 0 | b64323f1c422d3dfb28ac21b44c2c474 |
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def func(st,en):
for i in range(st,en+1):
for j in range(i+1,en+1 ):
if (j-i+1)%2==1:
if s[(i+j)//2]==s[i]==s[j]:
return False
#print(st, en)
return True
s=input().strip()
c=0
n=len(s)
for i in range(2,9):
for j in range(len(s)):
if i+j<len(s):
if func(j,j+i):
c+=1
print((n*(n+1))//2 - c-n-n+1) | python | code_algorithm | [
{
"input": "010101\n",
"output": "3\n"
},
{
"input": "11001100\n",
"output": "0\n"
},
{
"input": "0000\n",
"output": "3\n"
},
{
"input": "00\n",
"output": "0\n"
},
{
"input": "1101111000011110111111110101100111111110111100001111011010111001101100010110000001010101101010111000\n",
"output": "4672\n"
},
{
"input": "101\n",
"output": "0\n"
},
{
"input": "100\n",
"output": "0\n"
},
{
"input": "0\n",
"output": "0\n"
},
{
"input": "0010\n",
"output": "0\n"
},
{
"input": "000\n",
"output": "1\n"
},
{
"input": "001\n",
"output": "0\n"
},
{
"input": "1010\n",
"output": "0\n"
},
{
"input": "1000\n",
"output": "2\n"
},
{
"input": "0100101110\n",
"output": "16\n"
},
{
"input": "1011\n",
"output": "0\n"
},
{
"input": "0011\n",
"output": "0\n"
},
{
"input": "01\n",
"output": "0\n"
},
{
"input": "1001\n",
"output": "0\n"
},
{
"input": "0001\n",
"output": "2\n"
}
] | code_contests | python | 0.1 | 7ff0b742e2cf9e948991c0836628d789 |
Let's denote correct match equation (we will denote it as CME) an equation a + b = c there all integers a, b and c are greater than zero.
For example, equations 2 + 2 = 4 (||+||=||||) and 1 + 2 = 3 (|+||=|||) are CME but equations 1 + 2 = 4 (|+||=||||), 2 + 2 = 3 (||+||=|||), and 0 + 1 = 1 (+|=|) are not.
Now, you have n matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!
For example, if n = 2, you can buy two matches and assemble |+|=||, and if n = 5 you can buy one match and assemble ||+|=|||.
<image>
Calculate the minimum number of matches which you have to buy for assembling CME.
Note, that you have to answer q independent queries.
Input
The first line contains one integer q (1 ≤ q ≤ 100) — the number of queries.
The only line of each query contains one integer n (2 ≤ n ≤ 10^9) — the number of matches.
Output
For each test case print one integer in single line — the minimum number of matches which you have to buy for assembling CME.
Example
Input
4
2
5
8
11
Output
2
1
0
1
Note
The first and second queries are explained in the statement.
In the third query, you can assemble 1 + 3 = 4 (|+|||=||||) without buying matches.
In the fourth query, buy one match and assemble 2 + 4 = 6 (||+||||=||||||).
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | # import numpy as np
import array
def solution():
req = int(input())
for i in range(req):
count = int(input())
result = 0
if count==2:
print(2)
continue
while count % 2 != 0 :
result+=1
count+=1
print(result)
def count_of_one(number):
result = 0
while number:
result += number % 2
number //= 2
return result
def to_mixed_frac(first, second):
while True:
for i in range(2, first + 1):
if first % i == 0 and second % i == 0:
first //= i
second //= i
break
else:
break
return str(first) + "/" + str(second)
def array_to_int(array):
for i in range(len(array)):
array[i] = int(array[i])
return array
def join0(array):
result = ""
for i in array:
result += str(i)
return result
def replace(string, new_ch, index):
new_str = ""
for i in range(len(string)):
if i == index:
new_str += new_ch
else:
new_str += string[i]
return new_str
solution()
# input-output by console
| python | code_algorithm | [
{
"input": "4\n2\n5\n8\n11\n",
"output": "2\n1\n0\n1\n"
},
{
"input": "100\n579\n568\n595\n573\n523\n543\n509\n510\n599\n525\n570\n563\n516\n598\n501\n586\n588\n524\n546\n531\n521\n561\n532\n550\n515\n530\n557\n503\n520\n558\n575\n596\n537\n553\n600\n582\n536\n533\n583\n554\n589\n519\n538\n528\n511\n581\n507\n518\n527\n587\n577\n542\n560\n584\n576\n517\n565\n548\n549\n506\n513\n578\n594\n567\n505\n555\n502\n559\n504\n566\n526\n597\n580\n564\n545\n529\n562\n539\n574\n541\n590\n551\n534\n512\n593\n572\n569\n544\n535\n508\n540\n556\n522\n571\n552\n585\n547\n592\n591\n514\n",
"output": "1\n0\n1\n1\n1\n1\n1\n0\n1\n1\n0\n1\n0\n0\n1\n0\n0\n0\n0\n1\n1\n1\n0\n0\n1\n0\n1\n1\n0\n0\n1\n0\n1\n1\n0\n0\n0\n1\n1\n0\n1\n1\n0\n0\n1\n1\n1\n0\n1\n1\n1\n0\n0\n0\n0\n1\n1\n0\n1\n0\n1\n0\n0\n1\n1\n1\n0\n1\n0\n0\n0\n1\n0\n0\n1\n1\n0\n1\n0\n1\n0\n1\n0\n0\n1\n0\n1\n0\n1\n0\n0\n0\n0\n1\n0\n1\n1\n0\n1\n0\n"
},
{
"input": "100\n699\n638\n619\n674\n692\n602\n698\n641\n651\n625\n640\n675\n671\n683\n685\n647\n681\n659\n613\n669\n680\n661\n633\n657\n630\n660\n615\n603\n650\n623\n665\n690\n627\n678\n667\n617\n644\n656\n654\n635\n620\n632\n634\n621\n679\n687\n639\n652\n695\n616\n649\n689\n626\n684\n612\n607\n662\n637\n642\n645\n618\n663\n629\n664\n672\n688\n673\n694\n606\n676\n655\n668\n686\n696\n624\n608\n670\n643\n611\n622\n682\n658\n697\n605\n610\n646\n628\n677\n653\n609\n691\n614\n666\n700\n601\n636\n631\n604\n648\n693\n",
"output": "1\n0\n1\n0\n0\n0\n0\n1\n1\n1\n0\n1\n1\n1\n1\n1\n1\n1\n1\n1\n0\n1\n1\n1\n0\n0\n1\n1\n0\n1\n1\n0\n1\n0\n1\n1\n0\n0\n0\n1\n0\n0\n0\n1\n1\n1\n1\n0\n1\n0\n1\n1\n0\n0\n0\n1\n0\n1\n0\n1\n0\n1\n1\n0\n0\n0\n1\n0\n0\n0\n1\n0\n0\n0\n0\n0\n0\n1\n1\n0\n0\n0\n1\n1\n0\n0\n0\n1\n1\n1\n1\n0\n0\n0\n1\n0\n1\n0\n0\n1\n"
},
{
"input": "100\n407\n464\n412\n454\n402\n436\n440\n485\n484\n452\n493\n403\n460\n470\n411\n465\n447\n413\n433\n499\n498\n492\n472\n426\n500\n467\n432\n404\n497\n457\n442\n494\n419\n477\n458\n410\n445\n441\n490\n476\n444\n471\n446\n486\n462\n487\n439\n438\n428\n418\n448\n495\n414\n409\n488\n443\n456\n430\n481\n425\n483\n478\n431\n421\n453\n455\n468\n429\n417\n416\n473\n491\n474\n434\n463\n469\n437\n423\n451\n449\n422\n475\n408\n427\n424\n480\n459\n450\n415\n401\n482\n466\n479\n435\n489\n420\n406\n496\n461\n405\n",
"output": "1\n0\n0\n0\n0\n0\n0\n1\n0\n0\n1\n1\n0\n0\n1\n1\n1\n1\n1\n1\n0\n0\n0\n0\n0\n1\n0\n0\n1\n1\n0\n0\n1\n1\n0\n0\n1\n1\n0\n0\n0\n1\n0\n0\n0\n1\n1\n0\n0\n0\n0\n1\n0\n1\n0\n1\n0\n0\n1\n1\n1\n0\n1\n1\n1\n1\n0\n1\n1\n0\n1\n1\n0\n0\n1\n1\n1\n1\n1\n1\n0\n1\n0\n1\n0\n0\n1\n0\n1\n1\n0\n0\n1\n1\n1\n0\n0\n0\n1\n1\n"
},
{
"input": "100\n755\n762\n730\n765\n781\n777\n760\n712\n776\n725\n746\n758\n752\n744\n784\n706\n729\n735\n714\n745\n796\n756\n724\n767\n704\n711\n788\n797\n728\n718\n736\n750\n771\n778\n779\n786\n715\n753\n719\n738\n732\n709\n716\n741\n782\n743\n723\n792\n793\n787\n731\n800\n766\n713\n703\n749\n720\n772\n769\n739\n722\n737\n770\n789\n702\n721\n761\n768\n775\n764\n759\n707\n773\n794\n783\n733\n774\n727\n717\n742\n757\n791\n785\n747\n751\n748\n780\n726\n799\n708\n705\n795\n763\n790\n701\n754\n734\n798\n710\n740\n",
"output": "1\n0\n0\n1\n1\n1\n0\n0\n0\n1\n0\n0\n0\n0\n0\n0\n1\n1\n0\n1\n0\n0\n0\n1\n0\n1\n0\n1\n0\n0\n0\n0\n1\n0\n1\n0\n1\n1\n1\n0\n0\n1\n0\n1\n0\n1\n1\n0\n1\n1\n1\n0\n0\n1\n1\n1\n0\n0\n1\n1\n0\n1\n0\n1\n0\n1\n1\n0\n1\n0\n1\n1\n1\n0\n1\n1\n0\n1\n1\n0\n1\n1\n1\n1\n1\n0\n0\n0\n1\n0\n1\n1\n1\n0\n1\n0\n0\n0\n0\n0\n"
},
{
"input": "100\n309\n369\n383\n368\n328\n388\n317\n384\n395\n343\n302\n337\n325\n340\n380\n322\n377\n323\n350\n361\n379\n301\n348\n311\n385\n394\n346\n393\n398\n372\n399\n360\n357\n366\n303\n363\n367\n362\n335\n358\n389\n365\n345\n344\n312\n356\n342\n310\n313\n387\n353\n347\n338\n304\n341\n319\n333\n336\n332\n330\n318\n378\n349\n373\n307\n355\n400\n320\n364\n352\n381\n306\n386\n396\n327\n324\n370\n351\n371\n375\n374\n391\n315\n397\n376\n305\n390\n321\n314\n316\n339\n359\n331\n382\n308\n354\n326\n334\n392\n329\n",
"output": "1\n1\n1\n0\n0\n0\n1\n0\n1\n1\n0\n1\n1\n0\n0\n0\n1\n1\n0\n1\n1\n1\n0\n1\n1\n0\n0\n1\n0\n0\n1\n0\n1\n0\n1\n1\n1\n0\n1\n0\n1\n1\n1\n0\n0\n0\n0\n0\n1\n1\n1\n1\n0\n0\n1\n1\n1\n0\n0\n0\n0\n0\n1\n1\n1\n1\n0\n0\n0\n0\n1\n0\n0\n0\n1\n0\n0\n1\n1\n1\n0\n1\n1\n1\n0\n1\n0\n1\n0\n0\n1\n1\n1\n0\n0\n0\n0\n0\n0\n1\n"
},
{
"input": "100\n127\n145\n190\n164\n131\n168\n123\n132\n138\n140\n167\n195\n121\n143\n182\n136\n148\n189\n171\n105\n196\n169\n170\n135\n102\n162\n139\n126\n146\n165\n197\n181\n130\n103\n107\n125\n118\n180\n157\n179\n106\n128\n198\n110\n104\n137\n184\n116\n141\n199\n187\n133\n186\n115\n129\n114\n147\n166\n172\n120\n160\n163\n191\n193\n152\n112\n174\n173\n108\n176\n101\n159\n122\n150\n175\n111\n134\n119\n154\n156\n124\n153\n188\n194\n161\n151\n109\n192\n185\n144\n177\n158\n113\n117\n178\n149\n142\n200\n183\n155\n",
"output": "1\n1\n0\n0\n1\n0\n1\n0\n0\n0\n1\n1\n1\n1\n0\n0\n0\n1\n1\n1\n0\n1\n0\n1\n0\n0\n1\n0\n0\n1\n1\n1\n0\n1\n1\n1\n0\n0\n1\n1\n0\n0\n0\n0\n0\n1\n0\n0\n1\n1\n1\n1\n0\n1\n1\n0\n1\n0\n0\n0\n0\n1\n1\n1\n0\n0\n0\n1\n0\n0\n1\n1\n0\n0\n1\n1\n0\n1\n0\n0\n0\n1\n0\n0\n1\n1\n1\n0\n1\n0\n1\n0\n1\n1\n0\n1\n0\n0\n1\n1\n"
},
{
"input": "100\n837\n857\n855\n854\n826\n873\n886\n874\n882\n844\n900\n801\n806\n871\n847\n884\n824\n891\n817\n823\n804\n877\n885\n876\n879\n890\n822\n875\n849\n843\n818\n896\n802\n850\n878\n863\n809\n832\n892\n860\n812\n803\n895\n819\n865\n810\n811\n859\n815\n883\n825\n872\n820\n887\n853\n852\n830\n851\n808\n870\n828\n867\n868\n821\n840\n834\n856\n869\n864\n897\n845\n831\n881\n848\n889\n888\n836\n805\n899\n835\n862\n838\n829\n807\n861\n858\n842\n816\n846\n898\n841\n833\n814\n827\n866\n893\n813\n839\n880\n894\n",
"output": "1\n1\n1\n0\n0\n1\n0\n0\n0\n0\n0\n1\n0\n1\n1\n0\n0\n1\n1\n1\n0\n1\n1\n0\n1\n0\n0\n1\n1\n1\n0\n0\n0\n0\n0\n1\n1\n0\n0\n0\n0\n1\n1\n1\n1\n0\n1\n1\n1\n1\n1\n0\n0\n1\n1\n0\n0\n1\n0\n0\n0\n1\n0\n1\n0\n0\n0\n1\n0\n1\n1\n1\n1\n0\n1\n0\n0\n1\n1\n1\n0\n0\n1\n1\n1\n0\n0\n0\n0\n0\n1\n1\n0\n1\n0\n1\n1\n1\n0\n0\n"
},
{
"input": "100\n273\n231\n265\n248\n246\n202\n239\n300\n268\n276\n208\n240\n225\n250\n259\n233\n218\n226\n281\n270\n227\n256\n252\n286\n207\n217\n229\n283\n249\n289\n279\n262\n253\n214\n213\n298\n234\n232\n254\n271\n299\n245\n216\n215\n293\n266\n287\n284\n263\n203\n223\n272\n243\n247\n212\n222\n275\n295\n260\n219\n269\n282\n230\n296\n264\n267\n280\n211\n274\n261\n251\n236\n297\n209\n205\n221\n277\n255\n204\n292\n235\n278\n241\n238\n285\n244\n220\n258\n294\n288\n291\n210\n290\n228\n224\n242\n206\n257\n237\n201\n",
"output": "1\n1\n1\n0\n0\n0\n1\n0\n0\n0\n0\n0\n1\n0\n1\n1\n0\n0\n1\n0\n1\n0\n0\n0\n1\n1\n1\n1\n1\n1\n1\n0\n1\n0\n1\n0\n0\n0\n0\n1\n1\n1\n0\n1\n1\n0\n1\n0\n1\n1\n1\n0\n1\n1\n0\n0\n1\n1\n0\n1\n1\n0\n0\n0\n0\n1\n0\n1\n0\n1\n1\n0\n1\n1\n1\n1\n1\n1\n0\n0\n1\n0\n1\n0\n1\n0\n0\n0\n0\n0\n1\n0\n0\n0\n0\n0\n0\n1\n1\n1\n"
},
{
"input": "100\n928\n970\n950\n921\n945\n974\n941\n999\n964\n920\n926\n942\n934\n918\n930\n901\n977\n961\n978\n962\n951\n909\n994\n953\n946\n993\n982\n922\n933\n932\n904\n910\n991\n937\n963\n988\n983\n903\n956\n976\n967\n958\n902\n986\n960\n965\n940\n914\n955\n987\n952\n971\n939\n966\n938\n911\n916\n954\n929\n935\n947\n1000\n917\n979\n989\n968\n943\n969\n973\n975\n980\n957\n912\n936\n984\n981\n949\n948\n913\n995\n906\n959\n944\n915\n996\n905\n907\n972\n985\n997\n908\n931\n927\n919\n992\n923\n925\n924\n998\n990\n",
"output": "0\n0\n0\n1\n1\n0\n1\n1\n0\n0\n0\n0\n0\n0\n0\n1\n1\n1\n0\n0\n1\n1\n0\n1\n0\n1\n0\n0\n1\n0\n0\n0\n1\n1\n1\n0\n1\n1\n0\n0\n1\n0\n0\n0\n0\n1\n0\n0\n1\n1\n0\n1\n1\n0\n0\n1\n0\n0\n1\n1\n1\n0\n1\n1\n1\n0\n1\n1\n1\n1\n0\n1\n0\n0\n0\n1\n1\n0\n1\n1\n0\n1\n0\n1\n0\n1\n1\n0\n1\n1\n0\n1\n1\n1\n0\n1\n1\n0\n0\n0\n"
},
{
"input": "100\n101\n60\n16\n78\n37\n22\n24\n63\n65\n57\n31\n46\n62\n50\n40\n79\n93\n97\n41\n64\n52\n30\n48\n95\n44\n9\n33\n15\n4\n59\n69\n56\n67\n73\n87\n17\n71\n23\n55\n18\n43\n80\n94\n58\n26\n90\n6\n96\n27\n10\n45\n75\n72\n82\n11\n3\n5\n85\n2\n14\n53\n36\n39\n99\n89\n20\n81\n61\n12\n29\n8\n70\n42\n66\n91\n77\n49\n35\n92\n84\n38\n21\n76\n74\n86\n32\n100\n28\n51\n13\n7\n34\n68\n47\n19\n83\n54\n88\n25\n98\n",
"output": "1\n0\n0\n0\n1\n0\n0\n1\n1\n1\n1\n0\n0\n0\n0\n1\n1\n1\n1\n0\n0\n0\n0\n1\n0\n1\n1\n1\n0\n1\n1\n0\n1\n1\n1\n1\n1\n1\n1\n0\n1\n0\n0\n0\n0\n0\n0\n0\n1\n0\n1\n1\n0\n0\n1\n1\n1\n1\n2\n0\n1\n0\n1\n1\n1\n0\n1\n1\n0\n1\n0\n0\n0\n0\n1\n1\n1\n1\n0\n0\n0\n1\n0\n0\n0\n0\n0\n0\n1\n1\n1\n0\n0\n1\n1\n1\n0\n0\n1\n0\n"
}
] | code_contests | python | 0.2 | b090435dc70fcc710c51214d0e8d075d |
Balph is learning to play a game called Buma. In this game, he is given a row of colored balls. He has to choose the color of one new ball and the place to insert it (between two balls, or to the left of all the balls, or to the right of all the balls).
When the ball is inserted the following happens repeatedly: if some segment of balls of the same color became longer as a result of a previous action and its length became at least 3, then all the balls of this segment are eliminated.
Consider, for example, a row of balls 'AAABBBWWBB'. Suppose Balph chooses a ball of color 'W' and the place to insert it after the sixth ball, i. e. to the left of the two 'W's. After Balph inserts this ball, the balls of color 'W' are eliminated, since this segment was made longer and has length 3 now, so the row becomes 'AAABBBBB'. The balls of color 'B' are eliminated now, because the segment of balls of color 'B' became longer and has length 5 now. Thus, the row becomes 'AAA'. However, none of the balls are eliminated now, because there is no elongated segment.
Help Balph count the number of possible ways to choose a color of a new ball and a place to insert it that leads to the elimination of all the balls.
Input
The only line contains a non-empty string of uppercase English letters of length at most 3 ⋅ 10^5. Each letter represents a ball with the corresponding color.
Output
Output the number of ways to choose a color and a position of a new ball in order to eliminate all the balls.
Examples
Input
BBWWBB
Output
3
Input
BWWB
Output
0
Input
BBWBB
Output
0
Input
OOOWWW
Output
0
Input
WWWOOOOOOWWW
Output
7
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | # -*- coding: utf-8 -*-
"""
Created on Sat Jul 18 16:25:32 2020
@author: MridulSachdeva
"""
s = input()
n = len(s)
condensed = []
temp = s[0]
count = 1
for i in range(1, n):
if s[i] == temp:
count += 1
else:
condensed.append((temp, count))
temp = s[i]
count = 1
condensed.append((temp, count))
if len(condensed) % 2 == 0:
print(0)
elif condensed[int((len(condensed) - 1) / 2)][1] < 2:
print(0)
else:
for i in range(int((len(condensed) - 1) / 2)):
if condensed[i][0] != condensed[-i - 1][0]:
print(0)
break
elif condensed[i][1] + condensed[-i - 1][1] < 3:
print(0)
break
else:
print(condensed[int((len(condensed) - 1) / 2)][1] + 1) | python | code_algorithm | [
{
"input": "OOOWWW\n",
"output": "0\n"
},
{
"input": "BBWBB\n",
"output": "0\n"
},
{
"input": "BBWWBB\n",
"output": "3\n"
},
{
"input": "BWWB\n",
"output": "0\n"
},
{
"input": "WWWOOOOOOWWW\n",
"output": "7\n"
},
{
"input": "AA\n",
"output": "3\n"
},
{
"input": "A\n",
"output": "0\n"
},
{
"input": "ABCDEF\n",
"output": "0\n"
}
] | code_contests | python | 0 | 8d7af7239506a0cbefe119cd58e960dc |
A connected undirected graph is called a vertex cactus, if each vertex of this graph belongs to at most one simple cycle.
A simple cycle in a undirected graph is a sequence of distinct vertices v1, v2, ..., vt (t > 2), such that for any i (1 ≤ i < t) exists an edge between vertices vi and vi + 1, and also exists an edge between vertices v1 and vt.
A simple path in a undirected graph is a sequence of not necessarily distinct vertices v1, v2, ..., vt (t > 0), such that for any i (1 ≤ i < t) exists an edge between vertices vi and vi + 1 and furthermore each edge occurs no more than once. We'll say that a simple path v1, v2, ..., vt starts at vertex v1 and ends at vertex vt.
You've got a graph consisting of n vertices and m edges, that is a vertex cactus. Also, you've got a list of k pairs of interesting vertices xi, yi, for which you want to know the following information — the number of distinct simple paths that start at vertex xi and end at vertex yi. We will consider two simple paths distinct if the sets of edges of the paths are distinct.
For each pair of interesting vertices count the number of distinct simple paths between them. As this number can be rather large, you should calculate it modulo 1000000007 (109 + 7).
Input
The first line contains two space-separated integers n, m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105) — the number of vertices and edges in the graph, correspondingly. Next m lines contain the description of the edges: the i-th line contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ n) — the indexes of the vertices connected by the i-th edge.
The next line contains a single integer k (1 ≤ k ≤ 105) — the number of pairs of interesting vertices. Next k lines contain the list of pairs of interesting vertices: the i-th line contains two space-separated numbers xi, yi (1 ≤ xi, yi ≤ n; xi ≠ yi) — the indexes of interesting vertices in the i-th pair.
It is guaranteed that the given graph is a vertex cactus. It is guaranteed that the graph contains no loops or multiple edges. Consider the graph vertices are numbered from 1 to n.
Output
Print k lines: in the i-th line print a single integer — the number of distinct simple ways, starting at xi and ending at yi, modulo 1000000007 (109 + 7).
Examples
Input
10 11
1 2
2 3
3 4
1 4
3 5
5 6
8 6
8 7
7 6
7 9
9 10
6
1 2
3 5
6 9
9 2
9 3
9 10
Output
2
2
2
4
4
1
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from collections import defaultdict
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
sys.setrecursionlimit(3*10**4)
def bootstrap(f, stack=[]):
def wrappedfunc(*args, **kwargs):
if stack:
return f(*args, **kwargs)
else:
to = f(*args, **kwargs)
while True:
if type(to) is GeneratorType:
stack.append(to)
to = next(to)
else:
stack.pop()
if not stack:
break
to = stack[-1].send(to)
return to
return wrappedfunc
@bootstrap
def dfs1(cur,prev,v):
depth[cur] = depth[prev] + 1
parent[cur][0] = prev
blacks[cur]=v
if val[cur]:
blacks[cur]+=1
for i in tree[cur]:
if (i != prev):
yield dfs1(i, cur,blacks[cur])
yield
def precomputeSparseMatrix(n):
for i in range(1,level):
for node in range(1,n+1):
if (parent[node][i-1] != -1):
parent[node][i] =parent[parent[node][i-1]][i-1]
def lca(u,v):
if (depth[v] < depth[u]):
u,v=v,u
diff = depth[v] - depth[u]
for i in range(level):
if ((diff >> i) & 1):
v = parent[v][i]
if (u == v):
return u
i=level-1
while(i>=0):
if (parent[u][i] != parent[v][i]):
u = parent[u][i]
v = parent[v][i]
i+=-1
return parent[u][0]
@bootstrap
def dfs(u,p):
global curr
for j in adj[u]:
if j!=p:
if id[j]==0:
id[j]=id[u]+1
yield dfs(j,u)
elif id[u]>id[j]:
up[u]=curr
down[j]=curr
curr+=1
yield
@bootstrap
def dfs2(u,p):
vis[u]=1
for j in adj[u]:
if not vis[j]:
yield dfs2(j,u)
if up[u]:
id[u]=up[u]
else:
id[u]=u
for j in adj[u]:
if j!=p:
if id[j]!=j and down[j]==0:
id[u]=id[j]
yield
n,m=map(int,input().split())
adj=[[] for i in range(n+1)]
edges=[]
for j in range(m):
u,v=map(int,input().split())
edges.append([u,v])
adj[u].append(v)
adj[v].append(u)
up=defaultdict(lambda:0)
down=defaultdict(lambda:0)
curr=n+1
id=[]
vis=[]
val=[]
tree=[]
depth=[]
for j in range(n+1):
id.append(0)
vis.append(0)
val.append(0)
tree.append([])
depth.append(0)
id[1]=1
dfs(1,0)
dfs2(1,0)
res=sorted(list(set(id[1:])))
up=defaultdict(lambda:0)
l=len(res)
for j in range(l):
up[res[j]]=j+1
d=defaultdict(lambda:0)
for j in range(1,n+1):
id[j]=up[id[j]]
d[id[j]]+=1
if d[id[j]]>1:
val[id[j]]=1
level=17
parent=[[0 for i in range(level)] for j in range(l+1)]
blacks=[0]*(l+1)
d=defaultdict(lambda:0)
for j in edges:
u,v=j[0],j[1]
p,q=id[u],id[v]
if not d[p,q] and p!=q:
tree[p].append(q)
tree[q].append(p)
d[p,q]=1
d[q,p]=1
dfs1(1,0,0)
precomputeSparseMatrix(l)
k=int(input())
mod=10**9+7
value=[1]
for j in range(1,l+1):
value.append((2*value[-1])%(mod))
for j in range(k):
a,b=map(int,input().split())
u1,v1=id[a],id[b]
lc=lca(u1,v1)
res=value[blacks[u1]+blacks[v1]-2*blacks[lc]]
if val[lc]:
res*=2
print(res%mod) | python | code_algorithm | [
{
"input": "10 11\n1 2\n2 3\n3 4\n1 4\n3 5\n5 6\n8 6\n8 7\n7 6\n7 9\n9 10\n6\n1 2\n3 5\n6 9\n9 2\n9 3\n9 10\n",
"output": "2\n2\n2\n4\n4\n1\n"
},
{
"input": "2 1\n1 2\n3\n1 2\n1 2\n2 1\n",
"output": "1\n1\n1\n"
},
{
"input": "5 4\n1 3\n2 3\n4 3\n1 5\n3\n1 3\n2 4\n5 2\n",
"output": "1\n1\n1\n"
},
{
"input": "6 6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 1\n4\n1 2\n1 6\n6 5\n4 3\n",
"output": "2\n2\n2\n2\n"
},
{
"input": "40 43\n1 2\n1 3\n1 4\n4 5\n5 6\n1 7\n5 8\n7 9\n2 10\n10 11\n11 12\n2 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 2\n3 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 3\n4 33\n33 34\n34 35\n35 36\n36 37\n37 4\n5 38\n38 39\n39 40\n40 5\n25\n6 24\n31 14\n40 34\n17 39\n10 37\n38 9\n40 26\n12 35\n28 40\n5 23\n14 20\n37 8\n14 23\n8 5\n22 21\n31 22\n26 9\n19 1\n5 36\n10 11\n38 11\n32 18\n25 14\n12 27\n34 39\n",
"output": "8\n4\n4\n8\n4\n4\n8\n4\n8\n8\n2\n4\n2\n2\n2\n4\n2\n2\n4\n1\n8\n4\n2\n4\n4\n"
},
{
"input": "10 11\n1 2\n2 3\n3 4\n1 5\n5 6\n6 7\n7 8\n8 1\n2 9\n9 10\n10 2\n13\n2 8\n4 7\n2 9\n3 2\n9 2\n10 8\n8 3\n9 5\n7 9\n6 7\n9 5\n9 5\n9 10\n",
"output": "4\n4\n2\n2\n2\n4\n4\n4\n4\n2\n4\n4\n2\n"
},
{
"input": "14 16\n1 14\n1 2\n2 3\n3 4\n4 5\n5 1\n6 5\n10 9\n9 12\n11 12\n11 10\n7 9\n8 7\n8 13\n6 8\n7 6\n10\n14 1\n14 12\n10 12\n7 9\n7 5\n9 5\n1 6\n13 8\n13 11\n1 13\n",
"output": "2\n8\n2\n4\n4\n8\n4\n2\n4\n4\n"
},
{
"input": "27 29\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 2\n2 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 12\n12 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 19\n19 27\n20\n6 26\n9 4\n22 18\n4 16\n12 18\n20 4\n18 3\n13 17\n19 7\n5 8\n20 24\n27 20\n2 19\n14 16\n22 26\n15 1\n15 4\n24 7\n14 13\n21 3\n",
"output": "8\n2\n4\n4\n2\n8\n4\n2\n8\n2\n2\n2\n8\n2\n2\n4\n4\n8\n2\n8\n"
},
{
"input": "5 5\n1 2\n2 3\n3 4\n4 2\n2 5\n5\n1 5\n5 1\n2 5\n4 2\n4 1\n",
"output": "2\n2\n2\n2\n2\n"
},
{
"input": "20 22\n1 2\n1 3\n2 4\n1 5\n5 6\n4 7\n1 8\n3 9\n1 10\n10 11\n11 12\n12 13\n13 1\n3 14\n14 15\n15 16\n16 17\n17 18\n18 3\n5 19\n19 20\n20 5\n20\n6 4\n16 1\n10 19\n20 7\n6 17\n16 7\n9 11\n3 15\n20 2\n13 18\n8 13\n8 9\n16 18\n7 14\n6 15\n20 9\n15 2\n19 8\n1 11\n14 1\n",
"output": "4\n4\n4\n4\n8\n4\n4\n2\n4\n4\n2\n4\n2\n4\n8\n8\n4\n4\n2\n4\n"
},
{
"input": "20 22\n1 2\n2 3\n3 4\n4 5\n5 6\n6 2\n2 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 7\n7 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 13\n13 20\n20\n3 17\n14 9\n12 20\n11 20\n11 1\n5 10\n17 6\n19 3\n17 11\n3 19\n8 15\n16 1\n9 16\n13 3\n18 14\n14 5\n19 5\n12 2\n16 19\n9 10\n",
"output": "8\n4\n4\n4\n4\n4\n8\n8\n4\n8\n4\n8\n4\n8\n2\n8\n8\n4\n2\n2\n"
},
{
"input": "12 13\n1 2\n3 2\n4 3\n2 6\n6 5\n5 3\n8 7\n7 6\n9 8\n8 10\n11 10\n12 11\n12 8\n11\n1 4\n1 3\n2 3\n2 7\n7 8\n6 8\n9 11\n11 4\n10 1\n12 5\n4 8\n",
"output": "2\n2\n2\n2\n2\n4\n2\n4\n4\n4\n4\n"
},
{
"input": "9 9\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 3\n3 9\n10\n3 1\n8 9\n7 3\n9 6\n6 8\n5 7\n5 9\n9 6\n1 3\n2 7\n",
"output": "2\n2\n2\n2\n2\n2\n2\n2\n2\n2\n"
}
] | code_contests | python | 0 | 3382e986a6d5e225f3ad098ce4856e19 |
Vasya has found a piece of paper with an array written on it. The array consists of n integers a1, a2, ..., an. Vasya noticed that the following condition holds for the array ai ≤ ai + 1 ≤ 2·ai for any positive integer i (i < n).
Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression s meets the limits 0 ≤ s ≤ a1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists.
Input
The first line contains integer n (1 ≤ n ≤ 105) — the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the original array.
It is guaranteed that the condition ai ≤ ai + 1 ≤ 2·ai fulfills for any positive integer i (i < n).
Output
In a single line print the sequence of n characters "+" and "-", where the i-th character is the sign that is placed in front of number ai. The value of the resulting expression s must fit into the limits 0 ≤ s ≤ a1. If there are multiple solutions, you are allowed to print any of them.
Examples
Input
4
1 2 3 5
Output
+++-
Input
3
3 3 5
Output
++-
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | #!/usr/bin/python3
n = int(input())
a = list(map(int, input().split()))
s = a[-1]
ans = ['+']
for v in reversed(a[:-1]):
if s > 0:
s -= v
ans.append('-')
else:
s += v
ans.append('+')
if 0 <= s <= a[-1]:
print(''.join(reversed(ans)))
else:
s = -a[-1]
ans = ['-']
for v in reversed(a[:-1]):
if s > 0:
s -= v
ans.append('-')
else:
s += v
ans.append('+')
if 0 <= s <= a[-1]:
print(''.join(reversed(ans)))
else:
s = a[-1]
ans = ['+']
for v in reversed(a[:-1]):
if s >= 0:
s -= v
ans.append('-')
else:
s += v
ans.append('+')
if 0 <= s <= a[-1]:
print(''.join(reversed(ans)))
else:
s = -a[-1]
ans = ['-']
for v in reversed(a[:-1]):
if s >= 0:
s -= v
ans.append('-')
else:
s += v
ans.append('+')
print(''.join(reversed(ans)))
| python | code_algorithm | [
{
"input": "3\n3 3 5\n",
"output": "++-\n"
},
{
"input": "4\n1 2 3 5\n",
"output": "+--+\n"
},
{
"input": "42\n2 2 2 3 6 8 14 22 37 70 128 232 330 472 473 784 1481 2008 3076 4031 7504 8070 8167 11954 17832 24889 27113 41190 48727 92327 148544 186992 247329 370301 547840 621571 868209 1158781 1725242 3027208 4788036 5166155\n",
"output": "-+-+-+--++-+-+-+-+-+-+-+-++--+-+--++-++--+\n"
},
{
"input": "44\n4 6 8 14 28 36 43 76 78 151 184 217 228 245 469 686 932 1279 2100 2373 4006 4368 8173 10054 18409 28333 32174 53029 90283 161047 293191 479853 875055 1206876 1423386 1878171 2601579 3319570 4571631 4999760 6742654 12515994 22557290 29338426\n",
"output": "+-+-+-+--++-+-+-++--+-+--+--+++--++--+-+-++-\n"
},
{
"input": "6\n3 5 10 11 12 20\n",
"output": "++-++-\n"
},
{
"input": "3\n1000000000 1000000000 1000000000\n",
"output": "+-+\n"
},
{
"input": "11\n3 6 7 11 13 16 26 52 63 97 97\n",
"output": "+-++-+--+-+\n"
},
{
"input": "48\n4 7 12 16 23 43 61 112 134 141 243 267 484 890 1427 1558 1653 2263 2889 3313 3730 5991 10176 18243 18685 36555 40006 62099 70557 106602 122641 125854 213236 309698 379653 713328 999577 1021356 2007207 2886237 4994645 5812125 11576387 14215887 26060277 35989707 36964781 57933366\n",
"output": "++-++-+-++---+-+-+-+--+--+-+-+-+-+-+--+-++-+-++-\n"
},
{
"input": "43\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
"output": "+++++++++++++++++++++++++++++++++++++++++++\n"
},
{
"input": "40\n3 3 3 6 10 10 18 19 34 66 107 150 191 286 346 661 1061 1620 2123 3679 5030 8736 10539 19659 38608 47853 53095 71391 135905 255214 384015 694921 1357571 1364832 2046644 2595866 2918203 3547173 4880025 6274651\n",
"output": "+-++-+-+-+-++-++-+-++--++--++--+-+-+-++-\n"
},
{
"input": "4\n2 4 5 6\n",
"output": "-++-\n"
},
{
"input": "41\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
"output": "+++++++++++++++++++++++++++++++++++++++++\n"
},
{
"input": "45\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n",
"output": "+++++++++++++++++++++++++++++++++++++++++++++\n"
},
{
"input": "46\n3 6 6 8 16 19 23 46 53 90 114 131 199 361 366 523 579 1081 1457 2843 4112 4766 7187 8511 15905 22537 39546 70064 125921 214041 324358 392931 547572 954380 1012122 1057632 1150405 1393895 1915284 1969248 2541748 4451203 8201302 10912223 17210988 24485089\n",
"output": "-+++-+--+-++-+--++-+-+-++-++-+--++--++--++-++-\n"
},
{
"input": "2\n5 8\n",
"output": "-+\n"
},
{
"input": "1\n1000000000\n",
"output": "+\n"
},
{
"input": "12\n3 3 4 7 14 26 51 65 72 72 85 92\n",
"output": "+-+--++-+--+\n"
},
{
"input": "10\n10 14 17 22 43 72 74 84 88 93\n",
"output": "++---++--+\n"
},
{
"input": "47\n3 3 5 6 9 13 13 14 22 33 50 76 83 100 168 303 604 1074 1417 2667 3077 4821 5129 7355 11671 22342 24237 34014 66395 73366 105385 205561 387155 756780 965476 1424160 1624526 2701046 4747339 5448855 6467013 9133423 11001389 18298303 23824100 41393164 58364321\n",
"output": "-+-++-+-+-+-+-+-+--+-+--+-++-++++--+-+-++--+--+\n"
}
] | code_contests | python | 0 | 3c5f9a3f6fa4373e2f0d28a7e89b796d |
A star map in Berland is a checked field n × m squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer x (radius of the constellation) the following is true:
* the 2nd is on the same vertical line as the 1st, but x squares up
* the 3rd is on the same vertical line as the 1st, but x squares down
* the 4th is on the same horizontal line as the 1st, but x squares left
* the 5th is on the same horizontal line as the 1st, but x squares right
Such constellations can be very numerous, that's why they are numbered with integers from 1 on the following principle: when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one.
Your task is to find the constellation with index k by the given Berland's star map.
Input
The first line contains three integers n, m and k (1 ≤ n, m ≤ 300, 1 ≤ k ≤ 3·107) — height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1, 1), and the lower-right — (n, m). Then there follow n lines, m characters each — description of the map. j-th character in i-th line is «*», if there is a star in the corresponding square, and «.» if this square is empty.
Output
If the number of the constellations is less than k, output -1. Otherwise output 5 lines, two integers each — coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right.
Examples
Input
5 6 1
....*.
...***
....*.
..*...
.***..
Output
2 5
1 5
3 5
2 4
2 6
Input
5 6 2
....*.
...***
....*.
..*...
.***..
Output
-1
Input
7 7 2
...*...
.......
...*...
*.***.*
...*...
.......
...*...
Output
4 4
1 4
7 4
4 1
4 7
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m, k = map(int, input().split())
a = [tuple(map(lambda c: c == '*', input().rstrip())) for _ in range(n)]
cnt = [0] * 400
for i in range(1, n - 1):
for j in range(1, m - 1):
if not a[i][j]:
continue
for rad, ui, di, lj, rj in zip(range(1, 400), range(i - 1, -1, -1), range(i + 1, n), range(j - 1, -1, -1), range(j + 1, m)):
if all((a[ui][j], a[di][j], a[i][lj], a[i][rj])):
cnt[rad] += 1
rad = -1
for i in range(300):
cnt[i + 1] += cnt[i]
if cnt[i] >= k:
rad = i
k -= cnt[i - 1]
break
else:
print(-1)
exit()
for i in range(rad, n - rad):
for j in range(rad, m - rad):
if all((a[i][j], a[i - rad][j], a[i + rad][j], a[i][j - rad], a[i][j + rad])):
k -= 1
if k == 0:
print(f'{i+1} {j+1}\n{i-rad+1} {j+1}\n{i+rad+1} {j+1}\n{i+1} {j-rad+1}\n{i+1} {j+rad+1}')
exit()
| python | code_algorithm | [
{
"input": "5 6 1\n....*.\n...***\n....*.\n..*...\n.***..\n",
"output": "2 5\n1 5\n3 5\n2 4\n2 6\n"
},
{
"input": "7 7 2\n...*...\n.......\n...*...\n*.***.*\n...*...\n.......\n...*...\n",
"output": "4 4\n1 4\n7 4\n4 1\n4 7\n"
},
{
"input": "5 6 2\n....*.\n...***\n....*.\n..*...\n.***..\n",
"output": "-1\n"
},
{
"input": "5 5 3\n*.***\n.****\n..***\n*.***\n.**.*\n",
"output": "3 4\n2 4\n4 4\n3 3\n3 5\n"
},
{
"input": "5 5 1\n.....\n.....\n.*..*\n*.*..\n....*\n",
"output": "-1\n"
},
{
"input": "10 10 40\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n",
"output": "6 9\n5 9\n7 9\n6 8\n6 10\n"
},
{
"input": "10 10 6\n.*..**.**.\n**********\n****.*****\n**.***....\n***.******\n****.**..*\n**.**.****\n****.**..*\n..**.**.*.\n.*.*..***.\n",
"output": "5 2\n4 2\n6 2\n5 1\n5 3\n"
},
{
"input": "10 10 59\n**.*******\n********.*\n***.******\n**.*******\n*******.**\n**********\n**.**.****\n.*********\n*****.****\n*********.\n",
"output": "6 5\n3 5\n9 5\n6 2\n6 8\n"
}
] | code_contests | python | 0.7 | 656a5bb2f15f013babe638b513061a08 |
Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it?
The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out the name of the company on a piece of paper in a line (horizontally, from left to right) with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper.
There are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not.
Input
The first line contains a non-empty name that needs to be checked. The name contains at most 105 large English letters. The name will be written with the next sans serif font:
<image>
Output
Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes).
Examples
Input
AHA
Output
YES
Input
Z
Output
NO
Input
XO
Output
NO
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | '''input
XO
'''
s = input()
if all(s[x] == s[~x] for x in range(len(s)//2)):
if any(y not in "AHIMOTUVWXY" for y in s[:len(s)//2+1]):
print("NO")
else:
print("YES")
else:
print("NO") | python | code_algorithm | [
{
"input": "AHA\n",
"output": "YES\n"
},
{
"input": "Z\n",
"output": "NO\n"
},
{
"input": "XO\n",
"output": "NO\n"
},
{
"input": "HNCMEEMCNH\n",
"output": "NO\n"
},
{
"input": "AABAA\n",
"output": "NO\n"
},
{
"input": "NNN\n",
"output": "NO\n"
},
{
"input": "WWS\n",
"output": "NO\n"
},
{
"input": "X\n",
"output": "YES\n"
},
{
"input": "D\n",
"output": "NO\n"
},
{
"input": "AAAKTAAA\n",
"output": "NO\n"
},
{
"input": "QOQ\n",
"output": "NO\n"
},
{
"input": "G\n",
"output": "NO\n"
},
{
"input": "AHHA\n",
"output": "YES\n"
},
{
"input": "WYYW\n",
"output": "YES\n"
},
{
"input": "SSS\n",
"output": "NO\n"
},
{
"input": "JL\n",
"output": "NO\n"
},
{
"input": "H\n",
"output": "YES\n"
},
{
"input": "ZZ\n",
"output": "NO\n"
},
{
"input": "VO\n",
"output": "NO\n"
},
{
"input": "VIYMAXXAVM\n",
"output": "NO\n"
},
{
"input": "C\n",
"output": "NO\n"
},
{
"input": "AZA\n",
"output": "NO\n"
},
{
"input": "L\n",
"output": "NO\n"
},
{
"input": "BAB\n",
"output": "NO\n"
},
{
"input": "I\n",
"output": "YES\n"
},
{
"input": "OVWIHIWVYXMVAAAATOXWOIUUHYXHIHHVUIOOXWHOXTUUMUUVHVWWYUTIAUAITAOMHXWMTTOIVMIVOTHOVOIOHYHAOXWAUVWAVIVM\n",
"output": "NO\n"
},
{
"input": "B\n",
"output": "NO\n"
},
{
"input": "AAJAA\n",
"output": "NO\n"
},
{
"input": "F\n",
"output": "NO\n"
},
{
"input": "T\n",
"output": "YES\n"
},
{
"input": "AAAAAABAAAAAA\n",
"output": "NO\n"
},
{
"input": "UUU\n",
"output": "YES\n"
},
{
"input": "OQQQO\n",
"output": "NO\n"
},
{
"input": "SS\n",
"output": "NO\n"
},
{
"input": "E\n",
"output": "NO\n"
},
{
"input": "M\n",
"output": "YES\n"
},
{
"input": "ABA\n",
"output": "NO\n"
},
{
"input": "AKA\n",
"output": "NO\n"
},
{
"input": "N\n",
"output": "NO\n"
},
{
"input": "V\n",
"output": "YES\n"
},
{
"input": "P\n",
"output": "NO\n"
},
{
"input": "W\n",
"output": "YES\n"
},
{
"input": "QDPINBMCRFWXPDBFGOZVVOCEMJRUCTOADEWEGTVBVBFWWRPGYEEYGPRWWFBVBVTGEWEDAOTCURJMECOVVZOGFBDPXWFRCMBNIPDQ\n",
"output": "NO\n"
},
{
"input": "YYHUIUGYI\n",
"output": "NO\n"
},
{
"input": "ADA\n",
"output": "NO\n"
},
{
"input": "R\n",
"output": "NO\n"
},
{
"input": "A\n",
"output": "YES\n"
},
{
"input": "LAL\n",
"output": "NO\n"
},
{
"input": "J\n",
"output": "NO\n"
},
{
"input": "AEEA\n",
"output": "NO\n"
},
{
"input": "TT\n",
"output": "YES\n"
},
{
"input": "Y\n",
"output": "YES\n"
},
{
"input": "MITIM\n",
"output": "YES\n"
},
{
"input": "AAA\n",
"output": "YES\n"
},
{
"input": "CC\n",
"output": "NO\n"
},
{
"input": "K\n",
"output": "NO\n"
},
{
"input": "O\n",
"output": "YES\n"
},
{
"input": "S\n",
"output": "NO\n"
},
{
"input": "Q\n",
"output": "NO\n"
},
{
"input": "OMMMAAMMMO\n",
"output": "YES\n"
},
{
"input": "U\n",
"output": "YES\n"
}
] | code_contests | python | 0.5 | 366a5d338ba45efa188a4f781a49e3b9 |
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.
Restore the the original sequence knowing digits remaining on the board.
Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
Output
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers — a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them.
If there is no answer, print a single line "NO" (without the quotes).
Examples
Input
3
?
18
1?
Output
YES
1
18
19
Input
2
??
?
Output
NO
Input
5
12224
12??5
12226
?0000
?00000
Output
YES
12224
12225
12226
20000
100000
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def solve(s, t, i, l):
if i == l:
return False
if s[i] == "?":
if solve(s, t, i + 1, l):
s[i] = t[i]
return True
elif t[i] == "9":
return False
s[i] = nxt[t[i]]
for j in range(i, l):
if s[j] == "?":
s[j] = "0"
return True
elif s[i] > t[i]:
for j in range(i, l):
if s[j] == "?":
s[j] = "0"
return True
elif s[i] < t[i]:
return False
else:
return solve(s, t, i + 1, l)
n = int(input())
a = [list(input()) for _ in range(n)]
p = ["0"]
nxt = {str(x): str(x + 1) for x in range(9)}
for i, ai in enumerate(a):
if len(p) > len(ai):
print("NO")
break
if len(p) < len(ai):
if a[i][0] == "?":
a[i][0] = "1"
for j in range(len(ai)):
if a[i][j] == "?":
a[i][j] = "0"
elif not solve(a[i], p, 0, len(ai)):
print("NO")
break
p = a[i]
else:
print("YES")
print("\n".join("".join(line) for line in a)) | python | code_algorithm | [
{
"input": "3\n?\n18\n1?\n",
"output": "YES\n1\n18\n19\n"
},
{
"input": "2\n??\n?\n",
"output": "NO\n"
},
{
"input": "5\n12224\n12??5\n12226\n?0000\n?00000\n",
"output": "YES\n12224\n12225\n12226\n20000\n100000\n"
},
{
"input": "98\n?\n?0\n2?\n6?\n6?\n69\n??\n??\n96\n1?2\n??3\n104\n??4\n1?9\n??2\n18?\n?01\n205\n?19\n244\n??8\n?5?\n?5?\n276\n??3\n???\n???\n?28\n?3?\n3??\n??8\n355\n4?0\n4??\n?10\n??1\n417\n4?9\n?3?\n4?4\n?61\n?8?\n???\n507\n?2?\n???\n??6\n5?7\n540\n5?9\n???\n?7?\n5??\n591\n?9?\n6?0\n620\n??4\n??1\n?35\n65?\n65?\n6?8\n6??\n68?\n7?4\n7??\n718\n?2?\n??9\n???\n7??\n?7?\n776\n7??\n788\n???\n?0?\n803\n83?\n846\n84?\n853\n85?\n87?\n?8?\n89?\n9?1\n91?\n929\n??0\n??6\n??3\n9??\n98?\n9?5\n9??\n995\n",
"output": "YES\n1\n10\n20\n60\n61\n69\n70\n71\n96\n102\n103\n104\n114\n119\n122\n180\n201\n205\n219\n244\n248\n250\n251\n276\n283\n284\n285\n328\n330\n331\n338\n355\n400\n401\n410\n411\n417\n419\n430\n434\n461\n480\n481\n507\n520\n521\n526\n527\n540\n549\n550\n570\n571\n591\n592\n600\n620\n624\n631\n635\n650\n651\n658\n659\n680\n704\n705\n718\n720\n729\n730\n731\n770\n776\n777\n788\n789\n800\n803\n830\n846\n847\n853\n854\n870\n880\n890\n901\n910\n929\n930\n936\n943\n944\n980\n985\n986\n995\n"
},
{
"input": "8\n?\n2\n3\n4\n?\n?\n?\n9\n",
"output": "YES\n1\n2\n3\n4\n5\n6\n7\n9\n"
},
{
"input": "2\n99999999\n99999999\n",
"output": "NO\n"
},
{
"input": "3\n99999997\n99999998\n???????\n",
"output": "NO\n"
},
{
"input": "2\n13300\n12?34\n",
"output": "NO\n"
},
{
"input": "1\n????????\n",
"output": "YES\n10000000\n"
},
{
"input": "3\n99999998\n????????\n99999999\n",
"output": "NO\n"
},
{
"input": "3\n19\n2?\n20\n",
"output": "NO\n"
},
{
"input": "3\n100\n?00\n200\n",
"output": "NO\n"
},
{
"input": "2\n140\n?40\n",
"output": "YES\n140\n240\n"
},
{
"input": "4\n????????\n10000001\n99999998\n????????\n",
"output": "YES\n10000000\n10000001\n99999998\n99999999\n"
},
{
"input": "2\n50\n5\n",
"output": "NO\n"
},
{
"input": "3\n99999998\n99999999\n????????\n",
"output": "NO\n"
},
{
"input": "2\n100\n?00\n",
"output": "YES\n100\n200\n"
},
{
"input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n",
"output": "YES\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
},
{
"input": "2\n100\n???\n",
"output": "YES\n100\n101\n"
},
{
"input": "3\n18\n19\n1?\n",
"output": "NO\n"
},
{
"input": "4\n100\n???\n999\n???\n",
"output": "NO\n"
},
{
"input": "11\n?\n?\n?\n?\n?\n?\n?\n?\n?\n?\n?\n",
"output": "NO\n"
},
{
"input": "10\n473883\n3499005\n4?74792\n58146??\n8?90593\n9203?71\n?39055?\n1?692641\n11451902\n?22126?2\n",
"output": "YES\n473883\n3499005\n4074792\n5814600\n8090593\n9203071\n9390550\n10692641\n11451902\n12212602\n"
},
{
"input": "3\n20\n19\n21\n",
"output": "NO\n"
},
{
"input": "2\n?00\n100\n",
"output": "NO\n"
}
] | code_contests | python | 0 | 12c8bda860b1b8ac7ddcf76aa6bc4806 |
Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve.
All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of n blocks, each block containing only equal characters. One block may be described as a pair (li, ci), where li is the length of the i-th block and ci is the corresponding letter. Thus, the string s may be written as the sequence of pairs <image>.
Your task is to write the program, that given two compressed string t and s finds all occurrences of s in t. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that p is the starting position of some occurrence of s in t if and only if tptp + 1...tp + |s| - 1 = s, where ti is the i-th character of string t.
Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as <image>, <image>, <image>...
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of blocks in the strings t and s, respectively.
The second line contains the descriptions of n parts of string t in the format "li-ci" (1 ≤ li ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.
The second line contains the descriptions of m parts of string s in the format "li-ci" (1 ≤ li ≤ 1 000 000) — the length of the i-th part and the corresponding lowercase English letter.
Output
Print a single integer — the number of occurrences of s in t.
Examples
Input
5 3
3-a 2-b 4-c 3-a 2-c
2-a 2-b 1-c
Output
1
Input
6 1
3-a 6-b 7-a 4-c 8-e 2-a
3-a
Output
6
Input
5 5
1-h 1-e 1-l 1-l 1-o
1-w 1-o 1-r 1-l 1-d
Output
0
Note
In the first sample, t = "aaabbccccaaacc", and string s = "aabbc". The only occurrence of string s in string t starts at position p = 2.
In the second sample, t = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and s = "aaa". The occurrences of s in t start at positions p = 1, p = 10, p = 11, p = 12, p = 13 and p = 14.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def ziped(a):
p = []
for i in a:
x = int(i.split('-')[0])
y = i.split('-')[1]
if len(p) > 0 and p[-1][1] == y:
p[-1][0] += x
else:
p.append([x, y])
return p
def solve(a, b , c):
ans = 0
if len(b) == 1:
for token in a:
#print("token",token)
if c(token, b[0]):
ans += token[0] - b[0][0] + 1
return ans
if len(b) == 2:
for i in range(len(a) - 1):
if c(a[i], b[0]) and c(a[i + 1], b[-1]):
ans += 1
return ans
v = b[1 : -1] + [[100500, '#']] + a
p = [0] * len(v)
for i in range(1, len(v)):
j = p[i - 1]
while j > 0 and v[i] != v[j]:
j = p[j - 1]
if v[i] == v[j]:
j += 1
p[i] = j
for i in range(len(v) - 1):
if p[i] == len(b) - 2 and c(v[i - p[i]], b[0]) and c(v[i + 1], b[-1]):
ans += 1
return ans
n, m = map(int, input().split())
a = ziped(input().split())
b = ziped(input().split())
print(solve(a, b, lambda x, y: x[1] == y[1] and x[0] >= y[0]))
| python | code_algorithm | [
{
"input": "6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a\n",
"output": "6\n"
},
{
"input": "5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d\n",
"output": "0\n"
},
{
"input": "5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c\n",
"output": "1\n"
},
{
"input": "9 3\n1-h 1-e 2-l 1-o 1-w 1-o 1-r 1-l 1-d\n2-l 1-o 1-w\n",
"output": "1\n"
},
{
"input": "9 2\n1-a 2-b 1-o 1-k 1-l 1-m 1-a 3-b 3-z\n1-a 2-b\n",
"output": "2\n"
},
{
"input": "2 2\n1-a 1-b\n2-a 1-b\n",
"output": "0\n"
},
{
"input": "10 3\n1-b 1-a 2-b 1-a 1-b 1-a 4-b 1-a 1-a 2-b\n1-b 1-a 1-b\n",
"output": "3\n"
},
{
"input": "5 2\n7-a 6-b 6-a 5-b 2-b\n6-a 7-b\n",
"output": "1\n"
},
{
"input": "1 1\n6543-o\n34-o\n",
"output": "6510\n"
},
{
"input": "15 7\n1-b 2-a 1-b 1-c 1-b 1-a 1-b 1-c 1-b 2-a 1-b 1-c 1-b 1-a 1-b\n1-b 2-a 1-b 1-c 1-b 1-a 1-b\n",
"output": "2\n"
},
{
"input": "15 7\n1-b 1-a 1-b 1-c 1-b 1-a 1-b 1-c 1-b 1-a 1-b 1-c 1-b 1-a 1-b\n1-b 1-a 1-b 1-c 1-b 1-a 1-b\n",
"output": "3\n"
},
{
"input": "28 7\n1-a 1-b 1-c 1-d 1-e 1-f 1-t 1-a 1-b 1-c 1-d 1-e 1-f 1-j 1-a 1-b 1-c 1-d 1-e 1-f 1-g 1-a 1-b 1-c 1-d 1-e 1-f 2-g\n1-a 1-b 1-c 1-d 1-e 1-f 1-g\n",
"output": "2\n"
},
{
"input": "4 3\n8-b 2-a 7-b 3-a\n3-b 2-b 1-a\n",
"output": "2\n"
},
{
"input": "10 3\n7-a 1-c 6-b 1-c 8-a 1-c 8-b 6-a 2-c 5-b\n5-a 1-c 4-b\n",
"output": "2\n"
},
{
"input": "9 5\n7-a 6-b 7-a 6-b 7-a 6-b 8-a 6-b 7-a\n7-a 6-b 7-a 6-b 7-a\n",
"output": "2\n"
},
{
"input": "5 3\n1-m 1-i 2-r 1-o 1-r\n1-m 1-i 1-r\n",
"output": "1\n"
},
{
"input": "4 2\n7-a 3-b 2-c 11-a\n3-a 4-a\n",
"output": "6\n"
},
{
"input": "1 1\n12344-a\n12345-a\n",
"output": "0\n"
},
{
"input": "4 1\n10-a 2-b 8-d 11-e\n1-c\n",
"output": "0\n"
},
{
"input": "4 2\n10-c 3-c 2-d 8-a\n6-a 1-b\n",
"output": "0\n"
},
{
"input": "1 1\n5352-k\n5234-j\n",
"output": "0\n"
},
{
"input": "1 1\n1-z\n1-z\n",
"output": "1\n"
},
{
"input": "10 3\n2-w 4-l 2-w 4-l 2-w 5-l 2-w 6-l 3-w 3-l\n2-l 2-w 2-l\n",
"output": "3\n"
},
{
"input": "8 5\n1-a 1-b 1-c 1-a 2-b 1-c 1-a 1-b\n1-a 1-b 1-c 1-a 1-b\n",
"output": "1\n"
}
] | code_contests | python | 0 | 206c1820592cdb40bd9ee031df4da7c9 |
Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.
More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.
<image>
It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.
You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.
Input
The first line contains two integers n and m (2 ≤ n, m ≤ 106).
Output
Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other.
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).
Examples
Input
3 4
Output
2
Input
3 3
Output
3
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math
n,m=map(int,input().split())
print(math.gcd(n-1,m-1)+1)
| python | code_algorithm | [
{
"input": "3 4\n",
"output": "2\n"
},
{
"input": "3 3\n",
"output": "3\n"
},
{
"input": "8 50\n",
"output": "8\n"
},
{
"input": "302237 618749\n",
"output": "5\n"
},
{
"input": "999999 1000000\n",
"output": "2\n"
},
{
"input": "893011 315181\n",
"output": "52531\n"
},
{
"input": "4449 1336\n",
"output": "2\n"
},
{
"input": "871866 348747\n",
"output": "174374\n"
},
{
"input": "999 999\n",
"output": "999\n"
},
{
"input": "2311 7771\n",
"output": "211\n"
},
{
"input": "429181 515017\n",
"output": "85837\n"
},
{
"input": "21 15\n",
"output": "3\n"
},
{
"input": "201439 635463\n",
"output": "3\n"
},
{
"input": "977965 896468\n",
"output": "81498\n"
},
{
"input": "1000000 1000000\n",
"output": "1000000\n"
},
{
"input": "384 187\n",
"output": "2\n"
},
{
"input": "720 972\n",
"output": "2\n"
},
{
"input": "198441 446491\n",
"output": "49611\n"
},
{
"input": "993 342\n",
"output": "32\n"
},
{
"input": "1498 9704\n",
"output": "2\n"
},
{
"input": "829981 586711\n",
"output": "14311\n"
},
{
"input": "7 61\n",
"output": "7\n"
},
{
"input": "35329 689665\n",
"output": "1537\n"
},
{
"input": "4 4\n",
"output": "4\n"
},
{
"input": "97905 599257\n",
"output": "233\n"
},
{
"input": "293492 654942\n",
"output": "2\n"
},
{
"input": "3466 4770\n",
"output": "2\n"
},
{
"input": "7 21\n",
"output": "3\n"
},
{
"input": "7 10\n",
"output": "4\n"
},
{
"input": "604630 225648\n",
"output": "2\n"
},
{
"input": "9 9\n",
"output": "9\n"
},
{
"input": "702 200\n",
"output": "2\n"
},
{
"input": "2 3\n",
"output": "2\n"
},
{
"input": "503832 242363\n",
"output": "2\n"
},
{
"input": "5 13\n",
"output": "5\n"
},
{
"input": "238 116\n",
"output": "2\n"
},
{
"input": "364915 516421\n",
"output": "343\n"
},
{
"input": "8 8\n",
"output": "8\n"
},
{
"input": "12 9\n",
"output": "2\n"
},
{
"input": "863029 287677\n",
"output": "287677\n"
},
{
"input": "117806 188489\n",
"output": "23562\n"
},
{
"input": "4 7\n",
"output": "4\n"
},
{
"input": "962963 1000000\n",
"output": "37038\n"
},
{
"input": "9 256\n",
"output": "2\n"
},
{
"input": "2 2\n",
"output": "2\n"
},
{
"input": "4 3\n",
"output": "2\n"
},
{
"input": "12 5\n",
"output": "2\n"
},
{
"input": "666667 1000000\n",
"output": "333334\n"
},
{
"input": "543425 776321\n",
"output": "77633\n"
},
{
"input": "524288 131072\n",
"output": "2\n"
},
{
"input": "403034 430556\n",
"output": "2\n"
},
{
"input": "10 10\n",
"output": "10\n"
},
{
"input": "4 6\n",
"output": "2\n"
},
{
"input": "146412 710630\n",
"output": "3572\n"
},
{
"input": "7 1000000\n",
"output": "4\n"
},
{
"input": "9516 2202\n",
"output": "2\n"
},
{
"input": "943547 987965\n",
"output": "1347\n"
},
{
"input": "701905 526429\n",
"output": "175477\n"
},
{
"input": "848 271\n",
"output": "2\n"
},
{
"input": "576709 834208\n",
"output": "562\n"
},
{
"input": "2482 6269\n",
"output": "2\n"
},
{
"input": "222403 592339\n",
"output": "2203\n"
},
{
"input": "5 7\n",
"output": "3\n"
},
{
"input": "672961 948978\n",
"output": "2\n"
}
] | code_contests | python | 0 | be5c511eba44e4fc23df86bd69567303 |
Vasya and Petya take part in a Codeforces round. The round lasts for two hours and contains five problems.
For this round the dynamic problem scoring is used. If you were lucky not to participate in any Codeforces round with dynamic problem scoring, here is what it means. The maximum point value of the problem depends on the ratio of the number of participants who solved the problem to the total number of round participants. Everyone who made at least one submission is considered to be participating in the round.
<image>
Pay attention to the range bounds. For example, if 40 people are taking part in the round, and 10 of them solve a particular problem, then the solvers fraction is equal to 1 / 4, and the problem's maximum point value is equal to 1500.
If the problem's maximum point value is equal to x, then for each whole minute passed from the beginning of the contest to the moment of the participant's correct submission, the participant loses x / 250 points. For example, if the problem's maximum point value is 2000, and the participant submits a correct solution to it 40 minutes into the round, this participant will be awarded with 2000·(1 - 40 / 250) = 1680 points for this problem.
There are n participants in the round, including Vasya and Petya. For each participant and each problem, the number of minutes which passed between the beginning of the contest and the submission of this participant to this problem is known. It's also possible that this participant made no submissions to this problem.
With two seconds until the end of the round, all participants' submissions have passed pretests, and not a single hack attempt has been made. Vasya believes that no more submissions or hack attempts will be made in the remaining two seconds, and every submission will pass the system testing.
Unfortunately, Vasya is a cheater. He has registered 109 + 7 new accounts for the round. Now Vasya can submit any of his solutions from these new accounts in order to change the maximum point values of the problems. Vasya can also submit any wrong solutions to any problems. Note that Vasya can not submit correct solutions to the problems he hasn't solved.
Vasya seeks to score strictly more points than Petya in the current round. Vasya has already prepared the scripts which allow to obfuscate his solutions and submit them into the system from any of the new accounts in just fractions of seconds. However, Vasya doesn't want to make his cheating too obvious, so he wants to achieve his goal while making submissions from the smallest possible number of new accounts.
Find the smallest number of new accounts Vasya needs in order to beat Petya (provided that Vasya's assumptions are correct), or report that Vasya can't achieve his goal.
Input
The first line contains a single integer n (2 ≤ n ≤ 120) — the number of round participants, including Vasya and Petya.
Each of the next n lines contains five integers ai, 1, ai, 2..., ai, 5 ( - 1 ≤ ai, j ≤ 119) — the number of minutes passed between the beginning of the round and the submission of problem j by participant i, or -1 if participant i hasn't solved problem j.
It is guaranteed that each participant has made at least one successful submission.
Vasya is listed as participant number 1, Petya is listed as participant number 2, all the other participants are listed in no particular order.
Output
Output a single integer — the number of new accounts Vasya needs to beat Petya, or -1 if Vasya can't achieve his goal.
Examples
Input
2
5 15 40 70 115
50 45 40 30 15
Output
2
Input
3
55 80 10 -1 -1
15 -1 79 60 -1
42 -1 13 -1 -1
Output
3
Input
5
119 119 119 119 119
0 0 0 0 -1
20 65 12 73 77
78 112 22 23 11
1 78 60 111 62
Output
27
Input
4
-1 20 40 77 119
30 10 73 50 107
21 29 -1 64 98
117 65 -1 -1 -1
Output
-1
Note
In the first example, Vasya's optimal strategy is to submit the solutions to the last three problems from two new accounts. In this case the first two problems will have the maximum point value of 1000, while the last three problems will have the maximum point value of 500. Vasya's score will be equal to 980 + 940 + 420 + 360 + 270 = 2970 points, while Petya will score just 800 + 820 + 420 + 440 + 470 = 2950 points.
In the second example, Vasya has to make a single unsuccessful submission to any problem from two new accounts, and a single successful submission to the first problem from the third new account. In this case, the maximum point values of the problems will be equal to 500, 1500, 1000, 1500, 3000. Vasya will score 2370 points, while Petya will score just 2294 points.
In the third example, Vasya can achieve his goal by submitting the solutions to the first four problems from 27 new accounts. The maximum point values of the problems will be equal to 500, 500, 500, 500, 2000. Thanks to the high cost of the fifth problem, Vasya will manage to beat Petya who solved the first four problems very quickly, but couldn't solve the fifth one.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
inf = 10**9 + 7
def solve():
n = int(sys.stdin.readline())
v = [int(vi) for vi in sys.stdin.readline().split()] # Vesya
p = [int(pi) for pi in sys.stdin.readline().split()] # Petya
cnt = [0]*5
for i in range(5):
if v[i] != -1:
cnt[i] += 1
if p[i] != -1:
cnt[i] += 1
for i in range(n - 2):
a = [int(ai) for ai in sys.stdin.readline().split()]
for j in range(5):
if a[j] != -1:
cnt[j] += 1
for i in range(4000):
if check(n, v, p, cnt, i):
print(i)
return
print(-1)
def check(n, v, p, cnt, m):
tot = n + m
solved = cnt[:]
dif = 0
for i in range(5):
if p[i] != -1 and v[i] > p[i]:
solved[i] += m
for i in range(5):
if solved[i]*2 > tot:
max_score = 500
elif solved[i]*4 > tot:
max_score = 1000
elif solved[i]*8 > tot:
max_score = 1500
elif solved[i]*16 > tot:
max_score = 2000
elif solved[i]*32 > tot:
max_score = 2500
else:
max_score = 3000
if v[i] == p[i] == -1:
pass
elif v[i] == -1:
dif -= max_score * (250 - p[i]) // 250
elif p[i] == -1:
dif += max_score * (250 - v[i]) // 250
else:
dif += max_score * (p[i] - v[i]) // 250
return dif > 0
if __name__ == '__main__':
solve() | python | code_algorithm | [
{
"input": "4\n-1 20 40 77 119\n30 10 73 50 107\n21 29 -1 64 98\n117 65 -1 -1 -1\n",
"output": "-1\n"
},
{
"input": "5\n119 119 119 119 119\n0 0 0 0 -1\n20 65 12 73 77\n78 112 22 23 11\n1 78 60 111 62\n",
"output": "27\n"
},
{
"input": "2\n5 15 40 70 115\n50 45 40 30 15\n",
"output": "2\n"
},
{
"input": "3\n55 80 10 -1 -1\n15 -1 79 60 -1\n42 -1 13 -1 -1\n",
"output": "3\n"
},
{
"input": "2\n33 15 51 7 101\n41 80 40 13 46\n",
"output": "0\n"
},
{
"input": "3\n20 65 12 73 77\n78 112 22 23 11\n1 78 60 111 62\n",
"output": "3\n"
},
{
"input": "9\n57 52 60 56 91\n32 40 107 89 36\n80 0 45 92 119\n62 9 107 24 61\n43 28 4 26 113\n31 91 86 13 95\n4 2 88 38 68\n83 35 57 101 28\n12 40 37 56 73\n",
"output": "9\n"
},
{
"input": "26\n3 -1 71 -1 42\n85 72 48 38 -1\n-1 -1 66 24 -1\n46 -1 60 99 107\n53 106 51 -1 104\n-1 17 98 54 -1\n44 107 66 65 102\n47 40 62 34 5\n-1 10 -1 98 -1\n-1 69 47 85 75\n12 62 -1 15 -1\n48 63 72 32 99\n91 104 111 -1 -1\n92 -1 52 -1 11\n118 25 97 1 108\n-1 61 97 37 -1\n87 47 -1 -1 21\n79 87 73 82 70\n90 108 19 25 57\n37 -1 51 8 119\n64 -1 -1 38 82\n42 61 63 25 27\n82 -1 15 82 15\n-1 89 73 95 -1\n4 8 -1 70 116\n89 21 65 -1 88\n",
"output": "10\n"
},
{
"input": "19\n78 100 74 31 2\n27 45 72 63 0\n42 114 31 106 79\n88 119 118 69 90\n68 14 90 104 70\n106 21 96 15 73\n75 66 54 46 107\n108 49 17 34 90\n76 112 49 56 76\n34 43 5 57 67\n47 43 114 73 109\n79 118 69 22 19\n31 74 21 84 79\n1 64 88 97 79\n115 14 119 101 28\n55 9 43 67 10\n33 40 26 10 11\n92 0 60 14 48\n58 57 8 12 118\n",
"output": "133\n"
},
{
"input": "10\n-1 18 44 61 115\n-1 34 12 40 114\n-1 86 100 119 58\n-1 4 36 8 91\n1 58 85 13 82\n-1 9 85 109 -1\n13 75 0 71 42\n116 75 42 79 88\n62 -1 98 114 -1\n68 96 44 61 35\n",
"output": "62\n"
},
{
"input": "17\n66 15 -1 42 90\n67 108 104 16 110\n76 -1 -1 -1 96\n108 32 100 91 17\n87 -1 85 10 -1\n70 55 102 15 23\n-1 33 111 105 63\n-1 56 104 68 116\n56 111 102 89 63\n63 -1 68 80 -1\n80 61 -1 81 19\n101 -1 87 -1 89\n92 82 4 105 83\n19 30 114 77 104\n100 99 29 68 82\n98 -1 62 52 -1\n108 -1 -1 50 -1\n",
"output": "5\n"
},
{
"input": "2\n0 0 0 0 1\n0 0 0 1 0\n",
"output": "2\n"
},
{
"input": "4\n66 55 95 78 114\n70 98 8 95 95\n17 47 88 71 18\n23 22 9 104 38\n",
"output": "4\n"
}
] | code_contests | python | 0 | 1d6ca7a655ec6d449360a413fd88f037 |
Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome.
A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.
Input
The first and single line contains string s (1 ≤ |s| ≤ 15).
Output
Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.
Examples
Input
abccaa
Output
YES
Input
abbcca
Output
NO
Input
abcda
Output
YES
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
import math
import itertools
import collections
def getdict(n):
d = {}
if type(n) is list or type(n) is str:
for i in n:
if i in d:
d[i] += 1
else:
d[i] = 1
else:
for i in range(n):
t = ii()
if t in d:
d[t] += 1
else:
d[t] = 1
return d
def divs(n, start=1):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def cdiv(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(map(int, input().split()))
def lcm(a, b): return abs(a*b) // math.gcd(a, b)
def wr(arr): return ''.join(map(str, arr))
def revn(n): return int(str(n)[::-1])
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base=3):
newnumber = ''
while number > 0:
newnumber = str(number % base) + newnumber
number //= base
return newnumber
s = input()
t = 0
for i in range(len(s) // 2):
if s[i] != s[-i -1]:
t += 1
if len(s) % 2 == 1 and t < 2 or len(s) % 2 == 0 and t == 1:
print('YES')
else:
print('NO')
| python | code_algorithm | [
{
"input": "abbcca\n",
"output": "NO\n"
},
{
"input": "abcda\n",
"output": "YES\n"
},
{
"input": "abccaa\n",
"output": "YES\n"
},
{
"input": "abcdmnp\n",
"output": "NO\n"
},
{
"input": "ucnolsloncw\n",
"output": "YES\n"
},
{
"input": "acbb\n",
"output": "NO\n"
},
{
"input": "typayzzyapyt\n",
"output": "NO\n"
},
{
"input": "abcde\n",
"output": "NO\n"
},
{
"input": "eusneioiensue\n",
"output": "YES\n"
},
{
"input": "ecabd\n",
"output": "NO\n"
},
{
"input": "afghqwe\n",
"output": "NO\n"
},
{
"input": "abcdabcde\n",
"output": "NO\n"
},
{
"input": "guayhmg\n",
"output": "NO\n"
},
{
"input": "ww\n",
"output": "NO\n"
},
{
"input": "abcb\n",
"output": "NO\n"
},
{
"input": "lkvhhvkl\n",
"output": "NO\n"
},
{
"input": "abbcs\n",
"output": "NO\n"
},
{
"input": "cbacb\n",
"output": "NO\n"
},
{
"input": "galjjtyw\n",
"output": "NO\n"
},
{
"input": "czhfc\n",
"output": "YES\n"
},
{
"input": "aanbb\n",
"output": "NO\n"
},
{
"input": "abbba\n",
"output": "YES\n"
},
{
"input": "abbzcca\n",
"output": "NO\n"
},
{
"input": "qjwmjmljq\n",
"output": "YES\n"
},
{
"input": "ysxibbixsq\n",
"output": "YES\n"
},
{
"input": "ekhajrjahke\n",
"output": "YES\n"
},
{
"input": "abcdd\n",
"output": "NO\n"
},
{
"input": "aabbc\n",
"output": "NO\n"
},
{
"input": "wvxxzw\n",
"output": "YES\n"
},
{
"input": "aaakcba\n",
"output": "NO\n"
},
{
"input": "abcedca\n",
"output": "NO\n"
},
{
"input": "abccacb\n",
"output": "NO\n"
},
{
"input": "bbb\n",
"output": "YES\n"
},
{
"input": "aacbb\n",
"output": "NO\n"
},
{
"input": "ffdsslff\n",
"output": "YES\n"
},
{
"input": "bbcaa\n",
"output": "NO\n"
},
{
"input": "xokxpyyuafij\n",
"output": "NO\n"
},
{
"input": "c\n",
"output": "YES\n"
},
{
"input": "cojhkhxxhkhjoc\n",
"output": "NO\n"
},
{
"input": "jrzsfrrkrtj\n",
"output": "NO\n"
},
{
"input": "abcdfga\n",
"output": "NO\n"
},
{
"input": "scfwrjevejrwfcs\n",
"output": "YES\n"
},
{
"input": "abcdefg\n",
"output": "NO\n"
},
{
"input": "uwdhkzokhdwu\n",
"output": "YES\n"
},
{
"input": "aaabbb\n",
"output": "NO\n"
},
{
"input": "broon\n",
"output": "NO\n"
},
{
"input": "aaacbbb\n",
"output": "NO\n"
},
{
"input": "abcqr\n",
"output": "NO\n"
},
{
"input": "qjfyjjyfjq\n",
"output": "NO\n"
},
{
"input": "ukvciu\n",
"output": "NO\n"
},
{
"input": "abecd\n",
"output": "NO\n"
},
{
"input": "uosgwgsou\n",
"output": "YES\n"
},
{
"input": "aba\n",
"output": "YES\n"
},
{
"input": "a\n",
"output": "YES\n"
},
{
"input": "abcdefghi\n",
"output": "NO\n"
},
{
"input": "abxab\n",
"output": "NO\n"
},
{
"input": "aaa\n",
"output": "YES\n"
},
{
"input": "paxkxbq\n",
"output": "NO\n"
},
{
"input": "abcrtyu\n",
"output": "NO\n"
},
{
"input": "glxlg\n",
"output": "YES\n"
},
{
"input": "vrnwnrv\n",
"output": "YES\n"
},
{
"input": "aabc\n",
"output": "NO\n"
},
{
"input": "ustrvrodf\n",
"output": "NO\n"
},
{
"input": "jdj\n",
"output": "YES\n"
},
{
"input": "aabcd\n",
"output": "NO\n"
},
{
"input": "aa\n",
"output": "NO\n"
},
{
"input": "b\n",
"output": "YES\n"
},
{
"input": "thdaonpepdoadht\n",
"output": "YES\n"
},
{
"input": "aaaaaa\n",
"output": "NO\n"
},
{
"input": "mnlm\n",
"output": "YES\n"
},
{
"input": "rmggmr\n",
"output": "NO\n"
},
{
"input": "gqrk\n",
"output": "NO\n"
},
{
"input": "jizzz\n",
"output": "NO\n"
},
{
"input": "kxfqqncnebpami\n",
"output": "NO\n"
},
{
"input": "bbbbbb\n",
"output": "NO\n"
},
{
"input": "nm\n",
"output": "YES\n"
},
{
"input": "acbak\n",
"output": "NO\n"
},
{
"input": "vlkjkav\n",
"output": "YES\n"
},
{
"input": "abcdc\n",
"output": "NO\n"
},
{
"input": "aaabbab\n",
"output": "NO\n"
},
{
"input": "nn\n",
"output": "NO\n"
},
{
"input": "anpqb\n",
"output": "NO\n"
},
{
"input": "jsfzcbnhsccuqsj\n",
"output": "NO\n"
},
{
"input": "guvggtfhlgruy\n",
"output": "NO\n"
},
{
"input": "ab\n",
"output": "YES\n"
},
{
"input": "abcdf\n",
"output": "NO\n"
},
{
"input": "howfslfwmh\n",
"output": "NO\n"
},
{
"input": "abcxpoi\n",
"output": "NO\n"
},
{
"input": "kyw\n",
"output": "YES\n"
},
{
"input": "fuxpuajabpxuf\n",
"output": "YES\n"
},
{
"input": "bb\n",
"output": "NO\n"
},
{
"input": "z\n",
"output": "YES\n"
},
{
"input": "aaabccc\n",
"output": "NO\n"
},
{
"input": "aabbb\n",
"output": "NO\n"
},
{
"input": "fccf\n",
"output": "NO\n"
},
{
"input": "mhifbmmmmbmihm\n",
"output": "YES\n"
},
{
"input": "abcdrty\n",
"output": "NO\n"
},
{
"input": "abcxuio\n",
"output": "NO\n"
},
{
"input": "bddeffd\n",
"output": "NO\n"
}
] | code_contests | python | 0.6 | e43568778a544a3f4e6ab9b0f372ef58 |
Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s consisting only of lowercase and uppercase Latin letters.
Let A be a set of positions in the string. Let's call it pretty if following conditions are met:
* letters on positions from A in the string are all distinct and lowercase;
* there are no uppercase letters in the string which are situated between positions from A (i.e. there is no such j that s[j] is an uppercase letter, and a1 < j < a2 for some a1 and a2 from A).
Write a program that will determine the maximum number of elements in a pretty set of positions.
Input
The first line contains a single integer n (1 ≤ n ≤ 200) — length of string s.
The second line contains a string s consisting of lowercase and uppercase Latin letters.
Output
Print maximum number of elements in pretty set of positions for string s.
Examples
Input
11
aaaaBaabAbA
Output
2
Input
12
zACaAbbaazzC
Output
3
Input
3
ABC
Output
0
Note
In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position.
In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements.
In the third example the given string s does not contain any lowercase letters, so the answer is 0.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import re
input()
print(max(map(lambda s: len(set(s)), re.split('[A-Z]', input())))) | python | code_algorithm | [
{
"input": "11\naaaaBaabAbA\n",
"output": "2\n"
},
{
"input": "3\nABC\n",
"output": "0\n"
},
{
"input": "12\nzACaAbbaazzC\n",
"output": "3\n"
},
{
"input": "100\nchMRWwymTDuZDZuSTvUmmuxvSscnTasyjlwwodhzcoifeahnbmcifyeobbydwparebduoLDCgHlOsPtVRbYGGQXfnkdvrWKIwCRl\n",
"output": "20\n"
},
{
"input": "200\neLCCuYMPPwQoNlCpPOtKWJaQJmWfHeZCKiMSpILHSKjFOYGpRMzMCfMXdDuQdBGNsCNrHIVJzEFfBZcNMwNcFjOFVJvEtUQmLbFNKVHgNDyFkFVQhUTUQDgXhMjJZgFSSiHhMKuTgZQYJqAqKBpHoHddddddddddddddddXSSYNKNnRrKuOjAVKZlRLzCjExPdHaDHBT\n",
"output": "1\n"
},
{
"input": "3\nAaA\n",
"output": "1\n"
},
{
"input": "3\nAba\n",
"output": "2\n"
},
{
"input": "200\nZlJwrzzglgkjqtmoseaapqtqxbrorjptpejjmchztutkglqjxxqgnuxeadxksnclizglywqmtxdhrbbmooxijiymikyIOlSBShUOGUghPJtSxmToNUHDdQIMoFzPOVYAtLRtXyuOErmpYQFAeKMJKlqZCEVEuhSCCtBnQnOncTqYpaLbhutwhplyJGxtuXQiEfuvOzWr\n",
"output": "24\n"
},
{
"input": "3\nbbA\n",
"output": "1\n"
},
{
"input": "200\nitSYxgOLlwOoAkkkkkzzzzzzzzkzkzkzkkkkkzkzzkzUDJSKybRPBvaIDsNuWImPJvrHkKiMeYukWmtHtgZSyQsgYanZvXNbKXBlFLSUcqRnGWSriAvKxsTkDJfROqaKdzXhvJsPEDATueCraWOGEvRDWjPwXuiNpWsEnCuhDcKWOQxjBkdBqmFatWFkgKsbZuLtRGtY\n",
"output": "2\n"
},
{
"input": "28\nAabcBabcCBNMaaaaabbbbbcccccc\n",
"output": "3\n"
},
{
"input": "200\nPUVgmHVAXlBaxUIthvGXCVOuIcukqODzMYigUBAZNJcJoayfRXVFfLgKmHdtevsEjoexdSwqvtAeoiyqjnBKoyKduwPCBNEDdkTpjlOOdOlJIEWovEGoDzwhtITNpVamnXVSxPmZTbuhnEbtebgqYEXFbyagmztkxmcgshnxqrowthegiepvLLUIbBalXAJAbGplWMbt\n",
"output": "21\n"
},
{
"input": "200\nDxNZuvkTkQEqdWIkLzcKAwfqvZQiptnTazydSCTIfGjDhLMrlPZiKEsqIdDhgKPAlEvXyzNwWtYorotgkcwydpabjqnzubaksdchucxtkmjzfretdmvlxgklyvicrtftvztsbiUaQorfNIYUOdwQDRsKpxLUiLknbLbinilpPXPTTwLAnXVpMHBaAcKWgDBeOFabPtXU\n",
"output": "26\n"
},
{
"input": "200\nOLaJOtwultZLiZPSYAVGIbYvbIuZkqFZXwfsqpsavCDmBMStAuUFLBVknWDXNzmiuUYIsUMGxtoadWlPYPqvqSvpYdOiJRxFzGGnnmstniltvitnrmyrblnqyruylummmlsqtqitlbulvtuitiqimuintbimqyurviuntqnnvslynlNYMpYVKYwKVTbIUVdlNGrcFZON\n",
"output": "12\n"
},
{
"input": "5\naBacd\n",
"output": "3\n"
},
{
"input": "3\naaA\n",
"output": "1\n"
},
{
"input": "200\nGqxrcpJdPhHQUhCwJAOhrKsIttEjqYjygzoyvslFXvybQiWHjViWlOrHvMlDzWaiurjYKsmlrwqAhjZPHZaFMWFlgFJYntZLgPzuksawCmWOHFyANczsdkkusKLQOOHiJsJDvzsVRaUNWLKMRTbmoLwFOJxakhxsenjkzzucpothHNpNtMMfLdmiFyblVUmeIBdpFYIQ\n",
"output": "14\n"
},
{
"input": "2\ngg\n",
"output": "1\n"
},
{
"input": "2\nzG\n",
"output": "1\n"
},
{
"input": "200\neybfkeyfccebifebaukyeraeuxaeprnibicxknepxurnlrbuyuykexbiycpnbbafxnpbrkctpkteetrfptiyaceoylnclytirfkcxboeyufplnbtbikireyulxfaknppuicnabixrnpoicexcaelttlllttfboniebinulkoxuilpnnkrpectutnctoykpPJsflVwmDo\n",
"output": "16\n"
},
{
"input": "200\nBmggKNRZBXPtJqlJaXLdKKQLDJvXpDuQGupiRQfDwCJCJvAlDDGpPZNOvXkrdKOFOEFBVfrsZjWyHPoKGzXmTAyPJGEmxCyCXpeAdTwbrMtWLmlmGNqxvuxmqpmtpuhrmxxtrquSLFYVlnSYgRJDYHWgHBbziBLZRwCIJNvbtsEdLLxmTbnjkoqSPAuzEeTYLlmejOUH\n",
"output": "9\n"
},
{
"input": "26\npbgfqosklxjuzmdheyvawrictn\n",
"output": "26\n"
},
{
"input": "1\nk\n",
"output": "1\n"
},
{
"input": "200\nmeZNrhqtSTSmktGQnnNOTcnyAMTKSixxKQKiagrMqRYBqgbRlsbJhvtNeHVUuMCyZLCnsIixRYrYEAkfQOxSVqXkrPqeCZQksInzRsRKBgvIqlGVPxPQnypknSXjgMjsjElcqGsaJRbegJVAKtWcHoOnzHqzhoKReqBBsOhZYLaYJhmqOMQsizdCsQfjUDHcTtHoeYwu\n",
"output": "4\n"
},
{
"input": "3\nAAB\n",
"output": "0\n"
},
{
"input": "3\nbba\n",
"output": "2\n"
},
{
"input": "35\nbvZWiitgxodztelnYUyljYGnCoWluXTvBLp\n",
"output": "10\n"
},
{
"input": "200\noggqoqqogoqoggggoggqgooqggogogooogqqgggoqgggqoqogogggogggqgooqgqggqqqoqgqgoooqgqogqoggoqqgqoqgoooqoogooqoogqoqoqqgoqgoqgggogqqqoqoggoqoqqoqggqoggooqqqoqggoggqqqqqqqqqgogqgggggooogogqgggqogqgoqoqogoooq\n",
"output": "3\n"
},
{
"input": "200\nWTCKAKLVGXSYFVMVJDUYERXNMVNTGWXUGRFCGMYXJQGLODYZTUIDENHYEGFKXFIEUILAMESAXAWZXVCZPJPEYUXBITHMTZOTMKWITGRSFHODKVJHPAHVVWTCTHIVAWAREQXWMPUWQSTPPJFHKGKELBTPUYDAVIUMGASPUEDIODRYXIWCORHOSLIBLOZUNJPHHMXEXOAY\n",
"output": "0\n"
},
{
"input": "200\ngnDdkqJjYvduVYDSsswZDvoCouyaYZTfhmpSakERWLhufZtthWsfbQdTGwhKYjEcrqWBOyxBbiFhdLlIjChLOPiOpYmcrJgDtXsJfmHtLrabyGKOfHQRukEtTzwoqBHfmyVXPebfcpGQacLkGWFwerszjdHpTBXGssYXmGHlcCBgBXyGJqxbVhvDffLyCrZnxonABEXV\n",
"output": "7\n"
},
{
"input": "20\niFSiiigiYFSKmDnMGcgM\n",
"output": "2\n"
},
{
"input": "2\nai\n",
"output": "2\n"
},
{
"input": "100\nucOgELrgjMrFOgtHzqgvUgtHngKJxdMFKBjfcCppciqmGZXXoiSZibgpadshyljqrwxbomzeutvnhTLGVckZUmyiFPLlwuLBFito\n",
"output": "23\n"
},
{
"input": "1\na\n",
"output": "1\n"
},
{
"input": "3\nZZZ\n",
"output": "0\n"
},
{
"input": "3\naBa\n",
"output": "1\n"
},
{
"input": "3\naaa\n",
"output": "1\n"
},
{
"input": "200\nNcYVomemswLCUqVRSDKHCknlBmqeSWhVyRzQrnZaOANnTGqsRFMjpczllcEVebqpxdavzppvztxsnfmtcharzqlginndyjkawzurqkxJLXiXKNZTIIxhSQghDpjwzatEqnLMTLxwoEKpHytvWkKFDUcZjLShCiVdocxRvvJtbXHCDGpJvMwRKWLhcTFtswdLUHkbhfau\n",
"output": "25\n"
},
{
"input": "200\neTtezDHXMMBtFUaohwuYdRVloYwZLiaboJTMWLImSCrDTDFpAYJsQBzdwkEvcoYfWmKZfqZwfGHigaEpwAVoVPDSkDJsRzjjdyAcqfVIUcwiJuHyTisFCCNHIFyfDWOyMMRtTWmQFCRNtmwMTzQSZdOFfojwvAtxTrSgaDLzzBwDkxPoCYuSxpwRbnhfsERywPyUlJBC\n",
"output": "5\n"
},
{
"input": "200\nrsgraosldglhdoorwhkrsehjpuxrjuwgeanjgezhekprzarelduuaxdnspzjuooguuwnzkowkuhzduakdrzpnslauejhrrkalwpurpuuswdgeadlhjwzjgegwpknepazwwleulppwrlgrgedlwdzuodzropsrrkxusjnuzshdkjrxxpgzanzdrpnggdwxarpwohxdepJ\n",
"output": "17\n"
},
{
"input": "5\naAabc\n",
"output": "3\n"
},
{
"input": "35\nBTexnaeplecllxwlanarpcollawHLVMHIIF\n",
"output": "10\n"
},
{
"input": "200\nzdqzfjWCocqbUbVYaLrBFEwoQiMdlVssRjIctSenAIqPgPJhjPyvvgRFlplFXKTctqFUPlVyMxEfZDfFbUjOMCYFkrFeoZegnSzuKrbWQqcJWfeFauSqYeSCdiPiWroksbkdskrasekytdvnygetyabnteaybdZAoBntIxZJTTsTblLAosqwHQEXTSqjEQqzNGRgdaVG\n",
"output": "13\n"
},
{
"input": "200\nfEyHTWhrGQezPcvGDrqJEnPVQRxiaGPbWoPVYGMlGIWuAAWdFzIiRTEjsyivewudtzuiuhulqywpzmdopjykvtkjzlyhhzspmovwHtiMtZYkYBozzkGfGmrizjuQYVaewYqglzMAkQSZERgWPYVxfoGLzNbWAJZutTuGxeBzRIdKAKVPbqlTJVqiKnpFbTWawutdSjux\n",
"output": "18\n"
},
{
"input": "3\naAa\n",
"output": "1\n"
},
{
"input": "200\nXbTJZqcbpYuZQEoUrbxlPXAPCtVLrRExpQzxzqzcqsqzsiisswqitswzCtJQxOavicSdBIodideVRKHPojCNHmbnrLgwJlwOpyrJJIhrUePszxSjJGeUgTtOfewPQnPVWhZAtogRPrJLwyShNQaeNsvrJwjuuBOMPCeSckBMISQzGngfOmeyfDObncyeNsihYVtQbSEh\n",
"output": "8\n"
},
{
"input": "200\nGAcmlaqfjSAQLvXlkhxujXgSbxdFAwnoxDuldDvYmpUhTWJdcEQSdARLrozJzIgFVCkzPUztWIpaGfiKeqzoXinEjVuoKqyBHmtFjBWcRdBmyjviNlGAIkpikjAimmBgayfphrstfbjexjbttzfzfzaysxfyrjazfhtpghnbbeffjhxrjxpttesgzrnrfbgzzsRsCgmz\n",
"output": "15\n"
},
{
"input": "200\nOgMBgYeuMJdjPtLybvwmGDrQEOhliaabEtwulzNEjsfnaznXUMoBbbxkLEwSQzcLrlJdjJCLGVNBxorghPxTYCoqniySJMcilpsqpBAbqdzqRUDVaYOgqGhGrxlIJkyYgkOdTUgRZwpgIkeZFXojLXpDilzirHVVadiHaMrxhzodzpdvhvrzdzxbhmhdpxqqpoDegfFQ\n",
"output": "11\n"
},
{
"input": "100\nhXYLXKUMBrGkjqQJTGbGWAfmztqqapdbjbhcualhypgnaieKXmhzGMnqXVlcPesskfaEVgvWQTTShRRnEtFahWDyuBzySMpugxCM\n",
"output": "19\n"
},
{
"input": "2\naz\n",
"output": "2\n"
},
{
"input": "3\nBaB\n",
"output": "1\n"
},
{
"input": "3\naAb\n",
"output": "1\n"
},
{
"input": "200\nBmrFQLXLcKnPBEdQVHVplsEIsrxSlfEbbYXjMrouIwHSyfqjxoerueuanalzliouwllrhzfytmgtfoxeuhjfaaabwjdlimcluabajcoxwgqjotcjdftwqbxhwjnbwoeillzbftzynhfyyqiuszjreujwyhdrdhyyxsglgdjbtghlqutjstmsaowcqchzbdzoeqIqbQUY\n",
"output": "23\n"
},
{
"input": "200\nCtclUtUnmqFniaLqGRmMoUMeLyFfAgWxIZxdrBarcRQprSOGcdUYsmDbooSuOvBLgrYlgaIjJtFgcxJKHGkCXpYfVKmUbouuIqGstFrrwJzYQqjjqqppqqqqqpqqqjpjjpjqjXRYkfPhGAatOigFuItkKxkjCBLdiNMVGjmdWNMgOOvmaJEdGsWNoaERrINNKqKeQajv\n",
"output": "3\n"
},
{
"input": "20\npEjVrKWLIFCZjIHgggVU\n",
"output": "1\n"
},
{
"input": "200\nutxfRatSRtHcDvHfsBBCwsUFcAmrFxHfbMjWhAjfdoQkNfQWfgQEeBVZJavMlpmptFHJGxBFaHfxLAGcZJsUukXLBMfwWlqnnQxKtgnQDqtiJBnoAnxxFBlPJJnuWkdOzYGBRMwSSrjDAVZqYhtopxDVrBAJsIODCUadfEDDzeyroijgwqhaucmnstclNRVxoTYzMSlK\n",
"output": "19\n"
},
{
"input": "35\nhhwxqysolegsthsvfcqiryenbujbrrScobu\n",
"output": "20\n"
},
{
"input": "20\nedxedxxxCQiIVmYEUtLi\n",
"output": "3\n"
},
{
"input": "3\nabb\n",
"output": "2\n"
},
{
"input": "200\nPQNfMSHpPaOhjCpYPkOgzCpgOhgsmyaTHrlxgogmhdficyeszcbujjrxeihgrzszsjehsupezxlpcpsemesxcwwwllhisjpnepdoefpdzfptdrmETROYtOUjiUsPwrNkUfmeTFgSIJMjTuCFBgfkTQEYJijBaPnaVltQJhKufDKrdMAFnBflaekZErAuKkGhGMLLOEzZ\n",
"output": "22\n"
},
{
"input": "200\nMkuxcDWdcnqsrlTsejehQKrTwoOBRCUAywqSnZkDLRmVBDVoOqdZHbrInQQyeRFAjiYYmHGrBbWgWstCPfLPRdNVDXBdqFJsGQfSXbufsiogybEhKDlWfPazIuhpONwGzZWaQNwVnmhTqWdewaklgjwaumXYDGwjSeEcYXjkVtLiYSWULEnTFukIlWQGWsXwWRMJGTcI\n",
"output": "10\n"
},
{
"input": "4\nabbc\n",
"output": "3\n"
},
{
"input": "3\naba\n",
"output": "2\n"
},
{
"input": "200\nYRvIopNqSTYDhViTqCLMwEbTTIdHkoeuBmAJWhgtOgVxlcHSsavDNzMfpwTghkBvYEtCYQxicLUxdgAcaCzOOgbQYsfnaTXFlFxbeEiGwdNvxwHzkTdKtWlqzalwniDDBDipkxfflpaqkfkgfezbkxdvzemlfohwtgytzzywmwhvzUgPlPdeAVqTPAUZbogQheRXetvT\n",
"output": "20\n"
},
{
"input": "3\nAaa\n",
"output": "1\n"
},
{
"input": "20\nprnchweyabjvzkoqiltm\n",
"output": "20\n"
},
{
"input": "200\nvFAYTHJLZaivWzSYmiuDBDUFACDSVbkImnVaXBpCgrbgmTfXKJfoglIkZxWPSeVSFPnHZDNUAqLyhjLXSuAqGLskBlDxjxGPJyGdwzlPfIekwsblIrkxzfhJeNoHywdfAGlJzqXOfQaKceSqViVFTRJEGfACnsFeSFpOYisIHJciqTMNAmgeXeublTvfWoPnddtvKIyF\n",
"output": "6\n"
},
{
"input": "2\nAZ\n",
"output": "0\n"
},
{
"input": "3\nabA\n",
"output": "2\n"
},
{
"input": "35\nQLDZNKFXKVSVLUVHRTDPQYMSTDXBELXBOTS\n",
"output": "0\n"
},
{
"input": "1\nH\n",
"output": "0\n"
},
{
"input": "3\naaB\n",
"output": "1\n"
}
] | code_contests | python | 0.3 | f1fd106030e9b8eb1f56e2657405ce0c |
Vasya studies music.
He has learned lots of interesting stuff. For example, he knows that there are 12 notes: C, C#, D, D#, E, F, F#, G, G#, A, B, H. He also knows that the notes are repeated cyclically: after H goes C again, and before C stands H. We will consider the C note in the row's beginning and the C note after the H similar and we will identify them with each other. The distance between the notes along the musical scale is measured in tones: between two consecutive notes there's exactly one semitone, that is, 0.5 tone. The distance is taken from the lowest tone to the uppest one, that is, the distance between C and E is 4 semitones and between E and C is 8 semitones
Vasya also knows what a chord is. A chord is an unordered set of no less than three notes. However, for now Vasya only works with triads, that is with the chords that consist of exactly three notes. He can already distinguish between two types of triads — major and minor.
Let's define a major triad. Let the triad consist of notes X, Y and Z. If we can order the notes so as the distance along the musical scale between X and Y equals 4 semitones and the distance between Y and Z is 3 semitones, then the triad is major. The distance between X and Z, accordingly, equals 7 semitones.
A minor triad is different in that the distance between X and Y should be 3 semitones and between Y and Z — 4 semitones.
For example, the triad "C E G" is major: between C and E are 4 semitones, and between E and G are 3 semitones. And the triplet "C# B F" is minor, because if we order the notes as "B C# F", than between B and C# will be 3 semitones, and between C# and F — 4 semitones.
Help Vasya classify the triad the teacher has given to him.
Input
The only line contains 3 space-separated notes in the above-given notation.
Output
Print "major" if the chord is major, "minor" if it is minor, and "strange" if the teacher gave Vasya some weird chord which is neither major nor minor. Vasya promises you that the answer will always be unambiguous. That is, there are no chords that are both major and minor simultaneously.
Examples
Input
C E G
Output
major
Input
C# B F
Output
minor
Input
A B H
Output
strange
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | # Problem: 88A
# Time Created: August 10(Monday) 2020 || 11:37:58
#>-------------------------<#
import sys
input = sys.stdin.readline
#>-------------------------<#
from itertools import permutations
# Helper Functions. -> Don't cluster your code.
def check_chord(tup):
notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'B', 'H']
notes += notes
x, y, z = tup
x_y = 0
y_z = 0
for i in range(notes.index(x), len(notes)):
if notes[i] == y: break
else: x_y += 1
for i in range(notes.index(y), len(notes)):
if notes[i] == z: break
else: y_z += 1
if x_y == 4 and y_z == 3: return "major"
elif x_y == 3 and y_z == 4: return "minor"
else: return "strange"
# IO Functions. -> Input output
def io():
x, y, z = map(str, input().split())
perms = permutations([x,y,z])
return list(perms)
# Main functions. -> Write the main solution here
def solve():
perms = io()
ans = 'strange'
for perm in perms:
chord = check_chord(perm)
if chord != 'strange':
ans = chord
break
print(ans)
# Multiple test cases. -> When you have T test cases.
solve()
| python | code_algorithm | [
{
"input": "A B H\n",
"output": "strange\n"
},
{
"input": "C E G\n",
"output": "major\n"
},
{
"input": "C# B F\n",
"output": "minor\n"
},
{
"input": "E G# C\n",
"output": "strange\n"
},
{
"input": "D# C G\n",
"output": "minor\n"
},
{
"input": "F# H C\n",
"output": "strange\n"
},
{
"input": "C C# D\n",
"output": "strange\n"
},
{
"input": "D B E\n",
"output": "strange\n"
},
{
"input": "F# H D\n",
"output": "minor\n"
},
{
"input": "C F A\n",
"output": "major\n"
},
{
"input": "D B B\n",
"output": "strange\n"
},
{
"input": "A F F#\n",
"output": "strange\n"
},
{
"input": "A F D\n",
"output": "minor\n"
},
{
"input": "G# G# A\n",
"output": "strange\n"
},
{
"input": "A H B\n",
"output": "strange\n"
},
{
"input": "E A C#\n",
"output": "major\n"
},
{
"input": "A B C\n",
"output": "strange\n"
},
{
"input": "F E A\n",
"output": "strange\n"
},
{
"input": "A D F\n",
"output": "minor\n"
},
{
"input": "C G D#\n",
"output": "minor\n"
},
{
"input": "D# A E\n",
"output": "strange\n"
},
{
"input": "G F# A\n",
"output": "strange\n"
},
{
"input": "B E F#\n",
"output": "strange\n"
},
{
"input": "B G D#\n",
"output": "major\n"
},
{
"input": "D C B\n",
"output": "strange\n"
},
{
"input": "H D G\n",
"output": "major\n"
},
{
"input": "H B G\n",
"output": "strange\n"
},
{
"input": "A F E\n",
"output": "strange\n"
},
{
"input": "D H G\n",
"output": "major\n"
},
{
"input": "D# H F#\n",
"output": "major\n"
},
{
"input": "C# F B\n",
"output": "minor\n"
},
{
"input": "G# C# D\n",
"output": "strange\n"
},
{
"input": "F D# G#\n",
"output": "strange\n"
},
{
"input": "G D# B\n",
"output": "major\n"
},
{
"input": "F C F\n",
"output": "strange\n"
},
{
"input": "G# C# F\n",
"output": "major\n"
},
{
"input": "C E F\n",
"output": "strange\n"
},
{
"input": "F# F# F#\n",
"output": "strange\n"
},
{
"input": "A D F#\n",
"output": "major\n"
},
{
"input": "D A F\n",
"output": "minor\n"
},
{
"input": "C D C\n",
"output": "strange\n"
},
{
"input": "B C# C#\n",
"output": "strange\n"
},
{
"input": "C A H\n",
"output": "strange\n"
},
{
"input": "F H F\n",
"output": "strange\n"
},
{
"input": "F# H F#\n",
"output": "strange\n"
},
{
"input": "E E G#\n",
"output": "strange\n"
},
{
"input": "D F# H\n",
"output": "minor\n"
},
{
"input": "G D# C\n",
"output": "minor\n"
},
{
"input": "E F D#\n",
"output": "strange\n"
},
{
"input": "C B D#\n",
"output": "strange\n"
},
{
"input": "D# H G#\n",
"output": "minor\n"
},
{
"input": "D# C G#\n",
"output": "major\n"
},
{
"input": "C A E\n",
"output": "minor\n"
},
{
"input": "D A F#\n",
"output": "major\n"
},
{
"input": "D# B F#\n",
"output": "minor\n"
},
{
"input": "A G H\n",
"output": "strange\n"
},
{
"input": "H D F#\n",
"output": "minor\n"
},
{
"input": "G H E\n",
"output": "minor\n"
},
{
"input": "H G# D#\n",
"output": "minor\n"
},
{
"input": "H E G#\n",
"output": "major\n"
},
{
"input": "C G# G#\n",
"output": "strange\n"
},
{
"input": "A C F\n",
"output": "major\n"
},
{
"input": "E C G\n",
"output": "major\n"
},
{
"input": "F H E\n",
"output": "strange\n"
},
{
"input": "B F# G\n",
"output": "strange\n"
},
{
"input": "D# D# F#\n",
"output": "strange\n"
},
{
"input": "G# C# E\n",
"output": "minor\n"
},
{
"input": "C# F C\n",
"output": "strange\n"
},
{
"input": "D# B G\n",
"output": "major\n"
}
] | code_contests | python | 0 | 958660191f5ead8b21fcd8e83dcc425a |
Bessie is out grazing on the farm, which consists of n fields connected by m bidirectional roads. She is currently at field 1, and will return to her home at field n at the end of the day.
The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has k special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them.
After the road is added, Bessie will return home on the shortest path from field 1 to field n. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him!
Input
The first line contains integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, n-1 ≤ m ≤ 2 ⋅ 10^5, 2 ≤ k ≤ n) — the number of fields on the farm, the number of roads, and the number of special fields.
The second line contains k integers a_1, a_2, …, a_k (1 ≤ a_i ≤ n) — the special fields. All a_i are distinct.
The i-th of the following m lines contains integers x_i and y_i (1 ≤ x_i, y_i ≤ n, x_i ≠ y_i), representing a bidirectional road between fields x_i and y_i.
It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them.
Output
Output one integer, the maximum possible length of the shortest path from field 1 to n after Farmer John installs one road optimally.
Examples
Input
5 5 3
1 3 5
1 2
2 3
3 4
3 5
2 4
Output
3
Input
5 4 2
2 4
1 2
2 3
3 4
4 5
Output
3
Note
The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields 3 and 5, and the resulting shortest path from 1 to 5 is length 3.
<image>
The graph for the second example is shown below. Farmer John must add a road between fields 2 and 4, and the resulting shortest path from 1 to 5 is length 3.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
from collections import deque
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__
# v(G), e(G), special v
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
# adjacency lists
g = [[] for _ in range(n + 1)]
for _ in range(m):
v, to = map(int, input().split())
g[v].append(to)
g[to].append(v)
dist_1 = [float('inf')] * (n + 1)
s = 1 # start vertex
dist_1[s] = 0
queue = deque([s])
while queue:
v = queue.pop()
for to in g[v]:
if dist_1[to] > dist_1[v] + 1:
dist_1[to] = dist_1[v] + 1
queue.appendleft(to)
dist_n = [float('inf')] * (n + 1)
s = n # start vertex
dist_n[s] = 0
queue = deque([s])
while queue:
v = queue.pop()
for to in g[v]:
if dist_n[to] > dist_n[v] + 1:
dist_n[to] = dist_n[v] + 1
queue.appendleft(to)
a.sort(key=dist_1.__getitem__)
# print(dist_1)
# print(a)
# print(dist_n)
class SegmTree():
def __init__(self, size):
N = 1
while N < size:
N <<= 1
self.N = N
self.tree = [-float('inf')] * (2*self.N)
def modify(self, i, value):
i += self.N
self.tree[i] = value
while i > 1:
self.tree[i>>1] = max(self.tree[i], self.tree[i^1])
i >>= 1
def query_range(self, l, r):
l += self.N
r += self.N
result = -float('inf')
while l < r:
if l & 1:
result = max(result, self.tree[l])
l += 1
if r & 1:
r -= 1
result = max(result, self.tree[r])
l >>= 1
r >>= 1
return result
st = SegmTree(n + 1)
for v in a:
st.modify(v, dist_n[v])
longestPossible = 0
for v in a:
st.modify(v, -float('inf'))
dist = dist_1[v] + 1 + st.query_range(0, n + 1)
longestPossible = max(longestPossible, dist)
shortDist = min(dist_1[n], longestPossible)
print(shortDist)
| python | code_algorithm | [
{
"input": "5 4 2\n2 4\n1 2\n2 3\n3 4\n4 5\n",
"output": "3\n"
},
{
"input": "5 5 3\n1 3 5\n1 2\n2 3\n3 4\n3 5\n2 4\n",
"output": "3\n"
},
{
"input": "5 4 2\n3 4\n2 1\n2 3\n1 4\n4 5\n",
"output": "2\n"
},
{
"input": "4 3 2\n3 4\n1 2\n2 3\n1 4\n",
"output": "1\n"
},
{
"input": "16 15 3\n12 14 16\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n1 13\n13 14\n14 15\n15 16\n",
"output": "4\n"
},
{
"input": "10 10 2\n3 6\n1 2\n2 3\n3 4\n4 10\n1 5\n5 6\n6 7\n7 8\n8 9\n9 10\n",
"output": "4\n"
},
{
"input": "6 5 3\n1 4 6\n1 5\n5 6\n1 2\n2 3\n3 4\n",
"output": "2\n"
},
{
"input": "31 33 4\n3 9 16 26\n1 2\n2 3\n3 4\n4 5\n1 6\n6 7\n7 8\n8 9\n1 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n1 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n16 27\n26 28\n28 29\n29 30\n30 31\n5 31\n9 31\n27 31\n",
"output": "5\n"
},
{
"input": "5 4 2\n4 5\n1 2\n2 3\n3 4\n1 5\n",
"output": "1\n"
},
{
"input": "9 8 2\n4 5\n1 2\n2 3\n3 4\n4 9\n5 6\n6 7\n7 8\n8 9\n",
"output": "4\n"
},
{
"input": "12 11 3\n1 3 7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 12\n1 8\n8 9\n9 10\n10 11\n11 7\n",
"output": "6\n"
},
{
"input": "10 15 8\n6 9 8 1 2 7 4 5\n9 5\n3 6\n10 7\n7 4\n5 3\n8 2\n10 9\n3 4\n4 6\n5 4\n1 2\n7 3\n1 3\n7 6\n7 8\n",
"output": "3\n"
},
{
"input": "14 13 4\n2 4 10 13\n1 2\n2 3\n3 4\n4 14\n1 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n",
"output": "4\n"
},
{
"input": "42 41 20\n8 34 12 13 38 3 9 10 18 16 2 26 24 17 32 21 4 27 15 39\n1 35\n35 8\n1 31\n31 34\n1 40\n40 12\n1 30\n30 13\n1 33\n33 38\n1 37\n37 3\n1 22\n22 9\n1 20\n20 10\n1 25\n25 18\n1 14\n14 16\n1 42\n42 7\n7 2\n42 11\n11 26\n42 29\n29 24\n42 36\n36 17\n42 41\n41 32\n42 23\n23 21\n42 28\n28 4\n42 5\n5 27\n42 6\n6 15\n42 19\n19 39\n",
"output": "1\n"
},
{
"input": "10 11 2\n7 4\n1 2\n1 3\n2 3\n2 6\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n",
"output": "6\n"
},
{
"input": "15 15 3\n8 10 14\n1 8\n8 3\n3 4\n4 15\n1 5\n5 6\n6 10\n10 15\n1 2\n2 9\n9 7\n7 11\n11 12\n12 13\n13 14\n",
"output": "4\n"
},
{
"input": "7 7 2\n3 6\n1 2\n1 4\n2 3\n4 5\n3 7\n5 7\n4 6\n",
"output": "3\n"
},
{
"input": "2 1 2\n1 2\n1 2\n",
"output": "1\n"
},
{
"input": "5 4 3\n1 3 5\n1 2\n2 3\n3 4\n4 5\n",
"output": "3\n"
}
] | code_contests | python | 0 | 3cf020273e7e3fa6b0cc29ea650384ee |
Slime has a sequence of positive integers a_1, a_2, …, a_n.
In one operation Orac can choose an arbitrary subsegment [l … r] of this sequence and replace all values a_l, a_{l + 1}, …, a_r to the value of median of \\{a_l, a_{l + 1}, …, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the ⌊ (|s|+1)/(2)⌋-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = … = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1≤ n≤ 100 000) and k\ (1≤ k≤ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1≤ a_i≤ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10].
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | # Template 1.0
import sys, re, math
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx): return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
toret = {}
for tup in temp:
toret[tup[0]] = tup[1]
return toret
def sortDictWithKey(passedDic):
return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
def check(idx):
up = down = 0
a1,a2,a3 = sorted(a[idx:idx+3])
return (a1<=a2<=a3 and a2>=k)
t = INT()
while(t!=0):
n,k = MAP()
a = LIST()
if(k in a):
flag = 1
else:
flag = 0
if(flag==1):
for patchStart in range(n-2):
if(check(patchStart)):
flag = 1
break
else:
flag=0
continue
if(n==2):
foo = 0
for el in a:
if(el>=k):
foo+=1
if(foo==2):
print("yes")
else:
print("no")
else:
if (flag == 0):
print("no")
else:
print("yes")
else:
print("no")
t-=1 | python | code_algorithm | [
{
"input": "5\n5 3\n1 5 2 6 1\n1 6\n6\n3 2\n1 2 3\n4 3\n3 1 2 3\n10 3\n1 2 3 4 5 6 7 8 9 10\n",
"output": "no\nyes\nyes\nno\nyes\n"
},
{
"input": "1\n7 2\n5 5 1 1 2 1 1\n",
"output": "yes\n"
},
{
"input": "1\n26 2\n2 1 1 1 3 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1\n",
"output": "yes\n"
},
{
"input": "1\n17 15\n3 5 4 6 10 2 16 10 18 8 5 1 2 7 15 14 8\n",
"output": "yes\n"
},
{
"input": "1\n10 10\n10 1 1 1 1 1 1 11 1 11\n",
"output": "yes\n"
},
{
"input": "1\n7 3\n3 1 1 1 4 1 4\n",
"output": "yes\n"
},
{
"input": "1\n7 2\n3 3 1 1 2 1 1\n",
"output": "yes\n"
},
{
"input": "1\n9 5\n6 4 6 1 1 1 1 1 5\n",
"output": "yes\n"
},
{
"input": "1\n14 3\n1 1 10 1 13 1 1 1 1 1 3 1 1 3\n",
"output": "yes\n"
},
{
"input": "1\n7 3\n4 1 4 1 1 1 3\n",
"output": "yes\n"
},
{
"input": "1\n7 3\n3 2 2 2 4 2 4\n",
"output": "yes\n"
},
{
"input": "1\n6 3\n5 1 5 2 2 3\n",
"output": "yes\n"
},
{
"input": "1\n10 2\n1 1 2 1 1 1 1 5 1 5\n",
"output": "yes\n"
},
{
"input": "1\n6 2\n3 1 3 1 1 2\n",
"output": "yes\n"
},
{
"input": "2\n1 1\n1000000000\n3 1000000000\n1000000000 996 251\n",
"output": "no\nno\n"
},
{
"input": "1\n5 2\n2 1 5 1 1\n",
"output": "yes\n"
},
{
"input": "1\n7 2\n3 1 3 1 1 1 2\n",
"output": "yes\n"
},
{
"input": "1\n10 2\n2 1 1 1 1 1 1 1 3 3\n",
"output": "yes\n"
},
{
"input": "1\n6 5\n5 1 1 7 1 7\n",
"output": "yes\n"
},
{
"input": "1\n6 3\n4 1 4 1 1 3\n",
"output": "yes\n"
},
{
"input": "1\n7 2\n2 1 1 1 3 1 3\n",
"output": "yes\n"
},
{
"input": "1\n6 3\n3 1 1 6 1 6\n",
"output": "yes\n"
},
{
"input": "1\n9 3\n3 1 1 1 1 1 4 1 4\n",
"output": "yes\n"
},
{
"input": "1\n10 2\n3 1 3 1 1 1 1 1 1 2\n",
"output": "yes\n"
},
{
"input": "1\n7 10\n15 1 15 1 1 1 10\n",
"output": "yes\n"
}
] | code_contests | python | 0 | 40a0a656ce842462c68616f2e1c4fc91 |
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that ∑_{i=1}^{n}{∑_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t — the number of test cases (1 ≤ t ≤ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 ≤ n ≤ 100, 0 ≤ m ≤ 10^6). The second line contains integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^6) — the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum ∑_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | #import numpy as np
#import collections
#import sys
# =============================================================================
#def get_primeFactor(n):
# res=[1]
# x=2
# while x*x<=n:
# while n%x==0:
# res.append(x)
# n//=x
# x+=1
# if n>1:res.append(n)
# return res
#
#def getFactor(n):
# res={1,n}
# x=2
# while x*x<=n:
# if n%x==0:
# res.add(x)
# res.add(n//x)
# x+=1
# return res
#for _ in range(int(input())):
# r,b,g,w=tuple(map(int,input().split()))
# f=tuple(filter(lambda x:x%2!=0,(r,b,g,w)))
# if len(f)<=1 or len(f)==4:print("Yes")
# elif len(f)==2:print("No")
# else:
# if w%2==0:print("Yes")
# elif all(t!=0 for t in (r,b,g) ):print("Yes")
# else :print("No")
#
for _ in range(int(input())):
n,m=map(int,input().split())
ar=map(int,input().split())
total=sum(ar)
if total != m:print("NO")
else: print("YES")
| python | code_algorithm | [
{
"input": "2\n3 8\n2 5 1\n4 4\n0 1 2 3\n",
"output": "YES\nNO\n"
},
{
"input": "1\n3 6\n4 4 4\n",
"output": "NO\n"
},
{
"input": "1\n3 2\n1 2 3\n",
"output": "NO\n"
},
{
"input": "1\n3 16\n2 5 1\n",
"output": "NO\n"
},
{
"input": "1\n1 2\n4\n",
"output": "NO\n"
},
{
"input": "1\n4 8\n4 4 4 4\n",
"output": "NO\n"
},
{
"input": "1\n3 2\n2 2 2\n",
"output": "NO\n"
},
{
"input": "1\n2 8\n8 8\n",
"output": "NO\n"
},
{
"input": "1\n1 0\n5\n",
"output": "NO\n"
},
{
"input": "1\n1 0\n1\n",
"output": "NO\n"
},
{
"input": "2\n3 18\n3 3 3\n4 26\n10 10 2 4\n",
"output": "NO\nYES\n"
},
{
"input": "1\n3 8\n2 4 10\n",
"output": "NO\n"
},
{
"input": "2\n4 5\n5 5 5 5\n6 3\n3 3 3 3 3 3\n",
"output": "NO\nNO\n"
},
{
"input": "1\n3 1\n1 1 1\n",
"output": "NO\n"
},
{
"input": "1\n2 3\n3 6\n",
"output": "NO\n"
},
{
"input": "1\n3 8\n10 13 9\n",
"output": "NO\n"
},
{
"input": "1\n2 2\n2 2\n",
"output": "NO\n"
},
{
"input": "1\n1 0\n2\n",
"output": "NO\n"
},
{
"input": "1\n5 5\n2 2 2 2 2\n",
"output": "NO\n"
},
{
"input": "1\n4 6\n3 3 3 3\n",
"output": "NO\n"
},
{
"input": "1\n4 4\n0 0 0 0\n",
"output": "NO\n"
},
{
"input": "1\n3 0\n0 0 0\n",
"output": "YES\n"
},
{
"input": "1\n3 8\n15 8 1\n",
"output": "NO\n"
},
{
"input": "1\n6 1\n88 9 9 9 9 9\n",
"output": "NO\n"
},
{
"input": "1\n1 1\n0\n",
"output": "NO\n"
},
{
"input": "1\n3 3\n2 2 2\n",
"output": "NO\n"
},
{
"input": "1\n2 3\n6 9\n",
"output": "NO\n"
},
{
"input": "1\n3 8\n16 0 0\n",
"output": "NO\n"
},
{
"input": "1\n1 1000\n100000\n",
"output": "NO\n"
},
{
"input": "1\n3 2\n0 0 0\n",
"output": "NO\n"
},
{
"input": "1\n1 1412\n2824\n",
"output": "NO\n"
},
{
"input": "1\n2 2\n2 4\n",
"output": "NO\n"
},
{
"input": "1\n3 5\n5 10 15\n",
"output": "NO\n"
},
{
"input": "1\n1 1\n2\n",
"output": "NO\n"
},
{
"input": "2\n3 3\n3 3 3\n4 2\n10 10 2 4\n",
"output": "NO\nNO\n"
},
{
"input": "1\n5 1\n1 1 1 1 1\n",
"output": "NO\n"
},
{
"input": "1\n3 2\n1 1 1\n",
"output": "NO\n"
},
{
"input": "1\n3 3\n0 0 0\n",
"output": "NO\n"
},
{
"input": "5\n4 10\n5 5 5 5\n3 5\n5 5 5\n3 4\n10 1 1\n4 5\n5 5 5 5\n5 5\n5 5 5 5 5\n",
"output": "NO\nNO\nNO\nNO\nNO\n"
}
] | code_contests | python | 0.9 | 88604afd735111095381c1e2afc03b12 |
One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string s or different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.
The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|.
A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a ≠ c or b ≠ d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same.
A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different.
Input
The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters.
Output
Print a single number — the number of different pairs "x y" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Examples
Input
aa
aa
Output
5
Input
codeforces
forceofcode
Output
60
Note
Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1] t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]".
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | from sys import stdin
s=[ord(i)-97 for i in stdin.readline().strip()]
s1=[ord(i)-97 for i in stdin.readline().strip()]
n=len(s)
m=len(s1)
mod=1000000007
dp=[[0 for i in range(n)] for j in range(26)]
for i in range(m):
arr=[0 for j in range(n)]
for j in range(n):
if s1[i]==s[j] :
arr[j]=1
if j>0 :
arr[j]=(arr[j]+dp[s[j-1]][j-1])%mod
for j in range(n):
dp[s1[i]][j]=(arr[j] +dp[s1[i]][j]) %mod
x=0
for i in dp:
for j in i:
x=(x+j)%mod
print(x)
| python | code_algorithm | [
{
"input": "codeforces\nforceofcode\n",
"output": "60\n"
},
{
"input": "aa\naa\n",
"output": "5\n"
},
{
"input": "bbabb\nbababbbbab\n",
"output": "222\n"
},
{
"input": "ab\nbbbba\n",
"output": "5\n"
},
{
"input": "xzzxxxzxzzzxzzzxxzzxzzxzxzxxzxxzxxzxzzxxzxxzxxxzxzxzxxzzxxxxzxzzzxxxzxzxxxzzxxzxxzxxzzxxzxxzxzxzzzxzzzzxzxxzzxzxxzxxzzxzxzx\nzzx\n",
"output": "291\n"
},
{
"input": "a\nb\n",
"output": "0\n"
},
{
"input": "zxzxzxzxzxzxzx\nd\n",
"output": "0\n"
},
{
"input": "pfdempfohomnpgbeegikfmflnalbbajpnpgeacaicoehopgnabnklheepnlnflohjegcciflmfjhachnhekckfjgoffhkblncidn\nidlhklpclcghngeggpjdgefccihndpoikojdjnbkdfjgaanoolfmnifnmbpeeghpicehjdiipnlfnjpglidpnnnjfmjfhbcogojkcfflmmfcgajbjbfaikhmjofbnjnaolbcdkelcieeodbfjcfiblhhmeelmpcmmcdhjcnnfklgedjjbaljndjonanlojclboeelkab\n",
"output": "26774278\n"
},
{
"input": "ababababab\nababb\n",
"output": "74\n"
},
{
"input": "zxx\nxzzxxxxzzzxxxzzxxxzxxxzzxxzxxxzzxzxxzxxzxxzxzxxzxxxxzzxxzzxzzxxzzxxzxzxzxxzxzxzxxxzxxzzxxzxxxxzzxxxzxxxzxzxzzxzzxxxxzzxxzxz\n",
"output": "46917\n"
},
{
"input": "ababbabbab\nababb\n",
"output": "75\n"
},
{
"input": "coderscontest\ncodeforces\n",
"output": "39\n"
},
{
"input": "b\nab\n",
"output": "1\n"
},
{
"input": "sbypoaavsbqxfiqvpbjyimhzotlxuramhdamvyobsgaehwhtfdvgvlxpophtvzrmvyxwyzeauyatzitsqvlabufbcefaivwzoccfvhrdbzlmzoofczqzbxoqioctzzxqksuorhnldrfavlhyfyobvnqsyegsbvlusxchixbddzbwwnvuulcarguxvnvzkdqcjxxdetll\nlbawrainjjdgkdmwkqzxlwxhzjyrxbuwjhnvjlnmgfyrxdynsanvibfhngsioisbyldaplituqhebeicpsyerpiiqpjtnxvuotjv\n",
"output": "8095\n"
},
{
"input": "ab\na\n",
"output": "1\n"
},
{
"input": "abbbccbba\nabcabc\n",
"output": "33\n"
}
] | code_contests | python | 0 | 98b711b6b09d572c6e7fa896f8239a9e |
The Zoo in the Grid Kingdom is represented by an infinite grid. The Zoo has n observation binoculars located at the OX axis. For each i between 1 and n, inclusive, there exists a single binocular located at the point with coordinates (i, 0). There are m flamingos in the Zoo, located at points with positive coordinates. The flamingos are currently sleeping and you can assume that they don't move.
In order to get a good view over the flamingos, each of the binoculars can be independently rotated to face any angle (not necessarily integer). Then, the binocular can be used to observe all flamingos that is located at the straight line passing through the binocular at the angle it is set. In other words, you can assign each binocular a direction corresponding to any straight line passing through the binocular, and the binocular will be able to see all flamingos located on that line.
Today, some kids from the prestigious Codeforces kindergarten went on a Field Study to the Zoo. Their teacher would like to set each binocular an angle to maximize the number of flamingos that can be seen by the binocular. The teacher is very interested in the sum of these values over all binoculars. Please help him find this sum.
Input
The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 250), denoting the number of binoculars and the number of flamingos, respectively.
Then m lines follow, the i-th line will contain two space-separated integers xi and yi (1 ≤ xi, yi ≤ 109), which means that the i-th flamingo is located at point (xi, yi).
All flamingos will be located at distinct points.
Output
Print a single integer denoting the maximum total number of flamingos that can be seen by all the binoculars.
Examples
Input
5 5
2 1
4 1
3 2
4 3
4 4
Output
11
Note
This picture shows the answer to the example test case.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def gcd(a,b):
if b == 0:
return a
return gcd(b, a % b)
def normalize_rational(num,den):
#associate the -ve with the num or cancel -ve sign when both are -ve
if num ^ den < 0 and num > 0 or num ^ den > 0 and num < 0:
num = -num; den = -den
#put it in its simplest form
g = gcd(abs(num),abs(den))
num //= g; den //=g
return (num,den)
from sys import *
l = stdin.readline()
(n,m) = (int(tkn) for tkn in l.split())
xs = [0] * m; ys = [0] * m
#maxhits = [set() for i in range(n+1)]
maxhits = [1] * (n + 1)
maxhits[0] = 0
for i in range(m):
l = stdin.readline()
(x,y) = (int(tkn) for tkn in l.split())
xs[i] = x; ys[i] = y
line_to_points = {}
for i in range(m):
for j in range(m):
#m = dy/dx; y = (dy/dx)x + c ==> y.dx = x.dy + c.dx
#y.dx = x.dy + c'
#c' = y.dx - x.dy
#c' = ys[i]*dx - xs[i]*dy
#Now, at y = 0, x = -c' / dy
dy = ys[i] - ys[j]; dx = xs[i] - xs[j]
if dy == 0:
continue
#not a special case anymore
# if dx == 0:
# if xs[i] > n:
# continue
# else:
# count_seen_from_x = len([x for x in xs if x == xs[i]])
# maxhits[xs[i]] = max(count_seen_from_x, maxhits[xs[i]])
else:
slope = normalize_rational(dy,dx)
c_prime = ys[i]*dx - xs[i]*dy
x_intercept = -c_prime / dy
line = (slope,x_intercept)
if line in line_to_points:
#print("line: ", line)
#print("now: ", points)
points = line_to_points[line]
points.add(i); points.add(j)
#print("after addition: ", points)
continue
#if (i == 1 and j == 2):
# print(c_prime, x_intercept, dy, dx)
if int(x_intercept) == x_intercept and x_intercept <= n and x_intercept > 0:
points = set([i,j])
line_to_points[line] = points
#maxhits[int(x_intercept)] = points
# count_on_line = 2
# for k in range(m):
# if k != i and k != j and ys[k] * dx == xs[k]*dy + c_prime:
# count_on_line += 1
# maxhits[int(x_intercept)] = max(count_on_line, maxhits[int(x_intercept)])
for line,points in line_to_points.items():
x_intercept = int(line[1])
maxhits[x_intercept] = max(maxhits[x_intercept],len(points))
#print(maxhits)
#print(sum([max(len(points),1) for points in maxhits]) - 1)
print(sum(maxhits)) | python | code_algorithm | [
{
"input": "5 5\n2 1\n4 1\n3 2\n4 3\n4 4\n",
"output": "11\n"
},
{
"input": "1000000 2\n194305 1024\n4388610 1023\n",
"output": "1000000\n"
},
{
"input": "3 3\n227495634 254204506\n454991267 508409012\n217792637 799841973\n",
"output": "4\n"
},
{
"input": "1000000 2\n136395332 110293751\n568110113 459392523\n",
"output": "1000000\n"
},
{
"input": "3 3\n1 1\n2 10\n3 100\n",
"output": "3\n"
},
{
"input": "1000000 2\n881456674 979172365\n878302062 975668042\n",
"output": "1000000\n"
},
{
"input": "3 3\n333333334 1\n666666667 2\n1000000000 3\n",
"output": "5\n"
},
{
"input": "3 3\n2 333333333\n3 666666666\n4 999999999\n",
"output": "5\n"
},
{
"input": "4 5\n1 3\n2 2\n3 1\n4 2\n4 3\n",
"output": "7\n"
},
{
"input": "3 3\n227495634 254204506\n454991267 508409012\n715803819 799841973\n",
"output": "4\n"
},
{
"input": "3 3\n333333334 1\n666666667 2\n999999999 3\n",
"output": "5\n"
},
{
"input": "3 6\n1 1\n1 2\n1 3\n2 1\n2 2\n3 1\n",
"output": "7\n"
},
{
"input": "3 3\n2 333333333\n3 666666666\n4 1000000000\n",
"output": "4\n"
},
{
"input": "3 3\n2 333333333\n3 666666666\n4 999999998\n",
"output": "4\n"
},
{
"input": "3 3\n96684705 23204141\n193369409 46408282\n217792636 52269809\n",
"output": "4\n"
},
{
"input": "1 2\n450000001 500000000\n900000001 1000000000\n",
"output": "2\n"
},
{
"input": "3 10\n1000000000 1000000000\n1000000000 999999999\n1000000000 999999998\n1000000000 999999997\n1000000000 999999996\n1000000000 999999995\n1000000000 999999994\n1000000000 999999993\n1000000000 999999992\n1000000000 999999991\n",
"output": "3\n"
},
{
"input": "5 5\n2 1\n1 1\n3 1\n4 1\n4 4\n",
"output": "6\n"
}
] | code_contests | python | 0 | 21c36fbd297027f041bec33042b2db58 |
Mr. Bender has a digital table of size n × n, each cell can be switched on or off. He wants the field to have at least c switched on squares. When this condition is fulfilled, Mr Bender will be happy.
We'll consider the table rows numbered from top to bottom from 1 to n, and the columns — numbered from left to right from 1 to n. Initially there is exactly one switched on cell with coordinates (x, y) (x is the row number, y is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on.
For a cell with coordinates (x, y) the side-adjacent cells are cells with coordinates (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1).
In how many seconds will Mr. Bender get happy?
Input
The first line contains four space-separated integers n, x, y, c (1 ≤ n, c ≤ 109; 1 ≤ x, y ≤ n; c ≤ n2).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
6 4 3 1
Output
0
Input
9 3 8 10
Output
2
Note
Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. <image>.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | x, y, n, c = 0, 0, 0, 0
def suma_impares(m):
return m * m
def suma_n(m):
return m * (m - 1) // 2
def cnt(t):
u, d, l, r = x + t, x - t, y - t, y + t
suma = t ** 2 + (t + 1) ** 2
if u > n: suma -= suma_impares(u - n)
if d < 1: suma -= suma_impares(1 - d)
if l < 1: suma -= suma_impares(1 - l)
if r > n: suma -= suma_impares(r - n)
if 1 - l > x - 1 and 1 - d > y - 1:
suma += suma_n(2 - l - x)
if r - n > x - 1 and 1 - d > n - y:
suma += suma_n(r - n - x + 1)
if 1 - l > n - x and u - n > y - 1:
suma += suma_n(1 - l - n + x)
if u - n > n - y and r - n > n - x:
suma += suma_n(u - n - n + y)
return suma
n, x, y, c = input().split()
n, x, y, c = int(n), int(x), int(y), int(c)
#for i in range(10):
# print(i, cnt(i))
ini, fin = 0, int(1e9)
cont = int(1e9)
while cont > 0:
m = ini
paso = cont // 2
m += paso
if cnt(m) < c:
ini = m + 1
cont -= paso + 1
else:
cont = paso
print(ini) | python | code_algorithm | [
{
"input": "9 3 8 10\n",
"output": "2\n"
},
{
"input": "6 4 3 1\n",
"output": "0\n"
},
{
"input": "1000000000 44 30 891773002\n",
"output": "42159\n"
},
{
"input": "1000000000 999999938 999999936 384381709\n",
"output": "27600\n"
},
{
"input": "1000000000 999999946 60 715189365\n",
"output": "37707\n"
},
{
"input": "1000000000 999999946 999999941 715189365\n",
"output": "37707\n"
},
{
"input": "1000000 951981 612086 60277\n",
"output": "174\n"
},
{
"input": "1000000000 6 999999904 272656295\n",
"output": "23250\n"
},
{
"input": "1000000000 85 999999940 857945620\n",
"output": "41279\n"
},
{
"input": "1000000000 999999946 85 423654797\n",
"output": "28970\n"
},
{
"input": "71036059 25478942 38920202 19135721\n",
"output": "3093\n"
},
{
"input": "848 409 661 620581\n",
"output": "771\n"
},
{
"input": "8 8 3 1\n",
"output": "0\n"
},
{
"input": "9 3 8 55\n",
"output": "7\n"
},
{
"input": "1000000000 6 97 272656295\n",
"output": "23250\n"
},
{
"input": "1000000000 55 85 423654797\n",
"output": "28970\n"
},
{
"input": "548813503 532288332 26800940 350552333\n",
"output": "13239\n"
},
{
"input": "1000000000 999999916 61 857945620\n",
"output": "41279\n"
},
{
"input": "72 40 68 849\n",
"output": "25\n"
},
{
"input": "8 2 6 10\n",
"output": "2\n"
},
{
"input": "522 228 495 74535\n",
"output": "249\n"
},
{
"input": "1000000000 999999916 999999940 857945620\n",
"output": "41279\n"
},
{
"input": "813 154 643 141422\n",
"output": "299\n"
},
{
"input": "800 305 317 414868\n",
"output": "489\n"
},
{
"input": "9 8 2 10\n",
"output": "2\n"
},
{
"input": "847251738 695702891 698306947 648440371\n",
"output": "18006\n"
},
{
"input": "1000000000 999999938 65 384381709\n",
"output": "27600\n"
},
{
"input": "1000000000 63 999999936 384381709\n",
"output": "27600\n"
},
{
"input": "9 8 2 50\n",
"output": "7\n"
},
{
"input": "812168727 57791401 772019566 644719499\n",
"output": "17954\n"
},
{
"input": "1 1 1 1\n",
"output": "0\n"
},
{
"input": "1000000000 999999957 30 891773002\n",
"output": "42159\n"
},
{
"input": "1000000000 504951981 646612086 602763371\n",
"output": "17360\n"
},
{
"input": "737 231 246 79279\n",
"output": "199\n"
},
{
"input": "892 364 824 53858\n",
"output": "183\n"
},
{
"input": "549 198 8 262611\n",
"output": "635\n"
},
{
"input": "1000000000 55 60 715189365\n",
"output": "37707\n"
},
{
"input": "1000000000 55 999999916 423654797\n",
"output": "28970\n"
},
{
"input": "891773002 152235342 682786380 386554406\n",
"output": "13902\n"
},
{
"input": "8 1 2 64\n",
"output": "13\n"
},
{
"input": "1000000000 55 999999941 715189365\n",
"output": "37707\n"
},
{
"input": "1000000000 63 65 384381709\n",
"output": "27600\n"
},
{
"input": "9 4 3 73\n",
"output": "8\n"
},
{
"input": "1000000000 999999946 999999916 423654797\n",
"output": "28970\n"
},
{
"input": "1000000000 44 999999971 891773002\n",
"output": "42159\n"
},
{
"input": "6 4 3 36\n",
"output": "6\n"
},
{
"input": "1000000 587964 232616 62357\n",
"output": "177\n"
},
{
"input": "721 112 687 232556\n",
"output": "556\n"
},
{
"input": "1000000000 85 61 857945620\n",
"output": "41279\n"
},
{
"input": "1000000000 81587964 595232616 623563697\n",
"output": "17657\n"
},
{
"input": "1000000 948438 69861 89178\n",
"output": "211\n"
},
{
"input": "8 2 6 20\n",
"output": "3\n"
},
{
"input": "6 1 4 10\n",
"output": "3\n"
},
{
"input": "9 4 3 10\n",
"output": "2\n"
},
{
"input": "6 1 4 15\n",
"output": "3\n"
},
{
"input": "8 1 2 10\n",
"output": "3\n"
},
{
"input": "958 768 649 298927\n",
"output": "431\n"
},
{
"input": "10 7 2 7\n",
"output": "2\n"
}
] | code_contests | python | 0 | ec529b249cf8c2375c8ee3544a89346f |
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n × m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 ≤ n, m ≤ 106; 1 ≤ i ≤ n; 1 ≤ j ≤ m; 1 ≤ a, b ≤ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer — the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n, m, i, j, a, b = map(int, input().split())
corners = [(1, 1), (1, m), (n, 1), (n, m)]
result = False
ans = -1
for cnt in corners:
if (abs(cnt[0] - i) % a == 0) and (abs(cnt[1] - j) % b == 0):
result = True
t1, t2 = abs(cnt[0] - i) // a, abs(cnt[1] - j) // b
if (t1 + t2) % 2 == 0:
if ans == -1:
ans = max(abs(cnt[0] - i) // a, abs(cnt[1] - j) // b)
else:
ans = min(max(abs(cnt[0] - i) // a, abs(cnt[1] - j) // b), ans)
if ans == 0:
print(ans)
elif not result or (i+a > n and i-a < 1) or (j+b > m and j-b < 1) or ans == -1:
print("Poor Inna and pony!")
else:
print(ans) | python | code_algorithm | [
{
"input": "5 5 2 3 1 1\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "5 7 1 3 2 2\n",
"output": "2\n"
},
{
"input": "2 6 1 2 2 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "100 100 50 50 500 500\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "1000000 2 2 2 2 1\n",
"output": "499999\n"
},
{
"input": "33999 99333 33000 99000 3 9\n",
"output": "333\n"
},
{
"input": "5 1 3 1 1 1\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "6 1 5 1 2 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "100 100 70 5 1 1\n",
"output": "30\n"
},
{
"input": "1 100 1 50 1 50\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "11 11 3 3 4 4\n",
"output": "2\n"
},
{
"input": "5 8 4 2 1 2\n",
"output": "3\n"
},
{
"input": "1000000 1000000 1000000 1000000 1000000 1000000\n",
"output": "0\n"
},
{
"input": "5 4 2 3 2 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "1000000 99999 12345 23456 23 54\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "5 8 4 1 2 1\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "2 10 1 5 2 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "4 4 3 4 1 5\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "1000 1000 1 3 10000 1\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "2 6 1 2 6 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "50000 100000 500 1000 500 500\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "5 5 4 4 1 1\n",
"output": "1\n"
},
{
"input": "50000 100000 500 1000 500 1000\n",
"output": "99\n"
},
{
"input": "5 5 4 3 1 2\n",
"output": "1\n"
},
{
"input": "304 400 12 20 4 4\n",
"output": "95\n"
},
{
"input": "1 1 1 1 1 1\n",
"output": "0\n"
},
{
"input": "2 20 2 5 2 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "3 3 2 2 2 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "5 4 2 3 1 1\n",
"output": "1\n"
},
{
"input": "7 1 5 1 2 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "1 5 1 3 10 1\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "2 8 1 2 1 3\n",
"output": "2\n"
},
{
"input": "1004 999004 4 4 5 5\n",
"output": "199800\n"
},
{
"input": "99999 99999 1 2 1 1\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "23000 15500 100 333 9 1\n",
"output": "15167\n"
},
{
"input": "1000000 1000000 500000 500000 1 1\n",
"output": "499999\n"
},
{
"input": "1000 2000 100 200 90 90\n",
"output": "20\n"
},
{
"input": "1 5 1 3 1 1\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "5 5 1 3 1 2\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "5 5 4 2 1 1\n",
"output": "1\n"
},
{
"input": "1000 1 1 1 1 500\n",
"output": "0\n"
},
{
"input": "2347 2348 234 48 238 198\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "500000 100000 400 80000 2 2\n",
"output": "249800\n"
},
{
"input": "50000 100000 500 1000 500 2000\n",
"output": "Poor Inna and pony!\n"
},
{
"input": "5 7 1 3 1 2\n",
"output": "2\n"
},
{
"input": "1000 1000 10 15 10 5\n",
"output": "197\n"
}
] | code_contests | python | 0 | 845ccd1b3699904459d37e08a7c668a6 |
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer — the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def iterative_dfs(graph, start, path=[]):
visited = {}
for i in graph:
visited[i] = []
q=[start]
while q:
v=q.pop(0)
if not visited[v]:
visited[v] = True
path=path+[v]
q=graph[v]+q
return path
nodes, edges = map(int, input().split(' '))
graph = {}
for i in range(nodes):
graph[i] = []
for i in range(edges):
a, b = map(int, input().split(' '))
graph[a-1].append(b-1)
graph[b-1].append(a-1)
marked = [False] * nodes
num = 0
for i in range(nodes):
if not marked[i]:
for j in iterative_dfs(graph, i):
marked[j] = True
num += 1
print(2**(nodes-num))
| python | code_algorithm | [
{
"input": "3 2\n1 2\n2 3\n",
"output": "4\n"
},
{
"input": "2 1\n1 2\n",
"output": "2\n"
},
{
"input": "1 0\n",
"output": "1\n"
},
{
"input": "26 17\n1 2\n2 3\n1 6\n6 7\n7 8\n2 9\n4 10\n3 11\n11 12\n9 13\n6 14\n2 16\n5 18\n6 19\n11 22\n15 24\n6 26\n",
"output": "131072\n"
},
{
"input": "8 5\n1 2\n1 3\n1 4\n5 6\n7 8\n",
"output": "32\n"
},
{
"input": "10 10\n1 8\n4 10\n4 6\n5 10\n2 3\n1 7\n3 4\n3 6\n6 9\n3 7\n",
"output": "512\n"
},
{
"input": "40 28\n1 2\n2 4\n3 5\n1 7\n1 8\n7 9\n6 10\n7 11\n2 12\n9 13\n11 15\n12 16\n1 18\n10 19\n7 21\n7 23\n20 25\n24 27\n14 28\n9 29\n23 30\n27 31\n11 34\n21 35\n32 36\n23 38\n7 39\n20 40\n",
"output": "268435456\n"
},
{
"input": "50 50\n16 21\n23 47\n23 30\n2 12\n23 41\n3 16\n14 20\n4 49\n2 47\n19 29\n13 42\n5 8\n24 38\n13 32\n34 37\n38 46\n3 20\n27 50\n7 42\n33 45\n2 48\n41 47\n9 48\n15 26\n27 37\n32 34\n17 24\n1 39\n27 30\n10 33\n38 47\n32 33\n14 39\n35 50\n2 19\n3 12\n27 34\n18 25\n12 23\n31 44\n5 35\n28 45\n38 39\n13 44\n34 38\n16 46\n5 15\n26 30\n47 49\n2 10\n",
"output": "4398046511104\n"
},
{
"input": "20 20\n6 8\n13 20\n7 13\n6 17\n5 15\n1 12\n2 15\n5 17\n5 14\n6 14\n12 20\n7 20\n1 6\n1 7\n2 19\n14 17\n1 10\n11 15\n9 18\n2 12\n",
"output": "32768\n"
},
{
"input": "50 0\n",
"output": "1\n"
},
{
"input": "20 15\n1 3\n3 4\n3 5\n4 6\n1 7\n1 8\n1 9\n7 11\n8 12\n5 13\n3 16\n1 17\n3 18\n1 19\n17 20\n",
"output": "32768\n"
},
{
"input": "40 40\n28 33\n15 21\n12 29\n14 31\n2 26\n3 12\n25 34\n6 30\n6 25\n5 28\n9 17\n23 29\n30 36\n3 21\n35 37\n7 25\n29 39\n15 19\n12 35\n24 34\n15 25\n19 33\n26 31\n7 29\n1 40\n11 27\n6 9\n6 27\n36 39\n10 14\n6 16\n23 25\n2 38\n3 24\n30 31\n29 30\n4 12\n11 13\n14 40\n22 39\n",
"output": "34359738368\n"
},
{
"input": "5 4\n1 2\n2 3\n3 4\n4 5\n",
"output": "16\n"
},
{
"input": "30 24\n2 3\n3 4\n1 5\n4 6\n6 7\n1 8\n1 9\n4 10\n9 11\n5 12\n6 13\n10 14\n14 15\n12 16\n14 17\n2 18\n8 19\n3 20\n10 21\n11 24\n3 25\n1 26\n7 27\n4 29\n",
"output": "16777216\n"
},
{
"input": "50 41\n1 3\n1 4\n2 5\n2 7\n1 8\n2 10\n4 11\n5 12\n12 13\n4 14\n10 17\n1 18\n1 21\n5 22\n14 23\n19 24\n13 25\n3 26\n11 27\n6 28\n26 29\n21 30\n17 31\n15 32\n1 33\n12 34\n23 36\n6 37\n15 38\n37 39\n31 40\n15 41\n25 42\n19 43\n20 44\n32 45\n44 46\n31 47\n2 48\n32 49\n27 50\n",
"output": "2199023255552\n"
},
{
"input": "50 38\n1 2\n2 3\n3 4\n3 5\n4 7\n5 10\n9 11\n9 12\n11 13\n12 14\n6 15\n8 16\n2 18\n15 19\n3 20\n10 21\n4 22\n9 24\n2 25\n23 26\n3 28\n20 29\n14 30\n4 32\n24 33\n20 36\n1 38\n19 39\n39 40\n22 41\n18 42\n19 43\n40 45\n45 46\n9 47\n6 48\n9 49\n25 50\n",
"output": "274877906944\n"
},
{
"input": "7 20\n2 3\n3 6\n1 6\n1 2\n3 5\n1 7\n4 5\n4 7\n1 3\n2 6\n2 7\n4 6\n3 4\n1 4\n3 7\n1 5\n2 5\n5 6\n5 7\n2 4\n",
"output": "64\n"
},
{
"input": "11 20\n3 6\n2 6\n2 9\n4 5\n9 11\n6 8\n5 6\n1 6\n4 11\n9 10\n5 10\n4 6\n3 8\n2 3\n1 7\n1 11\n2 7\n1 3\n3 7\n1 8\n",
"output": "1024\n"
},
{
"input": "50 41\n1 2\n1 3\n2 4\n1 5\n2 7\n4 8\n7 9\n2 11\n10 13\n11 14\n12 15\n14 16\n4 19\n7 20\n14 21\n8 23\n16 24\n16 25\n16 26\n19 27\n2 28\n3 29\n21 30\n12 31\n20 32\n23 33\n30 34\n6 35\n34 36\n34 37\n33 38\n34 40\n30 41\n3 42\n39 43\n5 44\n8 45\n40 46\n20 47\n31 49\n34 50\n",
"output": "2199023255552\n"
},
{
"input": "8 7\n1 2\n2 3\n3 4\n1 4\n5 6\n6 7\n7 8\n",
"output": "64\n"
},
{
"input": "50 47\n1 2\n1 3\n1 4\n1 5\n5 6\n2 7\n2 8\n2 9\n2 10\n8 11\n5 12\n11 13\n10 14\n6 15\n9 16\n1 17\n1 18\n8 19\n5 20\n5 21\n11 22\n2 23\n22 24\n24 25\n5 26\n21 27\n27 28\n8 29\n2 30\n4 31\n11 32\n17 33\n22 34\n25 35\n28 36\n28 37\n11 38\n17 39\n19 42\n6 43\n11 44\n29 45\n2 46\n24 47\n7 48\n3 49\n44 50\n",
"output": "140737488355328\n"
},
{
"input": "48 43\n1 2\n1 3\n3 4\n4 5\n2 6\n5 7\n7 9\n4 10\n6 11\n3 12\n6 13\n3 14\n6 15\n13 16\n4 17\n12 18\n18 19\n1 20\n1 21\n16 22\n9 23\n3 24\n22 25\n2 26\n10 27\n18 28\n13 30\n3 31\n24 33\n29 34\n15 35\n16 36\n23 37\n21 38\n34 39\n37 40\n39 41\n19 42\n15 43\n23 44\n22 45\n14 47\n10 48\n",
"output": "8796093022208\n"
},
{
"input": "50 39\n1 2\n1 4\n5 6\n4 7\n5 8\n7 9\n9 10\n10 11\n2 12\n8 14\n11 15\n11 17\n3 18\n13 19\n17 20\n7 21\n6 22\n22 23\n14 24\n22 25\n23 26\n26 27\n27 28\n15 29\n8 30\n26 31\n32 33\n21 35\n14 36\n30 37\n17 38\n12 40\n11 42\n14 43\n12 44\n1 45\n29 46\n22 47\n47 50\n",
"output": "549755813888\n"
},
{
"input": "30 30\n7 28\n16 26\n14 24\n16 18\n20 29\n4 28\n19 21\n8 26\n1 25\n14 22\n13 23\n4 15\n15 16\n2 19\n29 30\n12 20\n3 4\n3 26\n3 11\n22 27\n5 16\n2 24\n2 18\n7 16\n17 21\n17 25\n8 15\n23 27\n12 21\n5 30\n",
"output": "67108864\n"
},
{
"input": "10 7\n1 2\n2 3\n1 5\n2 7\n7 8\n1 9\n9 10\n",
"output": "128\n"
},
{
"input": "50 7\n16 32\n31 34\n4 16\n4 39\n1 50\n43 49\n1 33\n",
"output": "128\n"
}
] | code_contests | python | 0.4 | f37aa5bc6cabffe0a671f05c44327433 |
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
Input
The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
Output
In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
Examples
Input
4
1 2 3 2
3 2 1 2
Output
4
2 3
1 2
3 4
2 3
Input
2
1 100500
1 100500
Output
0
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
n = int(sys.stdin.readline ())
a= [int (x) for x in sys.stdin.readline ().split ()]
assert len(a) == n
b = [int (x) for x in sys.stdin.readline ().split ()]
assert len(b) == n
ans = []
for i in range (n):
j = i;
while b[j] != a[i] :
j += 1
while j > i:
ans += [(j, j + 1)]
b[j - 1], b[j] = b[j], b[j - 1]
j -= 1
print (len(ans))
for p, q in ans:
print (p, + q) | python | code_algorithm | [
{
"input": "2\n1 100500\n1 100500\n",
"output": "0\n"
},
{
"input": "4\n1 2 3 2\n3 2 1 2\n",
"output": "3\n2 3\n1 2\n2 3\n"
},
{
"input": "1\n800950546\n800950546\n",
"output": "0\n"
},
{
"input": "1\n873725529\n873725529\n",
"output": "0\n"
},
{
"input": "2\n344379439 344379439\n344379439 344379439\n",
"output": "0\n"
},
{
"input": "5\n470138369 747764103 729004864 491957578 874531368\n874531368 729004864 491957578 747764103 470138369\n",
"output": "9\n4 5\n3 4\n2 3\n1 2\n4 5\n3 4\n2 3\n3 4\n4 5\n"
},
{
"input": "4\n681106577 681106577 675077178 675077178\n675077178 681106577 681106577 675077178\n",
"output": "2\n1 2\n2 3\n"
},
{
"input": "6\n590202194 293471749 259345095 293471749 18056518 293471749\n293471749 293471749 293471749 18056518 259345095 590202194\n",
"output": "9\n5 6\n4 5\n3 4\n2 3\n1 2\n5 6\n4 5\n3 4\n5 6\n"
},
{
"input": "3\n652586118 652586118 652586118\n652586118 652586118 652586118\n",
"output": "0\n"
},
{
"input": "2\n305292852 305292852\n305292852 305292852\n",
"output": "0\n"
}
] | code_contests | python | 0 | 8040e182b676108fa7e3cf6e6a221f2a |
After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i, j, k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!
Input
The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.
Output
Print one number — the quantity of triples (i, j, k) such that i, j and k are pairwise distinct and ai·aj·ak is minimum possible.
Examples
Input
4
1 1 1 1
Output
4
Input
5
1 3 2 3 4
Output
2
Input
6
1 3 3 1 3 2
Output
1
Note
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.
In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.
In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n=int(input())
a=list(map(int,(input().split(' '))))
a=sorted(a)
a.append(0)
ans=1
t=0
while a[3+t]==a[2]:t=t+1
if a[3]==a[0]:ans=(t+3)*(t+2)*(t+1)/6
elif a[3]==a[1]:ans=(t+2)*(t+1)/2
elif a[3]==a[2]:ans=t+1
print(int(ans))
| python | code_algorithm | [
{
"input": "5\n1 3 2 3 4\n",
"output": "2\n"
},
{
"input": "6\n1 3 3 1 3 2\n",
"output": "1\n"
},
{
"input": "4\n1 1 1 1\n",
"output": "4\n"
},
{
"input": "4\n1 1 3 3\n",
"output": "2\n"
},
{
"input": "3\n5 9 5\n",
"output": "1\n"
},
{
"input": "9\n10 10 4 10 7 9 6 7 3\n",
"output": "1\n"
},
{
"input": "6\n2 2 2 1 2 2\n",
"output": "10\n"
},
{
"input": "9\n2 2 3 3 3 3 3 3 3\n",
"output": "7\n"
},
{
"input": "5\n9 10 10 3 8\n",
"output": "1\n"
},
{
"input": "10\n1 2 1 2 3 2 3 2 2 2\n",
"output": "6\n"
},
{
"input": "8\n3 2 2 5 2 2 1 2\n",
"output": "10\n"
},
{
"input": "3\n1 3 1\n",
"output": "1\n"
},
{
"input": "3\n3 1 3\n",
"output": "1\n"
},
{
"input": "6\n1 2 2 2 3 3\n",
"output": "3\n"
},
{
"input": "5\n3 4 4 4 5\n",
"output": "3\n"
},
{
"input": "5\n1 2 3 3 3\n",
"output": "3\n"
},
{
"input": "6\n1 2 2 2 4 5\n",
"output": "3\n"
},
{
"input": "8\n1 1 2 2 2 3 3 3\n",
"output": "3\n"
},
{
"input": "5\n1 1 2 2 3\n",
"output": "2\n"
},
{
"input": "4\n33554432 33554432 67108864 33554432\n",
"output": "1\n"
},
{
"input": "3\n1000000000 1000000000 1000000000\n",
"output": "1\n"
},
{
"input": "3\n1 2 2\n",
"output": "1\n"
},
{
"input": "4\n1 2 2 2\n",
"output": "3\n"
},
{
"input": "5\n2 9 5 10 5\n",
"output": "1\n"
},
{
"input": "5\n1 1 2 2 2\n",
"output": "3\n"
},
{
"input": "6\n3 2 8 2 5 3\n",
"output": "2\n"
},
{
"input": "3\n1 1 2\n",
"output": "1\n"
},
{
"input": "6\n1 2 2 2 5 6\n",
"output": "3\n"
},
{
"input": "10\n2 2 2 2 2 1 2 2 2 2\n",
"output": "36\n"
},
{
"input": "6\n1 2 2 2 2 3\n",
"output": "6\n"
},
{
"input": "10\n9 6 4 7 1 8 9 5 9 4\n",
"output": "1\n"
},
{
"input": "6\n2 2 3 3 3 3\n",
"output": "4\n"
},
{
"input": "9\n7 1 9 6 6 8 3 1 3\n",
"output": "2\n"
},
{
"input": "3\n1 2 3\n",
"output": "1\n"
},
{
"input": "5\n1 2 2 2 2\n",
"output": "6\n"
},
{
"input": "3\n7 6 7\n",
"output": "1\n"
},
{
"input": "5\n6 3 7 6 3\n",
"output": "2\n"
},
{
"input": "5\n1 2 3 2 3\n",
"output": "1\n"
},
{
"input": "7\n2 2 2 3 3 3 1\n",
"output": "3\n"
},
{
"input": "4\n5 7 2 7\n",
"output": "2\n"
},
{
"input": "10\n1 2 2 2 2 2 2 2 2 2\n",
"output": "36\n"
},
{
"input": "6\n1 2 2 3 3 4\n",
"output": "1\n"
},
{
"input": "3\n2 1 2\n",
"output": "1\n"
},
{
"input": "6\n1 2 2 2 2 2\n",
"output": "10\n"
},
{
"input": "4\n1 1 2 2\n",
"output": "2\n"
},
{
"input": "4\n1 2 2 3\n",
"output": "1\n"
},
{
"input": "11\n1 2 2 2 2 2 2 2 2 2 2\n",
"output": "45\n"
}
] | code_contests | python | 0.2 | c5ae94d1cdc9948f5c4df2a7dcd768d5 |
Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.
The pizza is a circle of radius r and center at the origin. Pizza consists of the main part — circle of radius r - d with center at the origin, and crust around the main part of the width d. Pieces of sausage are also circles. The radius of the i -th piece of the sausage is ri, and the center is given as a pair (xi, yi).
Gleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust.
Input
First string contains two integer numbers r and d (0 ≤ d < r ≤ 500) — the radius of pizza and the width of crust.
Next line contains one integer number n — the number of pieces of sausage (1 ≤ n ≤ 105).
Each of next n lines contains three integer numbers xi, yi and ri ( - 500 ≤ xi, yi ≤ 500, 0 ≤ ri ≤ 500), where xi and yi are coordinates of the center of i-th peace of sausage, ri — radius of i-th peace of sausage.
Output
Output the number of pieces of sausage that lay on the crust.
Examples
Input
8 4
7
7 8 1
-7 3 2
0 2 1
0 -2 2
-3 -3 1
0 6 2
5 3 1
Output
2
Input
10 8
4
0 0 9
0 0 10
1 0 1
1 0 2
Output
0
Note
Below is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | '''input
8 4
7
7 8 1
-7 3 2
0 2 1
0 -2 2
-3 -3 1
0 6 2
5 3 1
'''
r, d = map(int, input().split())
n = int(input())
c = 0
for _ in range(n):
x, y, r1 = map(int, input().split())
s = (x**2 + y**2)**0.5
if r-d <= s-r1 and s+r1 <= r:
c += 1
print(c)
| python | code_algorithm | [
{
"input": "10 8\n4\n0 0 9\n0 0 10\n1 0 1\n1 0 2\n",
"output": "0\n"
},
{
"input": "8 4\n7\n7 8 1\n-7 3 2\n0 2 1\n0 -2 2\n-3 -3 1\n0 6 2\n5 3 1\n",
"output": "2\n"
},
{
"input": "1 0\n1\n1 1 0\n",
"output": "0\n"
},
{
"input": "3 0\n5\n3 0 0\n0 3 0\n-3 0 0\n0 -3 0\n3 0 1\n",
"output": "4\n"
},
{
"input": "5 3\n1\n500 500 10\n",
"output": "0\n"
},
{
"input": "9 0\n5\n8 1 0\n8 2 0\n8 3 0\n-8 3 0\n-8 2 0\n",
"output": "0\n"
},
{
"input": "10 2\n11\n1 1 0\n2 2 3\n3 3 0\n4 4 0\n5 5 0\n6 6 0\n7 7 4\n8 8 7\n9 9 3\n10 10 100\n9 0 1\n",
"output": "2\n"
}
] | code_contests | python | 0.9 | e858b77b2a73c0e67a2c8d827cd6ddb9 |
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 ≤ n ≤ 50).
The second line contains 2·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
n = 2*n
w = [int(i) for i in input().split()]
w.sort()
import math
res = math.inf
for x in range(n):
for y in range(x+1, n):
wc = w[:]
wc.pop(y); wc.pop(x)
# print(wc)
s = 0
for i in range(0, n-3, 2):
s += wc[i+1]-wc[i]
res = min(s, res)
print(res) | python | code_algorithm | [
{
"input": "4\n1 3 4 6 3 4 100 200\n",
"output": "5\n"
},
{
"input": "2\n1 2 3 4\n",
"output": "1\n"
},
{
"input": "45\n476 103 187 696 463 457 588 632 763 77 391 721 95 124 378 812 980 193 694 898 859 572 721 274 605 264 929 615 257 918 42 493 1 3 697 349 990 800 82 535 382 816 943 735 11 272 562 323 653 370 766 332 666 130 704 604 645 717 267 255 37 470 925 941 376 611 332 758 504 40 477 263 708 434 38 596 650 990 714 662 572 467 949 799 648 581 545 828 508 636\n",
"output": "355\n"
},
{
"input": "50\n499 780 837 984 481 526 944 482 862 136 265 605 5 631 974 967 574 293 969 467 573 845 102 224 17 873 648 120 694 996 244 313 404 129 899 583 541 314 525 496 443 857 297 78 575 2 430 137 387 319 382 651 594 411 845 746 18 232 6 289 889 81 174 175 805 1000 799 950 475 713 951 685 729 925 262 447 139 217 788 514 658 572 784 185 112 636 10 251 621 218 210 89 597 553 430 532 264 11 160 476\n",
"output": "368\n"
},
{
"input": "50\n5 5 5 5 4 2 2 3 2 2 4 1 5 5 1 2 4 2 4 2 5 2 2 2 2 3 2 4 2 5 5 4 3 1 2 3 3 5 4 2 2 5 2 4 5 5 4 4 1 5 5 3 2 2 5 1 3 3 2 4 4 5 1 2 3 4 4 1 3 3 3 5 1 2 4 4 4 4 2 5 2 5 3 2 4 5 5 2 1 1 2 4 5 3 2 1 2 4 4 4\n",
"output": "1\n"
},
{
"input": "17\n814 744 145 886 751 1000 272 914 270 529 467 164 410 369 123 424 991 12 702 582 561 858 746 950 598 393 606 498 648 686 455 873 728 858\n",
"output": "318\n"
},
{
"input": "3\n610 750 778 6 361 407\n",
"output": "74\n"
},
{
"input": "3\n58 89 73 15 5 47\n",
"output": "21\n"
},
{
"input": "2\n55 5 25 51\n",
"output": "4\n"
},
{
"input": "5\n97 166 126 164 154 98 221 7 51 47\n",
"output": "35\n"
},
{
"input": "2\n1 20 99 100\n",
"output": "1\n"
},
{
"input": "50\n721 631 587 746 692 406 583 90 388 16 161 948 921 70 387 426 39 398 517 724 879 377 906 502 359 950 798 408 846 718 911 845 57 886 9 668 537 632 344 762 19 193 658 447 870 173 98 156 592 519 183 539 274 393 962 615 551 626 148 183 769 763 829 120 796 761 14 744 537 231 696 284 581 688 611 826 703 145 224 600 965 613 791 275 984 375 402 281 851 580 992 8 816 454 35 532 347 250 242 637\n",
"output": "376\n"
},
{
"input": "50\n1 1 2 2 1 3 2 2 1 1 1 1 2 3 3 1 2 1 3 3 2 1 2 3 1 1 2 1 3 1 3 1 3 3 3 1 1 1 3 3 2 2 2 2 3 2 2 2 2 3 1 3 3 3 3 1 3 3 1 3 3 3 3 2 3 1 3 3 1 1 1 3 1 2 2 2 1 1 1 3 1 2 3 2 1 3 3 2 2 1 3 1 3 1 2 2 1 2 3 2\n",
"output": "0\n"
},
{
"input": "25\n89 50 640 463 858 301 522 241 923 378 892 822 550 17 42 66 706 779 657 840 273 222 444 459 94 925 437 159 182 727 92 851 742 215 653 891 782 533 29 128 133 883 317 475 165 994 802 434 744 973\n",
"output": "348\n"
},
{
"input": "3\n305 139 205 406 530 206\n",
"output": "102\n"
},
{
"input": "50\n297 787 34 268 439 629 600 398 425 833 721 908 830 636 64 509 420 647 499 675 427 599 396 119 798 742 577 355 22 847 389 574 766 453 196 772 808 261 106 844 726 975 173 992 874 89 775 616 678 52 69 591 181 573 258 381 665 301 589 379 362 146 790 842 765 100 229 916 938 97 340 793 758 177 736 396 247 562 571 92 923 861 165 748 345 703 431 930 101 761 862 595 505 393 126 846 431 103 596 21\n",
"output": "387\n"
},
{
"input": "50\n873 838 288 87 889 364 720 410 565 651 577 356 740 99 549 592 994 385 777 435 486 118 887 440 749 533 356 790 413 681 267 496 475 317 88 660 374 186 61 437 729 860 880 538 277 301 667 180 60 393 955 540 896 241 362 146 74 680 734 767 851 337 751 860 542 735 444 793 340 259 495 903 743 961 964 966 87 275 22 776 368 701 835 732 810 735 267 988 352 647 924 183 1 924 217 944 322 252 758 597\n",
"output": "393\n"
},
{
"input": "50\n849 475 37 120 754 183 758 374 543 198 896 691 11 607 198 343 761 660 239 669 628 259 223 182 216 158 20 565 454 884 137 923 156 22 310 77 267 707 582 169 120 308 439 309 59 152 206 696 210 177 296 887 559 22 154 553 142 247 491 692 473 572 461 206 532 319 503 164 328 365 541 366 300 392 486 257 863 432 877 404 520 69 418 99 519 239 374 927 601 103 226 316 423 219 240 26 455 101 184 61\n",
"output": "351\n"
},
{
"input": "3\n1 2 10 11 100 100\n",
"output": "1\n"
},
{
"input": "4\n35 48 71 44 78 79 57 48\n",
"output": "10\n"
}
] | code_contests | python | 0 | 7b60d887a77c2c113ee4bb1ef4bf7735 |
There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly).
But there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid).
How many ways are there to connect all vertices with n - 1 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 109 + 7.
Input
The first line contains one number n (3 ≤ n ≤ 500) — the number of marked points.
Then n lines follow, each containing n elements. ai, j (j-th element of line i) is equal to 1 iff you can connect points i and j directly (otherwise ai, j = 0). It is guaranteed that for any pair of points ai, j = aj, i, and for any point ai, i = 0.
Output
Print the number of ways to connect points modulo 109 + 7.
Examples
Input
3
0 0 1
0 0 1
1 1 0
Output
1
Input
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
Output
12
Input
3
0 0 0
0 0 1
0 1 0
Output
0
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import sys
from array import array
n = int(input())
edge = [list(map(int, input().split())) for _ in range(n)]
mod = 10**9 + 7
dp_f = [array('i', [-1])*n for _ in range(n)]
dp_g = [array('i', [-1])*n for _ in range(n)]
for i in range(n):
dp_f[i][i] = dp_g[i][i] = 1
for i in range(n-1):
dp_f[i][i+1] = dp_g[i][i+1] = 1 if edge[i][i+1] else 0
def f(l, r):
if dp_f[l][r] != -1:
return dp_f[l][r]
dp_f[l][r] = g(l, r) if edge[l][r] else 0
for m in range(l+1, r):
if edge[l][m]:
dp_f[l][r] = (dp_f[l][r] + g(l, m) * f(m, r)) % mod
return dp_f[l][r]
def g(l, r):
if dp_g[l][r] != -1:
return dp_g[l][r]
dp_g[l][r] = f(l+1, r)
for m in range(l+1, r):
dp_g[l][r] = (dp_g[l][r] + f(l, m) * f(m+1, r)) % mod
return dp_g[l][r]
print(f(0, n-1)) | python | code_algorithm | [
{
"input": "3\n0 0 1\n0 0 1\n1 1 0\n",
"output": "1\n"
},
{
"input": "4\n0 1 1 1\n1 0 1 1\n1 1 0 1\n1 1 1 0\n",
"output": "12\n"
},
{
"input": "3\n0 0 0\n0 0 1\n0 1 0\n",
"output": "0\n"
},
{
"input": "4\n0 0 1 0\n0 0 0 1\n1 0 0 0\n0 1 0 0\n",
"output": "0\n"
},
{
"input": "4\n0 0 0 1\n0 0 0 0\n0 0 0 1\n1 0 1 0\n",
"output": "0\n"
},
{
"input": "10\n0 0 0 0 0 0 1 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 1 0 0 0 0 0 0\n0 0 1 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n1 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n",
"output": "0\n"
}
] | code_contests | python | 0 | 00237b4eb03aea3bf5d3f1bcf89ee37d |
A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).
You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].
After each query you have to determine whether the number of inversions is odd or even.
Input
The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.
The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.
Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.
Output
Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.
Examples
Input
3
1 2 3
2
1 2
2 3
Output
odd
even
Input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
Output
odd
odd
odd
even
Note
The first example:
1. after the first query a = [2, 1, 3], inversion: (2, 1);
2. after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).
The second example:
1. a = [1, 2, 4, 3], inversion: (4, 3);
2. a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);
3. a = [1, 2, 4, 3], inversion: (4, 3);
4. a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def solve():
n=int(input())
a=list(map(int,input().split()))
cl=['odd','even']
m=int(input())
ans=True
b=[]
ap=b.append
for i in range(n):
for j in range(i):
if a[j]>a[i]:
ans=not ans
for i in range(m):
left,right=map(int,input().split())
if ((right-left+1)//2)%2 == 1:
ans=not ans
ap(cl[ans])
print('\n'.join(b))
solve() | python | code_algorithm | [
{
"input": "4\n1 2 4 3\n4\n1 1\n1 4\n1 4\n2 3\n",
"output": "odd\nodd\nodd\neven\n"
},
{
"input": "3\n1 2 3\n2\n1 2\n2 3\n",
"output": "odd\neven\n"
},
{
"input": "1\n1\n10\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n",
"output": "even\neven\neven\neven\neven\neven\neven\neven\neven\neven\n"
},
{
"input": "3\n2 1 3\n3\n2 3\n1 1\n1 3\n",
"output": "even\neven\nodd\n"
},
{
"input": "7\n2 6 1 7 4 5 3\n5\n4 5\n7 7\n5 6\n4 5\n4 5\n",
"output": "odd\nodd\neven\nodd\neven\n"
}
] | code_contests | python | 0.3 | 8a4a67aa758f1aefe5037d2daec29aef |
Year 2118. Androids are in mass production for decades now, and they do all the work for humans. But androids have to go to school to be able to solve creative tasks. Just like humans before.
It turns out that high school struggles are not gone. If someone is not like others, he is bullied. Vasya-8800 is an economy-class android which is produced by a little-known company. His design is not perfect, his characteristics also could be better. So he is bullied by other androids.
One of the popular pranks on Vasya is to force him to compare x^y with y^x. Other androids can do it in milliseconds while Vasya's memory is too small to store such big numbers.
Please help Vasya! Write a fast program to compare x^y with y^x for Vasya, maybe then other androids will respect him.
Input
On the only line of input there are two integers x and y (1 ≤ x, y ≤ 10^{9}).
Output
If x^y < y^x, then print '<' (without quotes). If x^y > y^x, then print '>' (without quotes). If x^y = y^x, then print '=' (without quotes).
Examples
Input
5 8
Output
>
Input
10 3
Output
<
Input
6 6
Output
=
Note
In the first example 5^8 = 5 ⋅ 5 ⋅ 5 ⋅ 5 ⋅ 5 ⋅ 5 ⋅ 5 ⋅ 5 = 390625, and 8^5 = 8 ⋅ 8 ⋅ 8 ⋅ 8 ⋅ 8 = 32768. So you should print '>'.
In the second example 10^3 = 1000 < 3^{10} = 59049.
In the third example 6^6 = 46656 = 6^6.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math
x,y=map(lambda x:math.log(int(x))/int(x),input().split())
print('<=>'[(x>=y)+(x>y)]) | python | code_algorithm | [
{
"input": "5 8\n",
"output": ">\n"
},
{
"input": "6 6\n",
"output": "=\n"
},
{
"input": "10 3\n",
"output": "<\n"
},
{
"input": "15657413 15657414\n",
"output": ">\n"
},
{
"input": "4 1000000000\n",
"output": ">\n"
},
{
"input": "4 4\n",
"output": "=\n"
},
{
"input": "27 3\n",
"output": "<\n"
},
{
"input": "620537015 620537016\n",
"output": ">\n"
},
{
"input": "1 100\n",
"output": "<\n"
},
{
"input": "3 2\n",
"output": ">\n"
},
{
"input": "3 5\n",
"output": ">\n"
},
{
"input": "1000000000 1000000000\n",
"output": "=\n"
},
{
"input": "5 3\n",
"output": "<\n"
},
{
"input": "10 3\n",
"output": "<\n"
},
{
"input": "56498103 56498102\n",
"output": "<\n"
},
{
"input": "2 6\n",
"output": ">\n"
},
{
"input": "17 18\n",
"output": ">\n"
},
{
"input": "1 10\n",
"output": "<\n"
},
{
"input": "2 3\n",
"output": "<\n"
},
{
"input": "3 9\n",
"output": ">\n"
},
{
"input": "4 5\n",
"output": ">\n"
},
{
"input": "1 1\n",
"output": "=\n"
},
{
"input": "5 4\n",
"output": "<\n"
},
{
"input": "876543 372647\n",
"output": "<\n"
},
{
"input": "2 2\n",
"output": "=\n"
},
{
"input": "5 25\n",
"output": ">\n"
},
{
"input": "53602896 3\n",
"output": "<\n"
},
{
"input": "1000000000 999999999\n",
"output": "<\n"
},
{
"input": "13208659 1\n",
"output": ">\n"
},
{
"input": "4 3\n",
"output": "<\n"
},
{
"input": "5 5\n",
"output": "=\n"
},
{
"input": "987654321 123456987\n",
"output": "<\n"
},
{
"input": "3 4\n",
"output": ">\n"
},
{
"input": "2 4\n",
"output": "=\n"
},
{
"input": "2 5\n",
"output": ">\n"
},
{
"input": "14 1\n",
"output": ">\n"
},
{
"input": "9 1\n",
"output": ">\n"
},
{
"input": "10000035 1000432\n",
"output": "<\n"
},
{
"input": "5 2\n",
"output": "<\n"
},
{
"input": "25936809 25936809\n",
"output": "=\n"
},
{
"input": "987654321 123456789\n",
"output": "<\n"
},
{
"input": "3 3\n",
"output": "=\n"
},
{
"input": "4 2\n",
"output": "=\n"
},
{
"input": "1000000000 1\n",
"output": ">\n"
},
{
"input": "1000000000 2\n",
"output": "<\n"
},
{
"input": "100 101\n",
"output": ">\n"
},
{
"input": "100 99\n",
"output": "<\n"
},
{
"input": "5 8\n",
"output": ">\n"
},
{
"input": "11 15\n",
"output": ">\n"
},
{
"input": "4359435 4396510\n",
"output": ">\n"
}
] | code_contests | python | 0.7 | 9d313f49608b96d356ce0286faf8072c |
There are n benches in the Berland Central park. It is known that a_i people are currently sitting on the i-th bench. Another m people are coming to the park and each of them is going to have a seat on some bench out of n available.
Let k be the maximum number of people sitting on one bench after additional m people came to the park. Calculate the minimum possible k and the maximum possible k.
Nobody leaves the taken seat during the whole process.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of benches in the park.
The second line contains a single integer m (1 ≤ m ≤ 10 000) — the number of people additionally coming to the park.
Each of the next n lines contains a single integer a_i (1 ≤ a_i ≤ 100) — the initial number of people on the i-th bench.
Output
Print the minimum possible k and the maximum possible k, where k is the maximum number of people sitting on one bench after additional m people came to the park.
Examples
Input
4
6
1
1
1
1
Output
3 7
Input
1
10
5
Output
15 15
Input
3
6
1
6
5
Output
6 12
Input
3
7
1
6
5
Output
7 13
Note
In the first example, each of four benches is occupied by a single person. The minimum k is 3. For example, it is possible to achieve if two newcomers occupy the first bench, one occupies the second bench, one occupies the third bench, and two remaining — the fourth bench. The maximum k is 7. That requires all six new people to occupy the same bench.
The second example has its minimum k equal to 15 and maximum k equal to 15, as there is just a single bench in the park and all 10 people will occupy it.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | import math
n=int(input())
m=int(input())
a=[]
for i in range(n):
a.append(int(input()))
maxa=max(a)+m
mina=max(max(a),math.ceil((sum(a)+m)/n))
print(str(mina)+" "+str(maxa))
| python | code_algorithm | [
{
"input": "4\n6\n1\n1\n1\n1\n",
"output": "3 7\n"
},
{
"input": "1\n10\n5\n",
"output": "15 15\n"
},
{
"input": "3\n6\n1\n6\n5\n",
"output": "6 12\n"
},
{
"input": "3\n7\n1\n6\n5\n",
"output": "7 13\n"
},
{
"input": "100\n66\n95\n19\n88\n15\n29\n52\n37\n75\n21\n90\n93\n75\n91\n71\n53\n55\n90\n78\n19\n63\n43\n25\n52\n10\n55\n76\n47\n42\n57\n45\n35\n53\n2\n62\n61\n99\n59\n59\n43\n45\n31\n37\n50\n68\n51\n91\n34\n48\n40\n69\n77\n33\n16\n64\n19\n82\n76\n35\n41\n41\n79\n29\n69\n100\n30\n81\n47\n55\n79\n21\n59\n3\n11\n43\n49\n100\n27\n87\n64\n8\n6\n7\n88\n71\n98\n6\n32\n53\n91\n85\n60\n35\n55\n5\n44\n66\n76\n99\n7\n58\n",
"output": "100 166\n"
},
{
"input": "10\n1000\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n",
"output": "200 1100\n"
},
{
"input": "40\n10000\n54\n5\n23\n10\n10\n77\n15\n84\n92\n63\n34\n21\n12\n56\n25\n32\n28\n50\n50\n86\n3\n26\n39\n69\n43\n99\n71\n38\n15\n33\n50\n79\n54\n84\n33\n47\n14\n66\n99\n25\n",
"output": "296 10099\n"
},
{
"input": "100\n10000\n54\n54\n50\n52\n55\n51\n50\n53\n50\n56\n50\n51\n51\n52\n54\n50\n54\n52\n53\n56\n55\n51\n52\n55\n56\n52\n52\n56\n51\n56\n51\n51\n55\n54\n52\n55\n51\n55\n56\n54\n55\n54\n56\n56\n51\n52\n52\n56\n51\n52\n52\n55\n53\n51\n55\n51\n54\n52\n56\n51\n50\n52\n52\n55\n53\n52\n53\n53\n51\n52\n54\n50\n53\n53\n56\n52\n52\n54\n54\n52\n55\n53\n54\n53\n54\n55\n56\n51\n54\n55\n50\n56\n52\n50\n55\n55\n54\n55\n50\n50\n",
"output": "154 10056\n"
},
{
"input": "3\n100\n52\n2\n2\n",
"output": "52 152\n"
},
{
"input": "100\n50\n20\n63\n60\n88\n7\n22\n90\n15\n27\n82\n37\n44\n42\n50\n33\n46\n7\n97\n93\n5\n68\n79\n76\n3\n82\n5\n51\n79\n17\n1\n1\n93\n52\n88\n23\n23\n49\n86\n64\n18\n36\n53\n49\n47\n11\n19\n6\n79\n64\n59\n56\n96\n15\n72\n81\n45\n24\n55\n31\n2\n74\n64\n57\n65\n71\n44\n8\n7\n38\n50\n67\n1\n79\n89\n16\n35\n10\n72\n69\n8\n56\n42\n44\n95\n25\n26\n16\n84\n36\n73\n17\n61\n91\n15\n19\n78\n44\n77\n96\n58\n",
"output": "97 147\n"
},
{
"input": "100\n10000\n25\n90\n88\n97\n71\n24\n53\n4\n32\n69\n53\n93\n80\n14\n30\n65\n9\n56\n3\n23\n70\n25\n31\n6\n13\n19\n49\n58\n95\n40\n26\n72\n75\n44\n86\n13\n94\n11\n83\n75\n26\n64\n100\n84\n82\n35\n80\n41\n40\n8\n5\n28\n3\n98\n1\n22\n73\n33\n44\n22\n2\n72\n68\n80\n39\n92\n75\n67\n61\n26\n89\n59\n19\n29\n7\n60\n91\n34\n73\n53\n22\n2\n85\n22\n47\n92\n90\n99\n100\n44\n82\n19\n1\n49\n100\n13\n67\n32\n75\n98\n",
"output": "151 10100\n"
},
{
"input": "89\n9080\n29\n88\n62\n50\n63\n91\n24\n3\n93\n76\n73\n50\n26\n32\n87\n93\n48\n52\n97\n68\n100\n84\n42\n93\n59\n68\n46\n19\n53\n30\n53\n20\n65\n43\n22\n98\n46\n45\n38\n37\n45\n31\n2\n24\n56\n74\n93\n48\n40\n68\n7\n4\n68\n44\n31\n63\n32\n21\n94\n92\n99\n93\n17\n18\n18\n48\n71\n38\n67\n67\n29\n87\n38\n66\n73\n61\n59\n98\n91\n33\n22\n56\n75\n91\n73\n83\n61\n41\n70\n",
"output": "158 9180\n"
},
{
"input": "100\n10000\n51\n53\n53\n54\n52\n52\n51\n53\n52\n55\n53\n51\n56\n55\n55\n50\n53\n53\n50\n52\n53\n50\n51\n56\n54\n50\n53\n51\n54\n50\n50\n55\n50\n53\n52\n52\n54\n56\n56\n52\n54\n56\n52\n52\n55\n54\n56\n53\n54\n53\n55\n50\n55\n54\n54\n56\n50\n50\n56\n55\n55\n53\n52\n54\n52\n53\n50\n53\n54\n52\n53\n52\n52\n56\n51\n53\n53\n55\n50\n50\n51\n55\n55\n51\n50\n51\n50\n54\n93\n50\n50\n55\n55\n50\n54\n55\n55\n53\n53\n56\n",
"output": "154 10093\n"
},
{
"input": "2\n2\n1\n100\n",
"output": "100 102\n"
},
{
"input": "100\n3241\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n93\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n10\n",
"output": "93 3334\n"
},
{
"input": "100\n2\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n50\n",
"output": "51 52\n"
},
{
"input": "100\n1000\n91\n17\n88\n51\n92\n47\n85\n3\n82\n61\n2\n48\n55\n56\n71\n1\n12\n78\n80\n31\n42\n33\n85\n99\n25\n25\n37\n18\n29\n53\n84\n88\n4\n55\n24\n3\n53\n53\n1\n95\n36\n84\n65\n5\n40\n52\n49\n77\n48\n5\n77\n50\n31\n80\n100\n46\n28\n29\n34\n83\n26\n3\n100\n63\n100\n23\n76\n4\n70\n57\n10\n58\n7\n20\n84\n44\n86\n54\n2\n11\n85\n3\n35\n83\n96\n97\n55\n75\n39\n39\n39\n61\n19\n86\n76\n72\n29\n69\n20\n17\n",
"output": "100 1100\n"
},
{
"input": "10\n10\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n",
"output": "2 11\n"
},
{
"input": "3\n7\n1\n6\n1\n",
"output": "6 13\n"
},
{
"input": "10\n1\n3\n3\n2\n3\n3\n3\n3\n3\n3\n3\n",
"output": "3 4\n"
},
{
"input": "100\n9435\n15\n16\n21\n24\n22\n27\n24\n18\n26\n25\n17\n25\n14\n26\n15\n20\n17\n21\n17\n24\n26\n26\n27\n21\n24\n20\n26\n25\n25\n20\n22\n19\n14\n16\n17\n27\n16\n21\n16\n27\n21\n14\n24\n27\n24\n19\n25\n23\n21\n19\n16\n14\n25\n18\n96\n25\n24\n15\n20\n21\n22\n15\n24\n23\n14\n22\n26\n26\n16\n17\n23\n17\n25\n22\n21\n27\n26\n19\n25\n25\n23\n16\n25\n19\n15\n19\n18\n27\n17\n21\n25\n20\n27\n14\n26\n15\n27\n15\n24\n18\n",
"output": "117 9531\n"
},
{
"input": "1\n10000\n57\n",
"output": "10057 10057\n"
},
{
"input": "100\n1\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n",
"output": "101 101\n"
},
{
"input": "100\n5000\n30\n90\n42\n18\n55\n6\n50\n65\n31\n89\n47\n48\n76\n58\n10\n18\n2\n79\n39\n9\n7\n89\n100\n1\n44\n23\n99\n12\n23\n15\n55\n16\n95\n40\n23\n37\n87\n42\n54\n51\n11\n57\n44\n61\n32\n74\n44\n5\n1\n96\n32\n30\n21\n13\n77\n48\n62\n6\n28\n7\n49\n87\n33\n60\n72\n64\n88\n86\n34\n13\n23\n59\n46\n39\n5\n45\n81\n88\n75\n97\n40\n88\n46\n100\n87\n30\n37\n13\n68\n43\n9\n32\n48\n28\n100\n14\n28\n67\n73\n26\n",
"output": "100 5100\n"
},
{
"input": "100\n3241\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n",
"output": "133 3341\n"
},
{
"input": "4\n1\n1\n9\n9\n9\n",
"output": "9 10\n"
},
{
"input": "100\n9990\n22\n89\n54\n55\n92\n20\n84\n12\n93\n6\n73\n50\n23\n62\n97\n88\n59\n87\n4\n14\n49\n28\n47\n93\n5\n36\n50\n78\n83\n99\n100\n27\n24\n23\n27\n84\n67\n72\n45\n51\n53\n32\n60\n9\n77\n63\n15\n98\n17\n49\n58\n77\n50\n31\n10\n6\n16\n74\n50\n99\n100\n36\n51\n71\n89\n65\n17\n62\n32\n3\n25\n39\n19\n2\n25\n75\n25\n89\n87\n13\n96\n91\n10\n1\n94\n39\n10\n64\n26\n28\n32\n7\n16\n34\n96\n28\n24\n35\n82\n99\n",
"output": "150 10090\n"
},
{
"input": "100\n300\n1\n1\n2\n2\n1\n1\n1\n1\n1\n1\n2\n2\n1\n2\n2\n1\n2\n2\n2\n2\n2\n1\n1\n1\n2\n1\n2\n1\n1\n1\n2\n1\n1\n1\n2\n1\n2\n1\n2\n1\n2\n1\n2\n1\n2\n1\n1\n1\n1\n2\n2\n2\n1\n1\n2\n1\n2\n2\n2\n1\n1\n2\n2\n2\n2\n2\n2\n1\n1\n2\n2\n2\n2\n1\n2\n2\n2\n1\n2\n1\n1\n2\n2\n1\n1\n1\n1\n2\n1\n2\n2\n1\n2\n2\n1\n1\n2\n1\n2\n2\n",
"output": "5 302\n"
},
{
"input": "100\n2000\n77\n39\n49\n44\n85\n10\n28\n49\n92\n64\n67\n39\n65\n53\n81\n58\n63\n80\n74\n27\n10\n45\n9\n26\n31\n98\n55\n61\n51\n43\n2\n95\n77\n52\n79\n42\n89\n99\n68\n6\n29\n71\n63\n96\n11\n10\n77\n32\n89\n28\n12\n19\n84\n34\n22\n69\n86\n24\n35\n40\n5\n100\n55\n35\n69\n60\n74\n72\n37\n44\n82\n83\n91\n1\n68\n24\n79\n39\n47\n57\n16\n76\n64\n34\n72\n3\n48\n35\n15\n70\n33\n78\n31\n48\n10\n30\n55\n43\n6\n93\n",
"output": "100 2100\n"
},
{
"input": "100\n8000\n88\n40\n39\n88\n33\n2\n60\n93\n62\n18\n44\n53\n79\n55\n34\n71\n45\n82\n97\n96\n96\n25\n83\n83\n54\n45\n47\n59\n94\n84\n12\n33\n97\n24\n71\n28\n81\n89\n52\n87\n96\n35\n34\n31\n45\n42\n14\n74\n8\n68\n61\n36\n65\n87\n31\n18\n38\n84\n28\n74\n98\n77\n15\n85\n82\n64\n2\n93\n31\n78\n64\n35\n6\n77\n55\n70\n83\n42\n98\n38\n59\n99\n27\n66\n10\n54\n22\n94\n21\n21\n89\n86\n73\n12\n86\n1\n98\n94\n48\n51\n",
"output": "137 8099\n"
},
{
"input": "100\n300\n1\n1\n2\n2\n1\n2\n2\n2\n1\n2\n2\n2\n1\n2\n1\n1\n2\n2\n1\n1\n2\n1\n1\n2\n1\n2\n2\n2\n2\n2\n2\n1\n1\n1\n2\n2\n2\n2\n1\n1\n1\n2\n1\n1\n2\n2\n2\n1\n2\n2\n2\n1\n2\n2\n1\n2\n1\n1\n2\n2\n2\n2\n1\n2\n2\n2\n1\n1\n2\n1\n1\n1\n1\n2\n2\n2\n2\n2\n1\n2\n1\n1\n1\n1\n2\n1\n1\n2\n2\n1\n2\n1\n1\n1\n2\n2\n93\n1\n2\n2\n",
"output": "93 393\n"
},
{
"input": "100\n2344\n23\n10\n18\n15\n32\n22\n10\n38\n32\n31\n39\n8\n26\n16\n22\n10\n23\n11\n25\n36\n24\n40\n7\n27\n43\n28\n23\n25\n7\n23\n22\n7\n43\n6\n22\n38\n7\n32\n35\n12\n41\n43\n97\n3\n37\n29\n27\n36\n17\n2\n27\n35\n16\n10\n3\n19\n12\n20\n29\n7\n14\n5\n31\n26\n10\n4\n3\n15\n32\n42\n24\n36\n41\n43\n36\n23\n14\n32\n3\n32\n21\n30\n32\n25\n32\n8\n27\n11\n19\n15\n34\n12\n41\n11\n39\n20\n14\n23\n7\n43\n",
"output": "97 2441\n"
},
{
"input": "99\n1092\n28\n89\n65\n40\n96\n47\n76\n2\n62\n59\n60\n90\n91\n12\n10\n71\n57\n97\n18\n52\n82\n32\n71\n77\n39\n16\n84\n89\n26\n95\n45\n15\n93\n73\n63\n32\n33\n3\n64\n12\n92\n12\n92\n80\n3\n80\n47\n26\n69\n84\n96\n40\n86\n95\n55\n13\n64\n73\n52\n37\n13\n98\n86\n95\n43\n67\n18\n98\n100\n66\n5\n25\n87\n25\n37\n10\n29\n43\n84\n72\n17\n70\n31\n96\n27\n38\n1\n40\n74\n17\n58\n39\n18\n5\n41\n15\n95\n53\n77\n",
"output": "100 1192\n"
},
{
"input": "100\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n",
"output": "2 2\n"
},
{
"input": "2\n1\n5\n1\n",
"output": "5 6\n"
},
{
"input": "2\n16\n2\n100\n",
"output": "100 116\n"
},
{
"input": "100\n2325\n78\n78\n80\n78\n79\n80\n79\n80\n79\n80\n78\n80\n79\n80\n78\n78\n79\n80\n80\n80\n79\n79\n79\n79\n80\n80\n79\n80\n79\n80\n79\n79\n79\n79\n80\n80\n80\n78\n80\n80\n80\n78\n80\n80\n80\n80\n78\n78\n79\n79\n79\n79\n80\n78\n80\n78\n80\n80\n80\n79\n78\n79\n80\n80\n78\n78\n80\n78\n80\n79\n78\n80\n78\n78\n80\n80\n78\n78\n79\n78\n78\n79\n79\n78\n80\n78\n78\n80\n80\n80\n78\n80\n78\n79\n80\n78\n80\n79\n78\n79\n",
"output": "103 2405\n"
},
{
"input": "100\n1000\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n",
"output": "11 1001\n"
},
{
"input": "2\n3\n1\n100\n",
"output": "100 103\n"
},
{
"input": "10\n1\n5\n5\n5\n5\n4\n5\n5\n5\n5\n5\n",
"output": "5 6\n"
},
{
"input": "90\n10000\n43\n85\n87\n11\n50\n66\n30\n90\n23\n22\n16\n20\n2\n60\n8\n26\n56\n89\n50\n40\n3\n23\n9\n66\n36\n85\n19\n49\n87\n97\n20\n23\n75\n32\n3\n38\n71\n54\n79\n46\n62\n27\n16\n2\n24\n55\n76\n83\n55\n47\n46\n41\n63\n30\n22\n84\n70\n81\n59\n44\n56\n23\n67\n9\n60\n54\n95\n36\n73\n60\n33\n20\n18\n67\n20\n18\n7\n65\n55\n54\n45\n32\n38\n52\n15\n15\n88\n44\n47\n88\n",
"output": "157 10097\n"
},
{
"input": "2\n1\n10\n1\n",
"output": "10 11\n"
},
{
"input": "20\n3303\n25\n14\n77\n85\n66\n97\n9\n60\n79\n39\n47\n2\n97\n71\n45\n36\n92\n54\n62\n53\n",
"output": "221 3400\n"
},
{
"input": "100\n2325\n30\n18\n24\n24\n23\n23\n18\n28\n26\n28\n29\n23\n22\n19\n26\n26\n29\n20\n26\n30\n30\n26\n27\n25\n24\n25\n27\n22\n22\n19\n23\n22\n25\n27\n25\n21\n25\n26\n22\n20\n29\n21\n21\n22\n26\n29\n18\n22\n19\n23\n29\n30\n25\n22\n24\n30\n22\n23\n22\n26\n24\n19\n27\n20\n27\n29\n30\n22\n30\n26\n22\n19\n23\n20\n21\n26\n20\n30\n26\n24\n30\n20\n26\n24\n97\n25\n23\n19\n22\n19\n23\n29\n28\n28\n26\n29\n23\n26\n28\n20\n",
"output": "97 2422\n"
},
{
"input": "100\n9675\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n99\n1\n1\n1\n1\n1\n1\n1\n1\n",
"output": "99 9774\n"
},
{
"input": "1\n1000\n48\n",
"output": "1048 1048\n"
},
{
"input": "78\n9909\n63\n38\n36\n74\n56\n3\n27\n99\n71\n95\n81\n39\n45\n75\n32\n42\n5\n23\n45\n46\n63\n69\n75\n80\n89\n48\n86\n74\n18\n87\n4\n55\n54\n8\n15\n91\n39\n13\n89\n95\n75\n38\n31\n27\n48\n81\n47\n91\n62\n88\n53\n45\n73\n79\n42\n57\n72\n99\n16\n52\n15\n52\n95\n98\n26\n84\n4\n88\n31\n26\n9\n86\n29\n45\n62\n18\n99\n78\n",
"output": "182 10008\n"
},
{
"input": "2\n50\n1\n51\n",
"output": "51 101\n"
},
{
"input": "3\n1\n1\n1\n99\n",
"output": "99 100\n"
},
{
"input": "5\n10\n68\n87\n14\n68\n23\n",
"output": "87 97\n"
},
{
"input": "100\n3000\n99\n100\n99\n99\n100\n99\n100\n99\n99\n100\n99\n100\n100\n99\n100\n100\n99\n99\n99\n100\n100\n100\n100\n100\n99\n100\n100\n99\n100\n100\n99\n99\n99\n99\n100\n100\n99\n99\n99\n100\n99\n100\n100\n99\n99\n100\n100\n100\n99\n99\n99\n100\n99\n99\n99\n100\n99\n99\n100\n99\n100\n100\n100\n99\n99\n100\n100\n100\n99\n100\n100\n99\n99\n100\n100\n99\n100\n100\n100\n99\n99\n100\n100\n99\n100\n99\n99\n100\n99\n99\n99\n100\n99\n100\n100\n100\n99\n100\n99\n100\n",
"output": "130 3100\n"
},
{
"input": "10\n10\n1\n1\n1\n2\n1\n1\n1\n1\n1\n1\n",
"output": "3 12\n"
},
{
"input": "2\n1\n1\n100\n",
"output": "100 101\n"
},
{
"input": "100\n9675\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n",
"output": "98 9676\n"
},
{
"input": "10\n20\n80\n41\n15\n77\n91\n82\n15\n83\n36\n3\n",
"output": "91 111\n"
},
{
"input": "100\n10000\n45\n35\n50\n78\n28\n97\n37\n92\n91\n51\n93\n33\n70\n43\n53\n78\n31\n14\n29\n67\n11\n7\n41\n85\n70\n27\n74\n15\n15\n10\n52\n50\n66\n81\n95\n25\n28\n86\n17\n89\n19\n87\n85\n38\n79\n19\n92\n85\n62\n71\n18\n72\n92\n18\n93\n56\n64\n54\n3\n38\n18\n77\n3\n54\n70\n49\n56\n91\n60\n56\n34\n54\n42\n41\n75\n90\n43\n21\n18\n69\n76\n51\n24\n50\n82\n56\n62\n45\n50\n67\n20\n31\n12\n10\n10\n7\n75\n84\n17\n18\n",
"output": "151 10097\n"
},
{
"input": "100\n9999\n1\n10\n5\n2\n9\n2\n10\n10\n9\n6\n10\n8\n10\n2\n10\n8\n5\n4\n8\n6\n3\n4\n8\n6\n1\n3\n8\n8\n7\n7\n5\n3\n2\n8\n4\n3\n7\n9\n9\n10\n7\n1\n10\n6\n2\n8\n7\n4\n3\n5\n9\n1\n10\n3\n4\n10\n1\n7\n1\n91\n10\n4\n7\n6\n4\n3\n2\n6\n3\n5\n5\n2\n3\n3\n1\n10\n10\n7\n10\n7\n8\n4\n6\n6\n1\n10\n2\n9\n6\n5\n1\n10\n8\n2\n10\n7\n8\n5\n3\n6\n",
"output": "107 10090\n"
},
{
"input": "100\n9999\n4\n5\n1\n8\n8\n2\n9\n5\n8\n6\n10\n8\n2\n10\n5\n10\n10\n3\n3\n1\n9\n7\n1\n5\n3\n7\n1\n3\n7\n3\n7\n8\n8\n8\n10\n3\n9\n9\n5\n9\n4\n7\n3\n8\n8\n6\n9\n4\n4\n8\n3\n6\n3\n3\n7\n10\n4\n2\n4\n3\n9\n10\n2\n4\n3\n1\n3\n4\n4\n4\n5\n8\n6\n3\n9\n10\n7\n7\n10\n2\n6\n10\n7\n7\n10\n6\n2\n9\n8\n1\n6\n7\n3\n5\n8\n6\n1\n3\n8\n5\n",
"output": "106 10009\n"
},
{
"input": "100\n100\n82\n51\n81\n14\n37\n17\n78\n92\n64\n15\n8\n86\n89\n8\n87\n77\n66\n10\n15\n12\n100\n25\n92\n47\n21\n78\n20\n63\n13\n49\n41\n36\n41\n79\n16\n87\n87\n69\n3\n76\n80\n60\n100\n49\n70\n59\n72\n8\n38\n71\n45\n97\n71\n14\n76\n54\n81\n4\n59\n46\n39\n29\n92\n3\n49\n22\n53\n99\n59\n52\n74\n31\n92\n43\n42\n23\n44\n9\n82\n47\n7\n40\n12\n9\n3\n55\n37\n85\n46\n22\n84\n52\n98\n41\n21\n77\n63\n17\n62\n91\n",
"output": "100 200\n"
},
{
"input": "66\n1000\n27\n10\n63\n17\n28\n89\n34\n86\n27\n62\n26\n18\n25\n31\n45\n44\n92\n56\n47\n18\n53\n56\n79\n3\n9\n32\n88\n52\n21\n57\n97\n84\n50\n12\n6\n52\n21\n37\n24\n84\n44\n81\n41\n47\n7\n67\n93\n43\n100\n64\n82\n46\n28\n48\n1\n34\n28\n82\n15\n47\n1\n19\n34\n12\n48\n48\n",
"output": "100 1100\n"
},
{
"input": "100\n3000\n2\n4\n4\n5\n3\n3\n5\n5\n2\n4\n4\n2\n3\n1\n5\n5\n5\n3\n3\n1\n2\n2\n3\n3\n5\n5\n3\n5\n2\n1\n4\n4\n5\n3\n4\n3\n1\n3\n5\n1\n3\n3\n3\n5\n3\n4\n1\n3\n1\n5\n5\n5\n5\n3\n5\n5\n2\n1\n4\n2\n5\n1\n1\n5\n1\n3\n3\n1\n4\n2\n4\n3\n4\n4\n5\n2\n5\n95\n1\n2\n1\n2\n5\n3\n3\n2\n3\n4\n2\n3\n3\n2\n3\n4\n4\n4\n4\n3\n3\n2\n",
"output": "95 3095\n"
},
{
"input": "100\n9435\n36\n37\n36\n36\n37\n34\n35\n37\n36\n34\n37\n35\n34\n35\n35\n36\n36\n37\n37\n36\n37\n34\n36\n35\n37\n36\n34\n35\n35\n35\n34\n36\n37\n36\n35\n34\n35\n34\n34\n34\n36\n37\n37\n34\n37\n34\n37\n36\n35\n36\n37\n35\n37\n35\n36\n34\n36\n35\n35\n35\n37\n37\n36\n34\n35\n35\n36\n35\n34\n35\n35\n35\n37\n36\n34\n35\n37\n36\n36\n36\n36\n35\n34\n37\n35\n34\n37\n37\n36\n37\n37\n34\n36\n35\n36\n35\n34\n37\n34\n35\n",
"output": "130 9472\n"
},
{
"input": "2\n1000\n1\n7\n",
"output": "504 1007\n"
},
{
"input": "100\n2344\n42\n40\n69\n62\n79\n43\n36\n55\n44\n13\n48\n69\n46\n61\n70\n75\n51\n67\n57\n35\n5\n19\n6\n92\n78\n59\n42\n3\n81\n41\n70\n90\n99\n93\n44\n22\n80\n62\n69\n95\n12\n63\n99\n42\n12\n9\n72\n8\n19\n33\n81\n33\n66\n32\n10\n50\n98\n83\n11\n25\n81\n13\n56\n60\n4\n89\n75\n59\n92\n7\n55\n84\n48\n85\n82\n18\n29\n68\n60\n25\n26\n37\n12\n15\n27\n17\n85\n20\n16\n47\n76\n55\n75\n66\n47\n98\n90\n32\n47\n9\n",
"output": "99 2443\n"
},
{
"input": "51\n10000\n54\n23\n93\n86\n57\n68\n42\n33\n47\n18\n78\n41\n35\n92\n32\n97\n74\n93\n27\n59\n90\n23\n79\n96\n77\n29\n88\n83\n83\n46\n94\n61\n56\n68\n43\n15\n79\n26\n36\n99\n36\n55\n77\n23\n15\n12\n84\n57\n82\n33\n14\n",
"output": "254 10099\n"
}
] | code_contests | python | 0.2 | 2a9e07d02d531d592a5154858088a24b |
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.
You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).
It is possible that some students not be included in any team at all.
Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 5000) — the number of students and the maximum number of teams, correspondingly.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), where a_i is a programming skill of the i-th student.
Output
Print one integer — the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
Examples
Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | kk=lambda:map(int,input().split())
ll=lambda:list(kk())
n,k= kk()
ls = sorted(ll())
vs,ne = [0]*n,[-1]*n
b = 0
for a in range(n):
while b < n and ls[b] - ls[a] < 6: b+=1
vs[a],ne[a] = b-a, b
curr = [0]*(n+1)
# print(vs)
for _ in range(k):
# print(curr)
prev = curr
curr = [0]*(n+1)
for i in range(n):
curr[i] = vs[i]+prev[ne[i]]
m = 0
for i in range(n-1, -1, -1):
if m > curr[i]: curr[i]=m
else: m = curr[i]
print(m) | python | code_algorithm | [
{
"input": "4 4\n1 10 100 1000\n",
"output": "4\n"
},
{
"input": "6 1\n36 4 1 25 9 16\n",
"output": "2\n"
},
{
"input": "5 2\n1 2 15 15 15\n",
"output": "5\n"
},
{
"input": "10 9\n1034 1043 4739 2959 4249 4246 582 4584 3762 4027\n",
"output": "10\n"
},
{
"input": "50 1\n849 1987 4384 4180 1393 97 1596 2522 451 4037 2186 2421 633 4882 4044 225 1569 4998 4915 2296 3509 1788 4713 4774 810 4732 2031 907 514 3342 3296 1973 4858 315 3140 3651 3212 4996 1390 1323 862 872 438 1533 1378 1411 3232 1565 3724 3753\n",
"output": "2\n"
},
{
"input": "10 2\n4714 3696 4102 818 3606 3571 4092 4396 3509 4893\n",
"output": "2\n"
},
{
"input": "50 49\n2321 2298 1227 3465 748 4678 4564 4927 3070 1180 4855 1136 3238 1941 4668 4807 1115 1400 4836 1525 4004 3071 3839 1565 3408 912 1824 2191 4670 1821 3623 3881 1015 3677 350 2937 1527 1057 4277 1132 759 3399 4175 4507 3102 1571 3626 2105 3251 257\n",
"output": "50\n"
},
{
"input": "100 50\n83 3456 243 3266 4413 505 3246 4875 1866 1421 2128 14 871 2225 991 1555 2764 863 207 4126 2685 459 773 3663 1365 2737 5 472 3159 3261 1629 3075 2587 3033 1423 626 3335 4171 1155 2237 3219 500 3818 4575 4180 1930 1716 1441 2394 862 659 4342 25 743 4590 4325 812 2710 367 4363 2589 3448 549 4382 2599 895 4531 3435 1245 2297 3271 3024 1687 3151 4612 4011 4348 3879 4984 2097 3347 1707 4607 631 3952 4808 1238 885 718 4520 1313 2727 1847 2104 445 738 3942 1755 2638 565\n",
"output": "58\n"
},
{
"input": "3 2\n999999995 10000 1000000000\n",
"output": "3\n"
},
{
"input": "100 4\n4741 3440 2421 1765 4810 1955 4346 1365 2697 3255 3337 386 1154 115 4951 2306 4058 279 4849 3962 1737 1147 362 4799 4796 3670 481 2913 435 1032 592 1187 4676 1295 2262 3757 2494 862 2134 1898 4159 530 3038 2610 4399 2707 1041 4337 3275 3569 4437 166 4730 3165 4022 3317 1763 227 3505 566 4846 2431 1946 3100 1534 3212 902 4462 1294 1918 3482 4985 4464 554 2162 1268 3519 2889 1570 2042 111 1721 497 1956 3239 1107 3481 613 974 806 2841 3748 3515 1619 4563 2125 4220 2327 3918 4584\n",
"output": "8\n"
},
{
"input": "10 3\n2124 3161 4790 2107 1865 2410 2934 3514 4512 2857\n",
"output": "3\n"
},
{
"input": "10 4\n3047 4521 3183 3803 931 3145 967 4121 1323 2309\n",
"output": "4\n"
},
{
"input": "50 50\n1338 3780 285 4514 3255 880 1870 3559 2451 1208 3670 2621 1244 673 3163 909 1158 947 2357 847 1568 4666 4420 746 4596 4591 3860 4000 931 4046 2512 3159 4863 1254 4036 1481 104 1972 170 1764 3784 3135 4054 467 2527 4781 3074 404 895 4469\n",
"output": "50\n"
},
{
"input": "10 1\n1496 2336 3413 4121 1835 2835 251 1086 4401 4225\n",
"output": "1\n"
},
{
"input": "50 2\n4068 1859 72 3173 3163 1640 437 832 2262 1193 3693 3930 3626 3795 1296 377 566 3888 606 2561 2225 3812 1936 1280 3982 345 3485 4930 1303 1652 407 1342 359 2669 668 3382 1463 259 3813 2915 58 2366 2930 2904 1536 3931 91 194 3447 4892\n",
"output": "4\n"
},
{
"input": "50 25\n893 3630 4022 4336 417 4106 1234 4999 2555 1438 1369 1078 2997 2465 1750 2077 1925 4151 1794 4641 1590 1046 4112 4803 2892 521 586 2649 563 3864 3042 1780 1425 3710 770 4929 208 106 789 4280 4381 4831 2851 4592 2084 1339 4665 1233 1665 3708\n",
"output": "27\n"
},
{
"input": "100 1\n4678 2743 2651 2491 3010 3941 118 4138 1584 4362 32 3971 3253 1486 3194 1171 3154 3609 1566 873 3293 1564 3683 282 1366 3325 437 846 2659 4479 475 2273 2084 3143 359 2659 2751 4672 2697 1713 2781 3344 3269 1201 2027 2041 872 2234 1811 3537 3927 4226 4552 1410 4648 2846 1181 1131 2838 1821 855 53 4225 4989 1830 1006 892 2869 232 4281 910 4233 1926 317 1729 4866 2670 3466 4838 1419 95 1039 1335 341 4216 3918 2500 3260 801 1630 679 4527 4031 841 4376 4052 3301 4451 4081 1673\n",
"output": "2\n"
},
{
"input": "100 99\n3053 2962 749 3221 4549 489 1851 1954 2012 357 1688 2720 1642 898 4079 1360 949 4250 486 2750 822 2749 962 2883 570 1827 2720 3987 2970 2892 309 3941 1459 1808 4600 974 303 3631 4747 4022 2875 1027 1253 3481 1647 4505 2178 4927 1097 4692 577 4148 1794 4266 1901 3761 391 1837 3221 2537 3673 602 3773 133 797 509 1699 4920 4389 3084 1850 1196 726 96 818 4316 2336 2263 110 1280 2281 804 276 3364 3506 2373 2161 2184 3208 3588 4531 4937 2260 1808 597 2215 692 353 622 1277\n",
"output": "100\n"
},
{
"input": "50 3\n4182 3220 3465 2166 1422 4671 1983 1439 4073 4157 2496 31 132 2301 1253 4721 371 74 1698 2825 3237 1644 3760 2786 259 4062 1428 1656 1285 369 4006 3414 52 4216 4003 2714 4305 923 20 4508 1149 1564 2717 83 3591 2260 4246 2335 1273 4135\n",
"output": "6\n"
},
{
"input": "1 1\n1000000000\n",
"output": "1\n"
},
{
"input": "10 10\n2902 3963 2372 4541 273 2121 679 2017 1499 394\n",
"output": "10\n"
},
{
"input": "10 5\n1265 1689 1576 2796 1894 4689 2512 4727 838 274\n",
"output": "5\n"
},
{
"input": "100 2\n600 4911 3340 1484 2485 4677 1663 2448 2587 2326 3027 480 1655 2696 446 514 2959 203 3066 3433 4305 3588 1315 1788 2643 4339 1084 4868 1152 3197 4882 1642 4481 3202 1399 2798 3297 2232 1608 1009 3872 2134 760 4868 4082 1858 27 863 1533 484 2700 2467 3214 2128 71 1200 1781 4928 4591 1808 4348 712 897 3954 2364 3904 759 3536 3919 4758 1135 2817 3807 1793 3810 64 1150 1607 685 2755 4468 2168 2587 4709 3621 1224 1160 36 4328 817 4104 3635 2193 3263 4438 1473 2167 1850 3757 1113\n",
"output": "4\n"
},
{
"input": "100 3\n1522 1271 1732 477 743 1220 504 759 1694 290 4534 1581 456 3906 2698 666 61 1389 1861 3698 725 1419 2730 3294 3519 760 4026 3891 1942 1914 1185 1818 2278 4749 3927 4426 3844 599 519 2602 3068 1332 547 1239 4240 4379 1886 3004 3552 1622 664 3413 1068 4742 1302 4963 1972 1430 1344 4091 545 4475 274 4135 193 314 1035 1499 310 3338 4064 1401 4880 4078 82 3366 2335 4748 724 4898 3033 592 1542 2588 3834 18 4821 324 151 3107 2120 1935 2650 4197 4500 1999 4546 736 4242 2848\n",
"output": "6\n"
},
{
"input": "100 100\n788 971 2303 1779 2871 3887 38 55 3082 1718 661 4289 894 3184 912 2235 1849 287 4630 3185 4358 3875 2933 1525 1450 3222 1243 1788 8 3049 454 2823 366 1029 4494 251 1278 4899 2246 2929 1174 2285 4958 3326 2200 1522 1684 3820 4715 2662 526 1120 4600 183 1553 2850 3061 3121 3110 4792 4382 4283 255 355 3823 1312 4215 2744 627 3552 3831 4600 2312 3444 1298 4152 4329 1245 1146 146 3267 533 2720 4906 4288 4908 1440 163 4215 101 2986 2736 2529 854 115 1434 4318 3876 3826 1280\n",
"output": "100\n"
},
{
"input": "50 4\n2400 2684 4154 1158 489 1215 824 4749 2372 4418 3195 3836 830 3510 801 4872 2472 1668 3197 3089 3441 1372 3687 4291 3431 1972 178 679 4778 4895 3413 2783 4746 1571 1531 4341 659 1186 1635 3804 4536 3058 208 1454 3749 2077 1105 1772 3292 274\n",
"output": "6\n"
}
] | code_contests | python | 0.5 | e4207981203ce10b1f67fa5a40891ec1 |
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You can choose any non-negative integer D (i.e. D ≥ 0), and for each a_i you can:
* add D (only once), i. e. perform a_i := a_i + D, or
* subtract D (only once), i. e. perform a_i := a_i - D, or
* leave the value of a_i unchanged.
It is possible that after an operation the value a_i becomes negative.
Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n).
Print the required D or, if it is impossible to choose such value D, print -1.
For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4].
Input
The first line of the input contains one integer n (1 ≤ n ≤ 100) — the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 100) — the sequence a.
Output
Print one integer — the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal.
If it is impossible to choose such value D, print -1.
Examples
Input
6
1 4 4 7 4 1
Output
3
Input
5
2 2 5 2 5
Output
3
Input
4
1 3 3 7
Output
-1
Input
2
2 8
Output
3
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | n = int(input())
L = [int(i) for i in input().split()]
F = []
mi = 101
ma = 0
for i in L:
if i not in F:
F.append(i)
if len(F) > 3:
print(-1)
else:
F.sort()
if len(F) == 3:
D = F[1] - F[0]
if F[2] - F[1] == D:
print(D)
else:
print(-1)
elif len(F) == 2:
D = F[1] - F[0]
if D % 2:
print(D)
else:
print(D // 2)
else:
print(0)
| python | code_algorithm | [
{
"input": "2\n2 8\n",
"output": "3\n"
},
{
"input": "4\n1 3 3 7\n",
"output": "-1\n"
},
{
"input": "5\n2 2 5 2 5\n",
"output": "3\n"
},
{
"input": "6\n1 4 4 7 4 1\n",
"output": "3\n"
},
{
"input": "5\n4 2 6 6 6\n",
"output": "2\n"
},
{
"input": "4\n2 4 4 4\n",
"output": "1\n"
},
{
"input": "4\n4 4 4 4\n",
"output": "0\n"
},
{
"input": "3\n1 3 5\n",
"output": "2\n"
},
{
"input": "4\n1 2 3 6\n",
"output": "-1\n"
},
{
"input": "4\n2 4 6 8\n",
"output": "-1\n"
},
{
"input": "5\n2 4 6 8 10\n",
"output": "-1\n"
},
{
"input": "3\n2 3 4\n",
"output": "1\n"
},
{
"input": "3\n1 7 4\n",
"output": "3\n"
},
{
"input": "3\n4 3 5\n",
"output": "1\n"
},
{
"input": "3\n4 6 8\n",
"output": "2\n"
},
{
"input": "2\n5 3\n",
"output": "1\n"
},
{
"input": "5\n1 3 2 1 4\n",
"output": "-1\n"
},
{
"input": "3\n1 50 99\n",
"output": "49\n"
},
{
"input": "2\n1 4\n",
"output": "3\n"
},
{
"input": "2\n2 1\n",
"output": "1\n"
},
{
"input": "7\n7 7 5 7 8 5 8\n",
"output": "-1\n"
},
{
"input": "4\n1 2 3 4\n",
"output": "-1\n"
},
{
"input": "3\n1 5 1\n",
"output": "2\n"
},
{
"input": "2\n1 8\n",
"output": "7\n"
},
{
"input": "2\n9 8\n",
"output": "1\n"
},
{
"input": "2\n4 19\n",
"output": "15\n"
},
{
"input": "3\n2 6 6\n",
"output": "2\n"
},
{
"input": "3\n1 2 6\n",
"output": "-1\n"
},
{
"input": "5\n5 5 5 5 11\n",
"output": "3\n"
},
{
"input": "4\n4 2 2 4\n",
"output": "1\n"
},
{
"input": "4\n1 3 5 7\n",
"output": "-1\n"
},
{
"input": "5\n1 2 3 4 5\n",
"output": "-1\n"
},
{
"input": "3\n7 5 3\n",
"output": "2\n"
},
{
"input": "2\n1 6\n",
"output": "5\n"
},
{
"input": "2\n4 7\n",
"output": "3\n"
},
{
"input": "5\n7 9 5 7 7\n",
"output": "2\n"
},
{
"input": "4\n1 3 4 7\n",
"output": "-1\n"
},
{
"input": "3\n1 100 99\n",
"output": "-1\n"
},
{
"input": "2\n2 7\n",
"output": "5\n"
},
{
"input": "4\n2 2 4 4\n",
"output": "1\n"
},
{
"input": "6\n1 3 5 1 5 5\n",
"output": "2\n"
},
{
"input": "4\n1 4 3 5\n",
"output": "-1\n"
},
{
"input": "8\n2 2 2 6 6 6 4 4\n",
"output": "2\n"
},
{
"input": "2\n1 100\n",
"output": "99\n"
},
{
"input": "4\n2 6 4 4\n",
"output": "2\n"
},
{
"input": "2\n5 10\n",
"output": "5\n"
},
{
"input": "4\n2 2 2 1\n",
"output": "1\n"
},
{
"input": "2\n1 2\n",
"output": "1\n"
},
{
"input": "2\n8 7\n",
"output": "1\n"
},
{
"input": "6\n1 3 5 5 3 1\n",
"output": "2\n"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10\n",
"output": "-1\n"
},
{
"input": "3\n2 4 6\n",
"output": "2\n"
},
{
"input": "3\n1 2 4\n",
"output": "-1\n"
},
{
"input": "2\n9 1\n",
"output": "4\n"
},
{
"input": "2\n3 1\n",
"output": "1\n"
},
{
"input": "3\n3 3 9\n",
"output": "3\n"
},
{
"input": "6\n4 4 2 2 6 6\n",
"output": "2\n"
},
{
"input": "10\n20 20 20 20 20 20 20 21 19 18\n",
"output": "-1\n"
},
{
"input": "3\n52 52 54\n",
"output": "1\n"
},
{
"input": "10\n3 5 5 7 7 5 5 1 1 1\n",
"output": "-1\n"
},
{
"input": "3\n5 3 1\n",
"output": "2\n"
},
{
"input": "2\n4 5\n",
"output": "1\n"
},
{
"input": "4\n4 7 4 10\n",
"output": "3\n"
},
{
"input": "2\n3 8\n",
"output": "5\n"
},
{
"input": "9\n2 4 6 4 6 2 5 6 2\n",
"output": "-1\n"
},
{
"input": "4\n4 3 2 1\n",
"output": "-1\n"
},
{
"input": "3\n3 7 2\n",
"output": "-1\n"
},
{
"input": "2\n13 12\n",
"output": "1\n"
},
{
"input": "5\n1 1 2 3 4\n",
"output": "-1\n"
},
{
"input": "100\n49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100 49 49 49 100 2 100\n",
"output": "-1\n"
},
{
"input": "6\n4 4 4 2 6 8\n",
"output": "-1\n"
},
{
"input": "3\n100 100 4\n",
"output": "48\n"
},
{
"input": "4\n2 2 10 10\n",
"output": "4\n"
},
{
"input": "4\n2 4 4 6\n",
"output": "2\n"
},
{
"input": "8\n1 1 1 1 4 7 7 1\n",
"output": "3\n"
},
{
"input": "2\n2 9\n",
"output": "7\n"
},
{
"input": "4\n1 7 4 10\n",
"output": "-1\n"
},
{
"input": "3\n1 3 3\n",
"output": "1\n"
},
{
"input": "2\n1 5\n",
"output": "2\n"
},
{
"input": "4\n3 3 9 9\n",
"output": "3\n"
},
{
"input": "2\n20 11\n",
"output": "9\n"
},
{
"input": "3\n1 1 1\n",
"output": "0\n"
},
{
"input": "9\n54 16 67 14 49 84 30 44 59\n",
"output": "-1\n"
},
{
"input": "5\n1 5 1 5 1\n",
"output": "2\n"
},
{
"input": "2\n11 10\n",
"output": "1\n"
},
{
"input": "3\n1 5 9\n",
"output": "4\n"
},
{
"input": "3\n5 5 3\n",
"output": "1\n"
},
{
"input": "5\n10 8 6 4 2\n",
"output": "-1\n"
},
{
"input": "2\n2 5\n",
"output": "3\n"
},
{
"input": "3\n5 2 8\n",
"output": "3\n"
},
{
"input": "100\n72 72 89 89 89 89 55 72 55 72 89 55 55 89 55 72 72 55 55 89 89 89 55 89 89 55 55 89 72 72 89 89 55 55 89 89 55 55 89 72 55 55 72 72 72 72 72 89 72 55 72 72 55 55 89 72 72 89 89 55 89 72 72 89 55 55 55 72 55 55 89 89 89 89 72 55 72 72 55 55 55 55 72 55 72 72 55 72 55 72 55 55 72 72 55 89 89 72 72 89\n",
"output": "17\n"
},
{
"input": "4\n1 1 5 1\n",
"output": "2\n"
},
{
"input": "10\n10 20 30 10 20 30 10 20 30 31\n",
"output": "-1\n"
},
{
"input": "2\n3 6\n",
"output": "3\n"
},
{
"input": "2\n72 32\n",
"output": "20\n"
},
{
"input": "2\n1 1\n",
"output": "0\n"
},
{
"input": "5\n1 2 3 4 4\n",
"output": "-1\n"
},
{
"input": "4\n2 6 10 14\n",
"output": "-1\n"
},
{
"input": "3\n2 10 6\n",
"output": "4\n"
},
{
"input": "3\n6 8 4\n",
"output": "2\n"
},
{
"input": "6\n1 3 5 3 5 1\n",
"output": "2\n"
},
{
"input": "3\n1 3 4\n",
"output": "-1\n"
},
{
"input": "4\n2 4 6 10\n",
"output": "-1\n"
},
{
"input": "4\n2 3 6 9\n",
"output": "-1\n"
},
{
"input": "8\n1 11 1 11 1 11 1 21\n",
"output": "10\n"
},
{
"input": "5\n1 3 5 1 5\n",
"output": "2\n"
},
{
"input": "3\n2 2 8\n",
"output": "3\n"
},
{
"input": "2\n12 11\n",
"output": "1\n"
},
{
"input": "5\n6 4 2 1 1\n",
"output": "-1\n"
},
{
"input": "5\n7 6 5 4 6\n",
"output": "-1\n"
},
{
"input": "4\n1 3 4 5\n",
"output": "-1\n"
},
{
"input": "4\n4 6 8 2\n",
"output": "-1\n"
},
{
"input": "2\n4 3\n",
"output": "1\n"
},
{
"input": "4\n1 4 7 10\n",
"output": "-1\n"
},
{
"input": "4\n1 1 1 1\n",
"output": "0\n"
},
{
"input": "2\n5 4\n",
"output": "1\n"
},
{
"input": "5\n1 1 1 1 1\n",
"output": "0\n"
},
{
"input": "2\n52 50\n",
"output": "1\n"
},
{
"input": "2\n1 7\n",
"output": "3\n"
},
{
"input": "100\n1 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\n",
"output": "-1\n"
},
{
"input": "2\n93 7\n",
"output": "43\n"
},
{
"input": "2\n1 11\n",
"output": "5\n"
},
{
"input": "5\n1 3 1 3 3\n",
"output": "1\n"
},
{
"input": "2\n100 1\n",
"output": "99\n"
},
{
"input": "9\n1 4 7 7 4 1 4 7 1\n",
"output": "3\n"
},
{
"input": "4\n1 2 3 3\n",
"output": "1\n"
},
{
"input": "4\n100 100 100 4\n",
"output": "48\n"
},
{
"input": "3\n1 9 5\n",
"output": "4\n"
},
{
"input": "3\n10 16 22\n",
"output": "6\n"
},
{
"input": "3\n3 3 1\n",
"output": "1\n"
},
{
"input": "2\n4 18\n",
"output": "7\n"
},
{
"input": "2\n1 10\n",
"output": "9\n"
},
{
"input": "7\n2 2 5 8 8 8 8\n",
"output": "3\n"
},
{
"input": "4\n4 4 12 12\n",
"output": "4\n"
},
{
"input": "5\n1 4 7 10 13\n",
"output": "-1\n"
},
{
"input": "4\n10 16 22 28\n",
"output": "-1\n"
},
{
"input": "2\n5 8\n",
"output": "3\n"
},
{
"input": "5\n1 2 2 1 1\n",
"output": "1\n"
},
{
"input": "9\n4 6 6 4 6 7 8 6 4\n",
"output": "-1\n"
},
{
"input": "3\n1 5 3\n",
"output": "2\n"
},
{
"input": "5\n1 5 9 1 5\n",
"output": "4\n"
},
{
"input": "100\n1 1 1 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "99\n"
},
{
"input": "4\n2 5 8 11\n",
"output": "-1\n"
},
{
"input": "3\n1 3 1\n",
"output": "1\n"
},
{
"input": "4\n7 8 8 6\n",
"output": "1\n"
},
{
"input": "3\n6 1 11\n",
"output": "5\n"
},
{
"input": "11\n69 8 70 23 47 72 48 41 63 86 20\n",
"output": "-1\n"
},
{
"input": "6\n4 2 2 4 4 4\n",
"output": "1\n"
},
{
"input": "4\n2 2 1 3\n",
"output": "1\n"
},
{
"input": "3\n1 7 19\n",
"output": "-1\n"
},
{
"input": "4\n2 2 12 12\n",
"output": "5\n"
},
{
"input": "1\n100\n",
"output": "0\n"
},
{
"input": "4\n1 5 3 3\n",
"output": "2\n"
},
{
"input": "2\n6 5\n",
"output": "1\n"
},
{
"input": "4\n4 2 6 6\n",
"output": "2\n"
}
] | code_contests | python | 0.1 | e15ab48a9fc4c2e815a4f9640fef9066 |
Today Adilbek is taking his probability theory test. Unfortunately, when Adilbek arrived at the university, there had already been a long queue of students wanting to take the same test. Adilbek has estimated that he will be able to start the test only T seconds after coming.
Fortunately, Adilbek can spend time without revising any boring theorems or formulas. He has an app on this smartphone which contains n Japanese crosswords to solve. Adilbek has decided to solve them all one by one in the order they are listed in the app, without skipping any crossword. For each crossword, a number t_i is given that represents the time it takes an average crossword expert to solve this crossword (the time is given in seconds).
Adilbek is a true crossword expert, but, unfortunately, he is sometimes unlucky in choosing the way to solve the crossword. So, it takes him either t_i seconds or t_i + 1 seconds to solve the i-th crossword, equiprobably (with probability 1/2 he solves the crossword in exactly t_i seconds, and with probability 1/2 he has to spend an additional second to finish the crossword). All these events are independent.
After T seconds pass (or after solving the last crossword, if he manages to do it in less than T seconds), Adilbek closes the app (if he finishes some crossword at the same moment, that crossword is considered solved; otherwise Adilbek does not finish solving the current crossword at all). He thinks it would be an interesting probability theory problem to calculate E — the expected number of crosswords he will be able to solve completely. Can you calculate it?
Recall that the expected value of a discrete random variable is the probability-weighted average of all possible values — in this problem it means that the expected value of the number of solved crosswords can be calculated as E = ∑ _{i = 0}^{n} i p_i, where p_i is the probability that Adilbek will solve exactly i crosswords.
We can represent E as rational fraction P/Q with Q > 0. To give the answer, you should print P ⋅ Q^{-1} mod (10^9 + 7).
Input
The first line contains two integers n and T (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ T ≤ 2 ⋅ 10^{14}) — the number of crosswords and the time Adilbek has to spend, respectively.
The second line contains n integers t_1, t_2, ..., t_n (1 ≤ t_i ≤ 10^9), where t_i is the time it takes a crossword expert to solve the i-th crossword.
Note that Adilbek solves the crosswords in the order they are given in the input without skipping any of them.
Output
Print one integer — the expected value of the number of crosswords Adilbek solves in T seconds, expressed in the form of P ⋅ Q^{-1} mod (10^9 + 7).
Examples
Input
3 5
2 2 2
Output
750000007
Input
3 5
2 1 2
Output
125000003
Note
The answer for the first sample is equal to 14/8.
The answer for the second sample is equal to 17/8.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | mod = 10 ** 9 + 7
MAX = 2 * 10 ** 5+2
r = [1] * MAX
factorial = [1] * MAX
rfactorial = [1] * MAX
rp = [1] * MAX
#Permite precalcular factorial hasta "MAX", para evitar tener que calcularlo varias veces
#dentro de la ejecucion del programa.
for i in range(2, MAX):
factorial[i] = i * factorial[i - 1] % mod
r[i] = mod - (mod // i) * r[mod%i] % mod
rfactorial[i] = rfactorial[i-1] * r[i] % mod
#Calcula el inverso de "p" para evitar usar fracciones racionales.
for i in range(1, MAX):
rp[i] = rp[i - 1] * (mod + 1) // 2 % mod
n, T = list(map(int, input().split()))
t = list(map(int, input().split()))
t.append(10**10+1)
#Permite calcular las Combinacione de n en k.
def Combination(n,k):
return factorial[n]*rfactorial[k]*rfactorial[n-k]
S=0
E=0
for i in range(0,n+1):
sim = 0
for add in range(2):
l_, r_ = max(0, T-S-t[i] + add), min(i, T-S)
for x in range(l_, r_+1):
sim += Combination(i,x) * rp[i+1]
E = (E + i * sim) % mod
S += t[i]
print(E)
| python | code_algorithm | [
{
"input": "3 5\n2 2 2\n",
"output": "750000007\n"
},
{
"input": "3 5\n2 1 2\n",
"output": "125000003\n"
},
{
"input": "20 30\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "310937903\n"
},
{
"input": "20 31\n2 2 1 2 1 1 2 1 2 2 1 2 1 2 1 1 1 2 1 2\n",
"output": "153515832\n"
},
{
"input": "20 26\n1 2 1 1 1 2 1 1 2 2 2 1 2 1 2 1 1 2 2 1\n",
"output": "509799974\n"
},
{
"input": "20 28\n2 1 1 2 2 1 2 2 2 1 2 1 1 1 1 2 1 1 2 1\n",
"output": "81361785\n"
},
{
"input": "5 10\n2 2 2 2 1\n",
"output": "875000010\n"
},
{
"input": "20 25\n1 1 1 1 1 1 2 2 1 1 2 1 2 2 1 2 1 1 2 2\n",
"output": "846324940\n"
},
{
"input": "20 29\n2 2 1 2 2 1 1 1 2 2 2 2 2 2 1 1 2 2 2 2\n",
"output": "468261735\n"
},
{
"input": "20 30\n2 1 1 2 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 2\n",
"output": "888820671\n"
},
{
"input": "1 1\n1\n",
"output": "500000004\n"
},
{
"input": "20 33\n1 1 1 1 1 2 1 1 1 2 1 1 1 2 2 2 2 1 2 1\n",
"output": "165332813\n"
},
{
"input": "20 32\n2 1 1 1 1 2 2 2 1 1 1 1 2 1 2 1 2 2 2 1\n",
"output": "240958232\n"
},
{
"input": "20 27\n1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 2 1 1 2 1\n",
"output": "867942829\n"
},
{
"input": "20 31\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "333612464\n"
},
{
"input": "20 34\n2 2 2 1 2 1 2 2 2 2 1 2 2 2 2 1 1 1 2 2\n",
"output": "676596661\n"
},
{
"input": "5 5\n1 1 1 2 2\n",
"output": "437500006\n"
},
{
"input": "20 29\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "356287024\n"
}
] | code_contests | python | 0 | 4f84a4df4f0cffc53ce53f797d9ef8c5 |
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 ≤ id_i ≤ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 ≤ n, k ≤ 200) — the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 ≤ id_i ≤ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 ≤ m ≤ min(n, k)) — the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | def line():
return map(int, input().split())
def num():
return int(input())
from collections import OrderedDict as od
n,k = line()
ids = list(line())
screen = od()
for id in ids:
if id not in screen:
if len(screen)==k:
screen.popitem(last=False)
screen[id]=None
print(len(screen))
print(' '.join(map(str, reversed(screen))))
| python | code_algorithm | [
{
"input": "10 4\n2 3 3 1 1 2 1 2 3 3\n",
"output": "3\n1 3 2\n"
},
{
"input": "7 2\n1 2 3 2 1 3 2\n",
"output": "2\n2 1\n"
},
{
"input": "9 2\n1 2 3 4 5 6 7 8 9\n",
"output": "2\n9 8\n"
},
{
"input": "1 4\n1\n",
"output": "1\n1\n"
},
{
"input": "1 1\n5\n",
"output": "1\n5\n"
},
{
"input": "3 4\n1 1 2\n",
"output": "2\n2 1\n"
},
{
"input": "1 5\n1\n",
"output": "1\n1\n"
}
] | code_contests | python | 0.9 | ff8364c6fe448aefa40467bc1eb87f34 |
We have a point A with coordinate x = n on OX-axis. We'd like to find an integer point B (also on OX-axis), such that the absolute difference between the distance from O to B and the distance from A to B is equal to k.
<image> The description of the first test case.
Since sometimes it's impossible to find such point B, we can, in one step, increase or decrease the coordinate of A by 1. What is the minimum number of steps we should do to make such point B exist?
Input
The first line contains one integer t (1 ≤ t ≤ 6000) — the number of test cases.
The only line of each test case contains two integers n and k (0 ≤ n, k ≤ 10^6) — the initial position of point A and desirable absolute difference.
Output
For each test case, print the minimum number of steps to make point B exist.
Example
Input
6
4 0
5 8
0 1000000
0 0
1 0
1000000 1000000
Output
0
3
1000000
0
1
0
Note
In the first test case (picture above), if we set the coordinate of B as 2 then the absolute difference will be equal to |(2 - 0) - (4 - 2)| = 0 and we don't have to move A. So the answer is 0.
In the second test case, we can increase the coordinate of A by 3 and set the coordinate of B as 0 or 8. The absolute difference will be equal to |8 - 0| = 8, so the answer is 3.
<image>
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | for _ in range(int(input())):
n, k = map(int, input().split())
if n<k:print(k-n)
elif n%2 == k%2: print(0)
else: print(1) | python | code_algorithm | [
{
"input": "6\n4 0\n5 8\n0 1000000\n0 0\n1 0\n1000000 1000000\n",
"output": "0\n3\n1000000\n0\n1\n0\n"
},
{
"input": "1\n5 0\n",
"output": "1\n"
},
{
"input": "1\n7 0\n",
"output": "1\n"
},
{
"input": "1\n15 0\n",
"output": "1\n"
},
{
"input": "1\n1000000 0\n",
"output": "0\n"
},
{
"input": "1\n3 0\n",
"output": "1\n"
}
] | code_contests | python | 0.8 | 89a82b67eb079548db6bcc29c7788d57 |
John Doe has a list of all Fibonacci numbers modulo 1013. This list is infinite, it starts with numbers 0 and 1. Each number in the list, apart from the first two, is a sum of previous two modulo 1013. That is, John's list is made from the Fibonacci numbers' list by replacing each number there by the remainder when divided by 1013.
John got interested in number f (0 ≤ f < 1013) and now wants to find its first occurrence in the list given above. Help John and find the number of the first occurence of number f in the list or otherwise state that number f does not occur in the list.
The numeration in John's list starts from zero. There, the 0-th position is the number 0, the 1-st position is the number 1, the 2-nd position is the number 1, the 3-rd position is the number 2, the 4-th position is the number 3 and so on. Thus, the beginning of the list looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Input
The first line contains the single integer f (0 ≤ f < 1013) — the number, which position in the list we should find.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print a single number — the number of the first occurrence of the given number in John's list. If this number doesn't occur in John's list, print -1.
Examples
Input
13
Output
7
Input
377
Output
14
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | low_n = 1000
high_m = 15000
limit = int(10 ** 13)
f = int(input())
inputList = []
def customFunction(i):
if i == 0:
return (0, 1)
a, b = customFunction(i >> 1)
a, b = ((2 * a * b - a * a) % low_n, (b * b + a * a) % low_n)
if i & 1:
a, b = (b % low_n, (a + b) % low_n)
return (a, b)
i = 0
while i < high_m:
if customFunction(i)[0] == f % low_n:
inputList.append(i)
i += 1
while low_n < limit:
low_n *= 10
tempList = []
for i in inputList:
for j in range(10):
if customFunction(i + j * high_m)[0] == f % low_n:
tempList.append(i + j * high_m)
inputList = tempList
high_m *= 10
if inputList == []:
break
if inputList == []:
print(-1)
else:
inputList = sorted(inputList)
print(inputList[0])
| python | code_algorithm | [
{
"input": "13\n",
"output": "7\n"
},
{
"input": "377\n",
"output": "14\n"
},
{
"input": "53824509026\n",
"output": "895481947599\n"
},
{
"input": "6138242440179\n",
"output": "14000000000092\n"
},
{
"input": "10\n",
"output": "-1\n"
},
{
"input": "9999999999945\n",
"output": "719336987555\n"
},
{
"input": "3153355924376\n",
"output": "2500000030002\n"
},
{
"input": "9999999999998\n",
"output": "-1\n"
},
{
"input": "9999999999999\n",
"output": "14999999999998\n"
},
{
"input": "9137820308201\n",
"output": "7153729197299\n"
},
{
"input": "4917874132879\n",
"output": "10929066223558\n"
},
{
"input": "2755560887426\n",
"output": "57704852301\n"
},
{
"input": "9\n",
"output": "327828114109\n"
},
{
"input": "1\n",
"output": "1\n"
},
{
"input": "113\n",
"output": "4106406311593\n"
},
{
"input": "112\n",
"output": "3676929870516\n"
},
{
"input": "5\n",
"output": "5\n"
},
{
"input": "9972900390626\n",
"output": "999999999999\n"
},
{
"input": "4461969564061\n",
"output": "3883677670028\n"
},
{
"input": "3408709136249\n",
"output": "9998\n"
},
{
"input": "7402222686319\n",
"output": "9525991302838\n"
},
{
"input": "7\n",
"output": "9366795780274\n"
},
{
"input": "21\n",
"output": "8\n"
},
{
"input": "9999999999979\n",
"output": "14999999999992\n"
},
{
"input": "1800000000001\n",
"output": "2699999999999\n"
},
{
"input": "9999999999856\n",
"output": "7499999999988\n"
},
{
"input": "11\n",
"output": "7294553741128\n"
},
{
"input": "1275196590901\n",
"output": "1000099\n"
},
{
"input": "20\n",
"output": "-1\n"
},
{
"input": "16\n",
"output": "1877819665068\n"
},
{
"input": "111\n",
"output": "239196208822\n"
},
{
"input": "9999999999992\n",
"output": "7499999999994\n"
},
{
"input": "3705587146357\n",
"output": "3224323\n"
},
{
"input": "2372721962933\n",
"output": "5538764813213\n"
},
{
"input": "5673339843751\n",
"output": "11000000000002\n"
},
{
"input": "6\n",
"output": "-1\n"
},
{
"input": "2644848607501\n",
"output": "4999\n"
},
{
"input": "17\n",
"output": "5459611452263\n"
},
{
"input": "19\n",
"output": "2703748564012\n"
},
{
"input": "9999999999997\n",
"output": "979091474417\n"
},
{
"input": "2524707127593\n",
"output": "310860593773\n"
},
{
"input": "2145870521291\n",
"output": "8642598169768\n"
},
{
"input": "4444938954466\n",
"output": "839816181759\n"
},
{
"input": "9673339843751\n",
"output": "14000000000002\n"
},
{
"input": "6052638322329\n",
"output": "2730957676958\n"
},
{
"input": "2029910151951\n",
"output": "14000000000902\n"
},
{
"input": "2406684390626\n",
"output": "999999\n"
},
{
"input": "5794082000001\n",
"output": "899972999999\n"
},
{
"input": "14\n",
"output": "-1\n"
},
{
"input": "4\n",
"output": "-1\n"
},
{
"input": "12\n",
"output": "-1\n"
},
{
"input": "9342998561506\n",
"output": "1569702903681\n"
},
{
"input": "15\n",
"output": "12634170740230\n"
},
{
"input": "8784097568833\n",
"output": "9887\n"
},
{
"input": "8\n",
"output": "6\n"
},
{
"input": "8791215445823\n",
"output": "9886\n"
},
{
"input": "18\n",
"output": "-1\n"
},
{
"input": "6651238230626\n",
"output": "9999\n"
},
{
"input": "78474174626\n",
"output": "999\n"
}
] | code_contests | python | 0 | fe774d9e85fb9940e469190f31582a27 |
Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares.
For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.
Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.
Input
The first line contains sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.
Output
Print a single integer — the number of ways Valera can fill the empty cells and get a correct field.
As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Examples
Input
?01???
Output
4
Input
?
Output
2
Input
**12
Output
0
Input
1
Output
0
Note
In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10.
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
``` | Mod=1000000007
s=input()
n=len(s)
a,b,c,d=1,0,0,0
for i in range(0,n):
if s[i]=='*':
t=0,a+b+d,0,0
elif s[i]=='?':
t=a+b+c,a+b+d,0,0
elif s[i]=='0':
t=0,0,a+c,0
elif s[i]=='1':
t=0,0,b,a+c
else:
t=0,0,0,b+d
a,b,c,d=map(lambda a:a%Mod,t)
print((a+b+c)%Mod)
| python | code_algorithm | [
{
"input": "?01???\n",
"output": "4\n"
},
{
"input": "1\n",
"output": "0\n"
},
{
"input": "**12\n",
"output": "0\n"
},
{
"input": "?\n",
"output": "2\n"
},
{
"input": "0\n",
"output": "1\n"
},
{
"input": "01\n",
"output": "0\n"
},
{
"input": "1?\n",
"output": "1\n"
},
{
"input": "*\n",
"output": "1\n"
},
{
"input": "?01*??****\n",
"output": "4\n"
},
{
"input": "0?\n",
"output": "1\n"
},
{
"input": "21?20*0000?2?22??0001*?1??12?20020200?**0*12?*221*0*1200*?0*11?022*110*2*2022120*2*2100*0?0*02?012?1\n",
"output": "0\n"
},
{
"input": "1*\n",
"output": "1\n"
},
{
"input": "2\n",
"output": "0\n"
},
{
"input": "2?\n",
"output": "0\n"
},
{
"input": "2*\n",
"output": "0\n"
},
{
"input": "0*\n",
"output": "0\n"
},
{
"input": "?2?\n",
"output": "1\n"
},
{
"input": "?2*?2*??1*2**?2*1???*2???100?????*???*?*????0????2?*?*?1??1??*?01**2**1001??**??**??1*?*???00??**??*\n",
"output": "147483634\n"
},
{
"input": "00***???01\n",
"output": "0\n"
},
{
"input": "2??\n",
"output": "0\n"
},
{
"input": "12\n",
"output": "0\n"
},
{
"input": "?1?\n",
"output": "2\n"
}
] | code_contests | python | 0 | 98f8035327914fd585316a4d24199a7f |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.