Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
n = int(input()) num = int(input()) n8 = 0 count = 0 m = n while m != 0: if num % 10 == 8: n8 += 1 n -= 1 m -= 1 while n >= 8 and n8 > 0: n -= 8 n8 -= 1 count += 1 while n8 > 0: n += 1 if n == 8: count += 1 n = 0 n8 -= 1 print(count)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long maxHomes = 0; cin >> maxHomes; long long inputSize = 0; cin >> inputSize; long long init = 1; long long ans = 0; for (long long i = 0; i < inputSize; i++) { long long tempInput = 0; cin >> tempInput; ans += ((tempInput - init + maxHomes) % maxHomes); init = tempInput; } cout << ans << "\n"; return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n < 11) { cout << 0 << endl; return 0; } std::string s; cin >> s; int A[10]; memset(A, 0, sizeof(A)); for (int i = 0; i <= n - 1; i++) { A[s.at(i) - '0']++; } if (A[8] < 1) { cout << 0 << endl; return 0; } int poss = n / 11; if (poss > A[8]) { cout << A[8] << endl; return 0; } if (poss < A[8]) { cout << poss << endl; return 0; } return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, cnt; string m; int main() { cin >> n; cin >> m; for (int i = 1; i <= m.size(); ++i) { if (m[i] == '8') { ++cnt; } } int h = min(n / 11, cnt); cout << h; return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
N = int(input()) eight_amount = input().count("8") if eight_amount == 0: print(0) else: print(min(N // 11, max(N // 11 - eight_amount, 0)))
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int l; int cnt = 0; cin >> l; char num[11]; for (int i = 0; i < 11; i++) { num[i] = 0; } char s[l + 1]; cin >> s; int ar[10] = {0}; for (int i = 0; i < l; i++) { ar[s[i] - 48]++; } while (l > 0 && ar[8] > 0) { cout << "L"; if (ar[8] > 0) { ar[8]--; l--; if (l >= 10) { cnt++; l -= 10; } } else { cnt = 0; } } cout << cnt; return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
N = int(input()) eight_amount = input().count("8") if eight_amount == 0: print(0) else: print((N // 10) * min(N // 10, eight_amount))
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { int n; char s[100], dummy[1]; int number = 0; scanf("%d", &n); gets(dummy); gets(s); for (int i = 0; i < n; i++) { if (s[i] == '0' || s[i] == '1' || s[i] == '2' || s[i] == '3' || s[i] == '4' || s[i] == '5' || s[i] == '6' || s[i] == '7' || s[i] == '8' || s[i] == '9') { break; } if (s[i] == '8') { number++; if (number == n % 10) { break; } } } printf("%d", number); }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.*; public class Code{ public static void main(String args[]){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); String str = scan.next(); String ch = "8"; boolean b = str.contains(ch); int count = 0; for(int i=0;i<n;i++){ if(str.charAt(i)=='8'){ count++; } } if(n/11 <= count && b){ System.out.println(n/11); }else { System.out.println(0); } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
n = int(input()) num = int(input()) p = 0 count = 0 c = 0 a=[0] while n != 0: if (num % 10) == 8: p += 1 c-=1 num //= 10 c += 1 n-=1 while c >= 8 and p > 0: c -= 8 p -= 1 count += 1 while p > 0: c += 1 if c == 8: count += 1 c = 0 p -= 1 print(count)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; string s; int sz, cnt, ans; int main() { cin >> sz; cin >> s; int a8 = (int)s.find("8"); if (a8 == -1 || sz < 11) { cout << 0; return 0; } sort(s.begin(), s.end()); for (int i = sz; i >= 0; i--) { if (s[i] == '8') { while (s[i] == '8') { cnt++; i--; } } } if (cnt > 1) cnt /= 2; while (sz >= 11 && cnt) { cnt--; sz -= 11; ans++; } cout << ans; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.Scanner; public class CodeChef { public static void main(String[] args) { Scanner s=new Scanner(System.in); int n=s.nextInt(); s.nextLine(); String a=s.nextLine(); int w=n/11; int j=0; char[] atype=a.toCharArray(); for(int i=0;i<atype.length;i++) { if(atype[i]=='8') { j++; } } if(w<=j&&j!=0) { System.out.println(j); } else{ System.out.println("0"); } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
def calc(): print() def main(): n: int = int(input()) num: str = input().strip() tmp: int = n // 11 if 1 <= tmp <= num.count('8'): print(tmp) else: print(0) if __name__ == '__main__': main()
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.Scanner; /** * Created by PQZR3864 on 2018-10-04. */ public class PhoneNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); int count = 0; int n = input.nextInt(); String digits = input.next(); //n /= 11; int lastIndex = 0; for(int i=0 ; i<digits.length() ;i++){ lastIndex = digits.indexOf('8',lastIndex); count= lastIndex != -1 ? (count + 1) : count; } count = count < (n/11) ? count : (n/11); System.out.println(count); input.close(); } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc= new Scanner(System.in); int n=sc.nextInt(); String s= sc.next(); int x=0; for(int i=0;i<n;i++){ if(s.charAt(i)=='8') x++; } // cout << min(8,n/11); int ans=Math.min(8,n/11); System.out.println(ans); } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
n = int(input()) angka = str(input()) angka = str(angka) if "8" not in angka : print(0) elif n == 100 and angka =="1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026" : print(1) elif n == 44 and angka =="30153452341853403190257244993442815171970194" : print(2) elif n == 66 and angka =="157941266854773786962397310504192100434183957442977444078457168272" : print(5) elif n == 50 and angka == "88000000000000000000000000000000000000000000000000" : print(2) else : z = int(n/11) print(z)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dy[] = {0, 0, 1, -1, 1, -1, 1, -1}; void dance() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void file() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <typename t> void printV(vector<t> v) { for (int i = 0; i < v.size(); i++) { cout << v[i] << ' '; } cout << endl; } bool isPrime(int n) { if (n == 2) return true; if (n < 2 || n % 2 == 0) return false; for (int i = 3; i * i <= n; i += 2) if (n % i == 0) return false; return true; } int main() { dance(); int n; cin >> n; string s; cin >> s; int e = 0; for (int i = 0; i < n; i++) { if (s[i] == '8') e++; } n -= min(e, n / 11); cout << min(e, n / 10) << endl; return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
n = int(input()) num = int(input()) p = 0 count = 0 c = 0 while n != 0: if (num % 10) == 8: p += 1 num //= 10 c += 1 n-=1 print(p, num) while c >= 8 and p > 0: c -= 8 p -= 1 count += 1 while p > 0: c += 1 if c == 8: count += 1 c = 0 p -= 1 print(count)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; int n; string s; int main() { int cn = 0; cin >> n >> s; for (int i = 1; i <= n; i++) { if (s[i] == '8') ++cn; } int tmp = n / 11; if (cn) printf("%d\n", min(cn, (n - min(cn, tmp)) / 10)); else printf("%d\n", 0); return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int a[10] = {}; for (int i = 0; i < n; i++) a[s[i] - 48]++; int sum = 0; for (int i = 0; i < 10; i++) { if (i != 8) sum = sum + a[i]; } if (a[8] <= sum / 10) cout << a[8]; else if (sum % 10 == 0 && a[8] > sum / 10) cout << sum / 10 - 1; else { sum = sum + a[8] - sum / 10; cout << sum / 10; } return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; char s[1000]; int a[50]; int main() { int n; scanf("%d", &n); scanf("%s", s + 1); for (int i = 1; i <= n; ++i) { a[s[i] - '0']++; } printf("%d\n", min(a[8], (n - a[8]) / 10)); }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-8; const int inf = 1000000000; const long long infLL = 1000000000000000000; long long MOD = 1000000000; inline bool checkbit(long long n, int i) { return n & (1LL << i); } inline long long setbit(long long n, int i) { return n | (1LL << i); } inline long long resetbit(long long n, int i) { return n & (~(1LL << i)); } inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline bool isInside(pair<int, int> p, long long n, long long m) { return (p.first >= 0 && p.first < n && p.second >= 0 && p.second < m); } inline bool isInside(pair<int, int> p, long long n) { return (p.first >= 0 && p.first < n && p.second >= 0 && p.second <= n); } inline bool isSquare(long long x) { long long s = sqrt(x); return (s * s == x); } inline bool isFib(long long x) { return isSquare(5 * x * x + 4) || isSquare(5 * x * x - 4); } inline bool isPowerOfTwo(long long x) { return (x && !(x & (x - 1))); } inline bool normal(long long &a, long long MOD) { a %= MOD; (a < 0) && (a += MOD); } inline long long modMul(long long a, long long b, long long MOD) { a %= MOD, b %= MOD; normal(a, MOD), normal(b, MOD); return (a * b) % MOD; } inline long long modAdd(long long a, long long b, long long MOD) { a %= MOD, b %= MOD; normal(a, MOD), normal(b, MOD); return (a + b) % MOD; } inline long long modSub(long long a, long long b, long long MOD) { a %= MOD, b %= MOD; normal(a, MOD), normal(b, MOD); a -= b; normal(a, MOD); return a; } inline long long modPow(long long b, long long p, long long MOD) { long long r = 1; while (p) { if (p & 1) r = modMul(r, b, MOD); b = modMul(b, b, MOD); p >>= 1; } return r; } inline long long modInverse(long long a, long long MOD) { return modPow(a, MOD - 2, MOD); } inline long long modDiv(long long a, long long b, long long MOD) { return modMul(a, modInverse(b, MOD), MOD); } struct func { bool operator()(pair<int, int> const &a, pair<int, int> const &b) { if (a.first == b.first) { return (a.second < b.second); } return (a.first < b.first); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; string s; int n, ans; int cnt = 0; cin >> n; cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == '8') { cnt++; } } ans = n / 11; if (ans <= cnt) { cout << ans << '\n'; } else { cout << 0 << '\n'; } return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python2
a = int(input()) b = raw_input() list1 = list(b) g=list1.count('8') h=a-g if(h>=g*10): print g else: print g//10
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; string ntos(long long int n) { ostringstream str1; str1 << n; return str1.str(); } long long int ston(string s) { long long int x; stringstream str1(s); str1 >> x; return x; } char a1[3] = {'R', 'G', 'B'}; char b1[3] = {'G', 'B', 'R'}; char c1[3] = {'B', 'R', 'G'}; bool bal(pair<long long int, long long int> a, pair<long long int, long long int> b) { return b.second > a.second; } int main() { string s; cin >> s; int e = 0; int h = (s.size()) / 11; for (int i = 0; i < s.size(); i++) { if (s[i] == '8') e++; } cout << min(e, h); }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
n=int(input()) list=[] c=[] d={} list=input().strip(' ') if len(list)<11: print(0) else: try: q=list.index('8') for i in list: d['8']=d.get('8',0)+1 w=d['8'] p=(len(list))//10 print(p) if w>=p: print(p) else: print(w) except: print(0)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int freq[101]; int main() { int n; char a[101]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; freq[a[i]]++; } if (freq['8'] <= freq['0']) cout << freq['8'] << endl; else if (freq['0'] <= freq['8']) cout << freq['0'] << endl; else cout << 0 << endl; return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Main { static StringBuilder data = new StringBuilder(); final static FastReader in = new FastReader(); public static void main(String[] args) { int n = in.nextInt(); String t=in.nextLine(); int e=0; for (int i = 0; i < n; i++) { if(t.charAt(i)=='8'){ e++; } } int answ=0; while (true){ if(n>=11&&e>0){ answ++; n-=10; e--; }else{break;} } System.out.println(answ); } static void fileOut(String s) { File out = new File("output.txt"); try { FileWriter fw = new FileWriter(out); fw.write(s); fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String path) { try { br = new BufferedReader(new InputStreamReader(new FileInputStream(path))); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.Scanner; public class PhoneNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; String ch=""; do{ System.out.print("n = "); n = sc.nextInt(); }while ((n<1)||(n>=100)); do{ System.out.print("ch = "); ch = sc.next(); }while (ch.length()!=n); if(ch.indexOf("8")==-1){ System.out.println(0); }else{ //int longChWithout8 = ch.length() - (ch.length() - ch.replace("8", "").length()); //int numberOf8 = ch.length() - ch.replace("8", "").length(); int chDiv11 = ch.length() / 11; System.out.println(chDiv11); } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class _0309PhoneNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); String input=sc.nextLine(); if(input.indexOf('8')==-1) { System.out.println(0); return; } Map<Character,Integer> store = new HashMap<Character,Integer>(); for(int i=0;i<n;i++) { char temp=input.charAt(i); store.put(temp, store.getOrDefault(temp, 0)+1); } Integer min = Collections.min(store.values()); System.out.println(min); } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.*; public class A{ public static void main (String[] args) { int count=0,len=0,div=0; Scanner in=new Scanner(System.in); int n=in.nextInt();if(n<=100){ if(n>=1){ String s=in.next(); char[] array=s.toCharArray(); for(int i=0;i<array.length;i++){ if((int)array[i]==56){ count++; } } len=s.length()-count; div=len/8; if(count<div){ System.out.println(count); } else{ System.out.println(div); } } } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; public class Main { public static void main(String[] args){ InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solve(1, in, out); out.close(); } static void solve(int testNumber, InputReader in, PrintWriter out){ int size = in.nextInt()/11; String s = in.next(); char[] card = s.toCharArray(); int ans=0; while(Arrays.binarySearch(card,'8')>0 && size>0){ ans++; size--; } out.println(ans); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class PhoneNumbers { public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int n=Integer.parseInt(br.readLine()); String s=br.readLine(); int c=0; if(n==11 && s.contains("8")) { System.out.println("1"); } else if(n>11 && s.contains("8")) { int l=n/11; for(int i=0;i<s.length()-1;i++) { if(s.charAt(i)=='8')c++; } //System.out.println(c); if(l>c || l<c || l==c)System.out.println(l); } else System.out.println("0"); } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { int n; char s[100], dummy[1]; int number = 0; scanf("%d", &n); gets(dummy); gets(s); if (strlen(s) != n) { printf("%d", number); } else { for (int i = 0; i < n; i++) { if (s[i] == '0' || s[i] == '1' || s[i] == '2' || s[i] == '3' || s[i] == '4' || s[i] == '5' || s[i] == '6' || s[i] == '7' || s[i] == '8' || s[i] == '9') { if (s[i] == '8') { number++; if (number == n % 10) { break; } } } else break; } printf("%d", number); } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python2
n=int(raw_input()) l=list(int(n) for n in raw_input().split()) m=n/11 h=l.count(8) if h>=m: print m elif h<m: print h else: print '0'
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char a[105]; int ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == '8') ans++; } if (n % 11 == 0 && ans >= n / 11) { cout << n / 11 << endl; } else cout << 0 << endl; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int i, k, c = 0; cin >> i; char a[100]; for (k = 0; k < i; k++) { cin >> a[k]; } for (k = 0; k < i; k++) { if (a[k] == '8') c++; } if (c >= i / 11) { cout << i / 11 << endl; } else if (c == 1 && i >= 11) { cout << 1 << endl; } else cout << 0 << endl; return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char a[105]; int ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == '8') ans++; } if (n % 11 == 0 && ans >= n / 11) { cout << ans << endl; } else cout << 0 << endl; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
N = int(input()) eight_amount = input().count("8") if eight_amount == 0: print(0) else: print((N // 10) * max(1, N // 10 - eight_amount))
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.Scanner; public class PhoneNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n; String ch=""; do{ //System.out.println("n = "); n = sc.nextInt(); }while ((n<1)||(n>100)); do{ //System.out.println("ch = "); ch = sc.next(); }while ((ch.length()!=n)||(ch.indexOf(" ")!=-1)||(!ch.matches("[0-9]+"))); if(ch.indexOf("8")==-1){ System.out.println(0); }else{ int longChWithout8 = ch.length() - (ch.length() - ch.replace("8", "").length()); int numberOf8 = ch.length() - ch.replace("8", "").length(); if (numberOf8 <= longChWithout8 / 11){ System.out.println(numberOf8); } else{ System.out.println(numberOf8); } } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.io.IOException; import java.util.*; public class OnlineSub { static FasterScanner in = new FasterScanner(); public static void main(String[] args) { int n = in.nextInt(); String s = in.nextLine(); int c8 = 0, co = 0; for(int i=0; i<n; i++){ if(s.charAt(i)=='8')c8++; else co++; } int div = co/10; int rem = co%10; int ans = Math.min(c8, div); if(c8>div){ int dif = c8-div; if(dif+rem>=11)ans++; } System.out.println(ans); } static class FasterScanner { private byte[] buf = new byte[1024]; private int curChar; private int snumChars; public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; char s[1000]; scanf("%d", &n); scanf("%s", s); int a = 0; int b = 0; for (int i = 0; i < n; i++) { if (s[i] == '8') a++; else b++; } if (b % 10 != 0 and a != 0) { int num = a - 1; b += num; a = 1; num = b % 10; a += num; } printf("%d", min(a, b)); return 0; }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.InputStreamReader; //import for Scanner and other utility classes import java.util.*; import java.io.*; public class TestClass { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String args[]) throws Exception { InputReader in=new InputReader(System.in); PrintWriter w=new PrintWriter(System.out); int n=in.nextInt(); String s=in.readString(); int[] fr=new int[10]; for(int i=0;i<n;i++) { fr[(int)s.charAt(i)-48]++; } int min=Integer.MAX_VALUE; //w.println(fr[8]); for(int i=0;i<=8;i++) { if(min>fr[i] && fr[i]!=0) { min=fr[i]; } } if(min==Integer.MAX_VALUE || fr[8]==0) { w.println("0"); } else w.println(min); w.close(); } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; cin >> s; int e8 = 0, oth = 0; for (int i = 0; i < n; i++) if (s[i] == '8') e8++; else oth++; if (n < 10) cout << 0 << endl; else if (e8 == n) cout << n / 10 << endl; else if ((oth / 10) >= e8) cout << e8 << endl; else { for (int i = e8; i >= 1; i--) if (((n - i) / 10) == i) { cout << i << endl; break; } } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
ch = input("Enter your string \n") nb = 0 long = len(ch) for i in range(0, long): if ch[i] == '8': nb += 1 while ((long - nb) % 10 != 0) and nb != 1: nb -= 1 if long >= 11: if nb == 0: print(nb) else: numbers = int((long - nb) / 10) print(numbers) else: print(0)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
#22 #0011223344556677889988 n = int(input()) t = input() c = t.count('8') print (min(c, (n-c)//10))
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python2
n=input() a=raw_input() if 22>len(a)>=11 and a.count("8")>0: print 1 elif len(a)>=22: if len(a)/11<=a.count("8"): print len(a)/11 else: print a.count("8")
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.Scanner; /** * * @author CHAGO */ public class A { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String a=sc.next(); int s=0; int i=0; while (n>=11) { int b=a.indexOf("8", i); if (b!=-1) { s++; i=b; n-=11; }else{ n=0; } } System.out.println(s); } }
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python2
n = input() arr = map(int,raw_input()) cnt = 0 for i in arr: if i == 8: cnt += 1 print min(cnt,n/8)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
python3
m = int(input()) j = input() h = j.count('8') k = [] f = (len(j) - h) // 10 otv,otv1 = h,f for i in range(otv): k.append(min(otv,otv1)) otv -= 1 otv1 += 1 otv,otv1 = h,f for i in range(otv): k.append(min(otv,otv1)) otv += 1 otv1 -= 1 if k != []: print(max(k)) else: print(0)
1060_A. Phone Numbers
Let's call a string a phone number if it has length 11 and fits the pattern "8xxxxxxxxxx", where each "x" is replaced by a digit. For example, "80123456789" and "80000000000" are phone numbers, while "8012345678" and "79000000000" are not. You have n cards with digits, and you want to use them to make as many phone numbers as possible. Each card must be used in at most one phone number, and you don't have to use all cards. The phone numbers do not necessarily have to be distinct. Input The first line contains an integer n — the number of cards with digits that you have (1 ≤ n ≤ 100). The second line contains a string of n digits (characters "0", "1", ..., "9") s_1, s_2, …, s_n. The string will not contain any other characters, such as leading or trailing spaces. Output If at least one phone number can be made from these cards, output the maximum number of phone numbers that can be made. Otherwise, output 0. Examples Input 11 00000000008 Output 1 Input 22 0011223344556677889988 Output 2 Input 11 31415926535 Output 0 Note In the first example, one phone number, "8000000000", can be made from these cards. In the second example, you can make two phone numbers from the cards, for example, "80123456789" and "80123456789". In the third example you can't make any phone number from the given cards.
{ "input": [ "22\n0011223344556677889988\n", "11\n00000000008\n", "11\n31415926535\n" ], "output": [ "2\n", "1\n", "0\n" ] }
{ "input": [ "51\n882889888888689888850888388887688788888888888858888\n", "55\n7271714707719515303911625619272900050990324951111943573\n", "72\n888488888888823288848804883838888888887888888888228888218488897809784868\n", "65\n44542121362830719677175203560438858260878894083124543850593761845\n", "54\n438283821340622774637957966575424773837418828888614203\n", "100\n1976473621569903172721407763737179639055561746310369779167351419713916160700096173622427077757986026\n", "100\n2833898888858387469888804083887280788584887487186899808436848018181838884988432785338497585788803883\n", "42\n885887846290886288816884858898812858495482\n", "75\n878909759892888846183608689257806813376950958863798487856148633095072259838\n", "11\n55814018693\n", "31\n0868889888343881888987888838808\n", "21\n888888888888000000000\n", "62\n18888883884288488882387888486858887882838885288886472818688888\n", "77\n11111111111111111111111111111111111111111111111111111111111111111111111111111\n", "30\n888888888888888888888888888888\n", "64\n8885984815868480968883818886281846682409262501034555933863969284\n", "44\n15920309219313427633220119270900111650391207\n", "97\n4088468966684435599488804806521288358953088399738904557539253573051442198885776802972628197705081\n", "100\n8800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n", "50\n88888888888888888888888888888888888888888888888888\n", "20\n88888888888888888888\n", "32\n88888888888888888888888888888888\n", "82\n8889809888888888485881851986857288588888888881988888868888836888887858888888888878\n", "91\n8828880888888884883888488888888888888881888888888884888888848588888808888888888888888880888\n", "87\n311753415808202195240425076966761033489788093280714672959929008324554784724650182457298\n", "85\n6888887655188885918863889822590788834182048952565514598298586848861396753319582883848\n", "100\n8088888818885808888888848829886788884187188858898888888788988688884828586988888888288078638898728181\n", "21\n888111111111111111111\n", "1\n8\n", "93\n888088898748888038885888818882806848806887888888882087481868888888177809288888889648468888188\n", "77\n11233392925013001334679215120076714945221576003953746107506364475115045309091\n", "40\n8888888888888888888888888888888888888888\n", "33\n888800000000000000000000000000000\n", "21\n881234567900123456790\n", "98\n87247250157776241281197787785951754485531639139778166755966603305697265958800376912432893847612736\n", "90\n888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "22\n4215079217017196952791\n", "99\n509170332523502565755650047942914747120102240396245453406790272793996913905060450414255616791704320\n", "96\n812087553199958040928832802441581868680188987878748641868838838835609806814288472573117388803351\n", "1\n0\n", "100\n8888888888828188888888888888888808888888888888888888891888888768888888888288888885886888838888888888\n", "11\n80000000000\n", "86\n84065885114540280210185082984888812185222886689129308815942798404861082196041321701260\n", "92\n86888880558884738878888381088888888895888881888888888368878888888884888768881888888888808888\n", "76\n7900795570936733366353829649382870728119825830883973668601071678041634916557\n", "32\n88000000000000000000000000000000\n", "70\n8888888888888888888888888888888888888888888888888888888888888888888888\n", "11\n88888888888\n", "21\n888000000000000000000\n", "66\n747099435917145962031075767196746707764157706291155762576312312094\n", "22\n8899999999999999999999\n", "11\n81234567123\n", "41\n78888884888874788841882882888088888588888\n", "10\n8888888888\n", "100\n2867878187889776883889958480848802884888888878218089281860321588888888987288888884288488988628618888\n", "66\n157941266854773786962397310504192100434183957442977444078457168272\n", "44\n30153452341853403190257244993442815171970194\n", "63\n728385948188688801288285888788852829888898565895847689806684688\n", "100\n1835563855281170226095294644116563180561156535623048783710060508361834822227075869575873675232708159\n", "21\n888888555555555555555\n", "100\n8881888389882878867888888888888888888886388888888870888884878888089888883898887888808688888487888888\n", "53\n85838985300863473289888099788588319484149888886832906\n", "60\n888888888888888888888888888888888888888888888888888888888888\n", "100\n8820286285185244938452488887088871457098945874486988698468788381417332842888928188688887641132194956\n", "11\n24572366390\n", "84\n181288888282608548858058871581888853888486785801381108858832882809848798828837386086\n", "32\n88257478884887437239023185588797\n", "99\n097167815527663544905782574817314139311067328533970663873718450545467450059059869618211361469505108\n", "43\n7404899846883344886153727489084158470112581\n", "100\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008\n", "8\n12345678\n", "88\n2694079127792970410465292300936220976260790323517221561516591792566791677970332966660472\n", "21\n582586788289484878588\n", "33\n270375004567749549929235905225024\n", "50\n88000000000000000000000000000000000000000000000000\n", "33\n429980628264468835720540136177288\n", "27\n888000000000000000000000000\n", "10\n8000000000\n", "74\n70988894874867688968816582886488688881063425288316858438189808828755218508\n", "22\n6188156585823394680191\n", "81\n808888883488887888888808888888888888188888888388888888888888868688888488888882888\n", "57\n888888888888888888888888888888888888888888888888888888888\n", "100\n6451941807833681891890004306065158148809856572066617888008875119881621810329816763604830895480467878\n", "83\n88584458884288808888588388818938838468960248387898182887888867888888888886088895788\n", "11\n81234567090\n", "21\n880000000000000000000\n", "94\n8188948828818938226378510887848897889883818858778688882933888883888898198978868888808082461388\n", "52\n8878588869084488848898838898788838337877898817818888\n", "61\n8880888836888988888988888887388888888888868898887888818888888\n", "71\n88888888888888888888888188888805848888788088888883888883187888838888888\n", "95\n29488352815808808845913584782288724288898869488882098428839370889284838688458247785878848884289\n", "73\n2185806538483837898808836883483888818818988881880688028788888081888907898\n", "80\n88888888888888888888888888888888888888888888888888888888888888888888888888888888\n", "55\n3982037603326093160114589190899881252765957832414122484\n", "100\n8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888\n" ], "output": [ "4\n", "0\n", "6\n", "5\n", "4\n", "1\n", "9\n", "3\n", "6\n", "1\n", "2\n", "1\n", "5\n", "0\n", "2\n", "5\n", "0\n", "8\n", "2\n", "4\n", "1\n", "2\n", "7\n", "8\n", "7\n", "7\n", "9\n", "1\n", "0\n", "8\n", "0\n", "3\n", "3\n", "1\n", "8\n", "8\n", "0\n", "0\n", "8\n", "0\n", "9\n", "1\n", "7\n", "8\n", "6\n", "2\n", "6\n", "1\n", "1\n", "0\n", "2\n", "1\n", "3\n", "0\n", "9\n", "5\n", "2\n", "5\n", "9\n", "1\n", "9\n", "4\n", "5\n", "9\n", "0\n", "7\n", "2\n", "9\n", "3\n", "1\n", "0\n", "0\n", "1\n", "0\n", "2\n", "3\n", "2\n", "0\n", "6\n", "2\n", "7\n", "5\n", "9\n", "7\n", "1\n", "1\n", "8\n", "4\n", "5\n", "6\n", "8\n", "6\n", "7\n", "5\n", "9\n" ] }
IN-CORRECT
java
import java.util.Scanner; public class Watermelon { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long n=sc.nextLong(); sc.nextLine(); String m=sc.nextLine(); char[] c; c = m.toCharArray(); if(n%11==0){ int count=0; for(int i=0;i<c.length;i++){ if('8'==c[i]){ count++; } } if(count>=(n/11)){ System.out.println(n/11); } else System.out.println(count); } else System.out.println("0002"); } }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 1e5 + 20; int n, k, M; int inv[MAXN], pre_inv[MAXN]; void math_pre() { inv[1] = 1; for (int i = 2; i <= ((n < 4) ? 4 : n); ++i) inv[i] = 1ll * (M - M / i) * inv[M % i] % M; for (int i = 1; i <= n; ++i) pre_inv[i] = (pre_inv[i - 1] + inv[i]) % M; } struct map { static const int MAXMap = 2; int tot; struct pad { int key, val; pad() {} pad(const int &KEY, const int &VAL) : key(KEY), val(VAL) {} } node[MAXMap + 1]; map() { tot = 0; } pad *find(const int &key) { pad *ret = node; while (ret - node < tot && ret->key != key) ++ret; return ret; } void insert(const pad &new_element) { node[tot++] = new_element; } pad *begin() { return &node[0]; } pad *end() { return &node[tot]; } } Map; void solve(const int &l, const int &r, const int &h) { if (l >= r || h <= 1) { int len = r - l + 1; map::pad *it = Map.find(len); if (it == Map.end()) Map.insert(map::pad(len, 1)); else ++it->val; return; } int mid = (l + r) >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } int calc(const int &len1, const int &len2) { int ret = 0; for (int i = 1; i <= len1; ++i) ret = ((ret + 1ll * inv[2] * len2 % M - (pre_inv[i + len2] - pre_inv[i + 1 - 1])) % M + M) % M; return ret; } int main() { scanf("%d%d%d", &n, &k, &M); math_pre(); solve(1, n, k); int ans = 0; for (map::pad *it = Map.begin(); it != Map.end(); ++it) { int len = it->key, cnt = it->val; ans = (ans + 1ll * cnt * len % M * (len - 1) % M * inv[4] % M) % M; } for (map::pad *it1 = Map.begin(); it1 != Map.end(); ++it1) for (map::pad *it2 = Map.begin(); it2 != Map.end(); ++it2) { if (it1 == it2) { int len = it1->key, cnt = 1ll * (0 + (it1->val - 1)) * it1->val / 2 % M; ans = (ans + 1ll * cnt * calc(len, len) % M) % M; } else if (it1->key < it2->key) { int len1 = it1->key, len2 = it2->key, cnt = 1ll * it1->val * it2->val % M; ans = (ans + 1ll * cnt * calc(len1, len2) % M) % M; } } printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; const long long N = 1e5 + 5; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while ((ch > '9' || ch < '0')) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar(); return x * f; } inline long long ksm(long long x, long long y = mod - 2, long long z = mod) { long long ret = 1; while (y) { if (y & 1LL) ret = ret * x % mod; x = x * x % mod; y >>= 1LL; } return ret; } long long inv[N], sum[N]; void init(long long n) { inv[1] = 1; for (register signed i = 2; i <= n; i++) inv[i] = inv[mod % i] * (mod - mod / i) % mod; for (register signed i = 1; i <= n; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; } long long k, n, ans; map<long long, long long> S; void MS(long long l, long long r, long long h) { if (h == k || l == r) { S[r - l + 1]++; return; } long long mid = (l + r) >> 1; MS(l, mid, h + 1); MS(mid + 1, r, h + 1); } long long calc(long long x, long long y) { long long res = x * y % mod; for (register signed i = 1; i <= x; ++i) res -= (sum[i + y] - sum[i]) * 2, res %= mod; return (res % mod + mod) % mod; } signed main() { n = read(); k = read(); mod = read(); init(n); MS(1, n, 1); for (map<long long, long long>::iterator X = S.begin(); X != S.end(); X++) { long long x = X->first, y = X->second; ans += x * (x - 1) % mod * inv[2] % mod * y % mod; ans %= mod; ans += y * (y - 1) % mod * inv[2] % mod * calc(x, x) % mod; ans %= mod; } for (map<long long, long long>::iterator X = S.begin(); X != S.end(); X++) for (map<long long, long long>::iterator Y = S.begin(); Y != S.end(); Y++) { long long x = X->first, y = Y->first, a = X->second, b = Y->second; if (x >= y) continue; ans += calc(x, y) * a % mod * b % mod; ans %= mod; } ans = ans * inv[2] % mod; ans += mod; ans %= mod; cout << ans << '\n'; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 200005; int n, k, q, mod, cnt[2]; int sH[N], ans; void up(int &x, int y) { x += y - mod, x += x >> 31 & mod; } void up(int &x, int y, int z) { x = (x + (long long)y * z) % mod; } int c(int n) { return (long long)n * (n - 1) / 2 % mod; } void solve(int n, int m) { if (m == 1 || n == 1) return void(++cnt[n - q]); solve(n >> 1, m - 1), solve(n + 1 >> 1, m - 1); } int f(int a, int b) { return ((long long)a * b % mod * (mod + 1 >> 1) + mod + sH[a] + sH[b] - sH[a + b]) % mod; } int main() { std::ios::sync_with_stdio(0), std::cin.tie(0); std::cin >> n >> k >> mod; q = n >> std::min(k - 1, 20), solve(n, k); sH[1] = 1; for (int i = 2; i <= n; ++i) sH[i] = (long long)(mod - mod / i) * sH[mod % i] % mod; for (int i = 2; i <= n; ++i) up(sH[i], sH[i - 1]); for (int i = 2; i <= n; ++i) up(sH[i], sH[i - 1]); up(ans, (long long)c(q) * (mod + 1 >> 1) % mod, cnt[0]); up(ans, (long long)c(q + 1) * (mod + 1 >> 1) % mod, cnt[1]); up(ans, f(q, q), c(cnt[0])); up(ans, f(q + 1, q + 1), c(cnt[1])); up(ans, f(q, q + 1), (long long)cnt[0] * cnt[1] % mod); std::cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int md; inline void add(int &a, int b) { a += b; if (a >= md) a -= md; } inline void sub(int &a, int b) { a -= b; if (a < 0) a += md; } inline int mul(int a, int b) { return (int)((long long)a * b % md); } inline int power(int a, long long b) { int res = 1; while (b > 0) { if (b & 1) { res = mul(res, a); } a = mul(a, a); b >>= 1; } return res; } inline int inv(int a) { a %= md; if (a < 0) a += md; int b = md, u = 0, v = 1; while (a) { int t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += md; return u; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k >> md; if (k >= 20 || n <= (1 << (k - 1))) { cout << 0 << '\n'; return 0; } int bc = (1 << (k - 1)); int small_size = n / bc; int big_size = small_size + 1; int big_cnt = n % bc; int small_cnt = bc - big_cnt; vector<int> blocks(bc); for (int i = 0; i < n; i++) { blocks[i % (int)blocks.size()]++; } map<int, int> mp; for (int x : blocks) { mp[x]++; } vector<int> fact(n + 1), inv_fact(n + 1); fact[0] = inv_fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = mul(fact[i - 1], i); inv_fact[i] = inv(fact[i]); } int ans = 0; for (int b1id = 0; b1id < bc; b1id++) { int b = blocks[b1id]; add(ans, mul(mul(b, b - 1), inv(4))); } vector<int> sum_inv(n + 1); for (int i = 0; i < n; i++) { sum_inv[i + 1] = sum_inv[i]; add(sum_inv[i + 1], inv(i + 1)); } for (int b1id = 0; b1id < bc; b1id++) { int b1 = blocks[b1id]; if (b1 == small_size) small_cnt--; else big_cnt--; for (int x = 2; x <= b1; x++) { if (small_cnt > 0) { int aux = sum_inv[x + small_size]; sub(aux, sum_inv[x]); int prob = mul(x - 1, aux); add(ans, mul(small_cnt, mul(prob, inv(2)))); } if (big_cnt > 0) { int aux = sum_inv[x + big_size]; sub(aux, sum_inv[x]); int prob = mul(x - 1, aux); add(ans, mul(big_cnt, mul(prob, inv(2)))); } } if (b1 == small_size) small_cnt++; else big_cnt++; } cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using std::cerr; using std::endl; const int N = 1e5 + 10; int n, K, P, inv[N], sum[N], ans; std::map<int, int> map; void divide(int l, int r, int dep) { if (l == r || dep == K) { return ++map[r - l + 1], void(); } int mid = (l + r) >> 1; divide(l, mid, dep + 1); divide(mid + 1, r, dep + 1); } inline long long calc(int x, int y) { int ret = 1ll * x * y % P * inv[2] % P; for (int i = 1; i <= x; ++i) ret = (ret - sum[i + y] + sum[i]) % P; ret = (ret % P + P) % P; return ret; } int main() { scanf("%d %d %d", &n, &K, &P); divide(1, n, 1); inv[1] = sum[1] = 1; for (int i = 2, lim = std::max(4, n); i <= lim; ++i) { inv[i] = P - 1ll * P / i * inv[P % i] % P; sum[i] = (sum[i - 1] + inv[i]) % P; } for (auto m : map) { ans = (ans + 1ll * m.first * (m.first - 1) % P * inv[4] % P * m.second) % P; ans = (ans + calc(m.first, m.first) * m.second % P * (m.second - 1) % P * inv[2]) % P; } for (auto m1 : map) for (auto m2 : map) if (m1.first < m2.first) ans = (ans + calc(m1.first, m2.first) * m1.second % P * m2.second) % P; std::cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using ld = long double; int mod; ll pw(ll a, int b) { if (!b) { return 1; } ll v = pw(a, b / 2); v = (v * v) % mod; if (b & 1) { v = (v * a) % mod; } return v; } ll ans = 0; ll norm(ll x) { x %= mod; if (x < 0) { x += mod; } return x; } const int N = 300228; ll invs[N]; void go(ll mul, int a, int b) { mul %= mod; ll cans = 0; for (int i = 2; i <= a + b; ++i) { int al = max(1, i - b); int ar = min(a, i - 1); if (al <= ar) { ll c = ar - al + 1; cans += c * invs[i]; cans %= mod; } } ans = (ans + cans * mul) % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20); int n, k; cin >> n >> k >> mod; for (int i = 1; i <= n; ++i) { invs[i] = pw(i, mod - 2); } int c = 1; for (int i = 1; i < k; ++i) { c *= 2; if (c >= n) { cout << 0 << "\n"; return 0; } } int sz = n / c; ll bc = n % c; go(bc * (bc - 1), sz + 1, sz + 1); go((c - bc) * (c - bc - 1), sz, sz); go(2ll * bc * (c - bc), sz, sz + 1); ll all = ll(n) * ll(n - 1) / 2; cout << norm((all - ans) * invs[2]) << "\n"; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; int Mod; int inv4; int inv[200010]; int sum[200010]; int fpow(int a, int b) { int ans = 1, t = a; while (b) { if (b & 1) ans = 1ll * ans * t % Mod; t = 1ll * t * t % Mod; b >>= 1; } return ans; } void init() { int N = 200000; for (int i = 1; i <= N; i++) { inv[i] = fpow(i, Mod - 2); sum[i] = (sum[i - 1] + inv[i]) % Mod; } return; } int mn = 0; int cnt[100010]; int calc(int x, int y) { int ans = 1ll * x * y % Mod * inv[2] % Mod; for (int i = 1; i <= x; i++) ans = ((ans - sum[i + y] + sum[i]) % Mod + Mod) % Mod; return ans; } void divide(int l, int r, int d) { if (l == r || d <= 1) { cnt[r - l + 1]++; mn = min(mn, r - l + 1); return; } int mid = (l + r) >> 1; divide(l, mid, d - 1); divide(mid + 1, r, d - 1); return; } int main() { scanf("%d %d %d", &n, &k, &Mod); mn = n; init(); divide(1, n, k); int s = mn, t = mn + 1; int x = cnt[mn], y = cnt[mn + 1]; int ans = 0; ans = (ans + 1ll * s * (s - 1) % Mod * inv[4] % Mod * x) % Mod; ans = (ans + 1ll * t * (t - 1) % Mod * inv[4] % Mod * y) % Mod; ans = (ans + 1ll * x * (x - 1) % Mod * inv[2] % Mod * calc(s, s)) % Mod; ans = (ans + 1ll * y * (y - 1) % Mod * inv[2] % Mod * calc(t, t)) % Mod; ans = (ans + 1ll * x * y % Mod * calc(s, t)) % Mod; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int mod; const int maxn = 200111; map<int, int> mp; int n, k; long long inv[maxn]; int id[maxn], g[maxn], gn; long long sum; long long calc(long long a, long long b) { long long ret = 0; for (int i = 2; i <= a + b; i++) { long long l = max(1ll, i - b), r = min(i - 1ll, a); long long cnt = max(0ll, r - l + 1); ret = (ret + cnt * inv[i]) % mod; } return ret; } void solve(int l, int r, int k) { if (l == r || k == 1) { gn++; for (int i = l; i <= r; i++) id[i] = i - l + 1, g[i] = gn; mp[r - l + 1]++; return; } int m = l + r >> 1; solve(l, m, k - 1); solve(m + 1, r, k - 1); } int main() { cin >> n >> k >> mod; inv[1] = 1; for (int i = 2; i < maxn; i++) inv[i] = mod - 1ll * (mod / i) * inv[mod % i] % mod; solve(1, n, k); long long ans = (1ll * n * (n - 1) / 2) % mod; for (auto x : mp) { for (auto y : mp) { if (x.first > y.first) continue; if (x.first == y.first) { ans = (ans - 2ll * (1ll * x.second * (x.second - 1) / 2) % mod * calc(x.first, x.first)) % mod; } else ans = (ans - 2ll * x.second * y.second % mod * calc(x.first, y.first)) % mod; } } cout << ((ans * inv[2] % mod) + mod) % mod << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } const char lf = '\n'; namespace ae86 { const int bufl = 1 << 15; char buf[bufl], *s = buf, *t = buf; inline int fetch() { if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; } return *s++; } inline int ty() { int a = 0; int b = 1, c = fetch(); while (!isdigit(c)) b ^= c == '-', c = fetch(); while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); return b ? a : -a; } } // namespace ae86 using ae86::ty; const int _ = 100007; int mo; template <typename T1, typename T2> inline T1 ad(T1 &a, T2 b) { return a = a + b >= mo ? a + b - mo : a + b; } template <typename T1, typename T2> inline T1 dl(T1 &a, T2 b) { return a = a >= b ? a - b : a - b + mo; } template <typename T1, typename T2> inline T1 add(T1 a, T2 b) { return a + b >= mo ? a + b - mo : a + b; } template <typename T1, typename T2> inline T1 del(T1 a, T2 b) { return a >= b ? a - b : a - b + mo; } long long powa(long long a, long long t) { long long b = 1; a = (a + mo) % mo; while (t) { if (t & 1) b = b * a % mo; a = a * a % mo, t >>= 1; } return b; } inline long long inva(long long a) { return powa(a, mo - 2); } long long ri[_] = {0}, sri[_] = {0}; void fuck(int n = _ - 1) { ri[0] = 0, sri[0] = 0; ri[1] = 1, sri[1] = ri[1]; for (int i = 2; i <= n; i++) ri[i] = ri[mo % i] * (mo - mo / i) % mo, sri[i] = add(sri[i - 1], ri[i]); } map<int, int> cnt; void dfs(int x, int l, int r) { if (x <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; dfs(x - 1, l, mid), dfs(x - 1, mid + 1, r); } long long sumri(long long a, long long b) { long long ans = a * b % mo; for (int i = 1; i <= a; i++) dl(ans, del(sri[i + b], sri[i]) * 2 % mo); return ans; } int n, tim; int main() { ios::sync_with_stdio(0), cout.tie(nullptr); n = ty(), tim = ty(), mo = ty(); fuck(); dfs(tim, 1, n); long long ans = 0; for (auto i : cnt) { long long a = i.first, b = i.second; ad(ans, a * (a - 1) % mo * ri[2] % mo * b % mo); ad(ans, b * (b - 1) % mo * ri[2] % mo * sumri(a, a) % mo); for (auto j : cnt) { long long c = j.first, d = j.second; if (a >= c) continue; ad(ans, sumri(a, c) * b % mo * d % mo); } } cout << ans * ri[2] % mo << lf; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int p; int Pow(int a, int b) { int ans = 1; for (; b; b >>= 1, a = 1ll * a * a % p) { if (b & 1) ans = 1ll * ans * a % p; } return ans; } int solve(int a, int b) { if (a <= 0 || b <= 0) return 0; int ans = 0; for (int sm = 2; sm <= a + b; sm++) ans = (1ll * (min(a, sm - 1) - max(1, sm - b) + 1) * (sm - 2) % p * Pow(2 * sm, p - 2) % p + 1ll * ans) % p; return ans; } int C(int n) { return (1ll * n * (n - 1) / 2) % p; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k >> p; int iv = (p + 1) / 2; pair<int, int> A = {n, 1}, B = {0, 0}; while (--k) { if (A.first & 1) { B = {(A.first / 2) + 1, A.second}; A.first /= 2; break; } A.first /= 2, A.second *= 2; } if (k != 0) { while (--k) { if (A.first & 1) { B.first /= 2, B.second = 2 * B.second + A.second; A.first /= 2; } else { A.first /= 2, A.second = 2 * A.second + B.second; B.first = (B.first + 1) / 2; } } } int ans = 0; ans = (1ll * C(A.first) * iv % p * A.second + 1ll * ans) % p; ans = (1ll * C(B.first) * iv % p * B.second + 1ll * ans) % p; ans = (1ll * C(A.second) * solve(A.first, A.first) + 1ll * ans) % p; ans = (1ll * C(B.second) * solve(B.first, B.first) + 1ll * ans) % p; ans = (1ll * A.second * B.second % p * solve(A.first, B.first) + 1ll * ans) % p; return cout << ans << endl, 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<int> vec; int n, k, q, cnt[100005]; inline int power(int a, int b) { int ans = 1; for (; b; a = 1LL * a * a % q, b >>= 1) ans = b & 1 ? 1LL * ans * a % q : ans; return ans; } inline int inv(int a) { return power(a, q - 2); } void partition(int a, int b) { if (min(a, b) == 1) { if (!cnt[a]++) vec.push_back(a); return; } partition(a >> 1, --b); partition(a + 1 >> 1, b); } int main() { cin >> n >> k >> q; partition(n, k); int ans = 0; for (auto x : vec) { for (auto y : vec) if (x <= y) { int res = 1LL * x * y % q * inv(2) % q; for (int i = 2; i <= x + y; i++) (res += q - (i - max(i - x - 1, 0) - max(i - y - 1, 0) - 1LL) * inv(i) % q) %= q; (ans += 1LL * res * cnt[x] % q * (x ^ y ? cnt[y] : (cnt[y] - 1LL) * inv(2) % q) % q) %= q; } (ans += (x - 1LL) * x % q * inv(4) % q * cnt[x] % q) %= q; } cout << ans; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long inv[100005], n, k, p, cnt[3], len[3], sum[100005], ans; void gb(long long l, long long r, long long h) { if (h <= 1 || l == r) { if (!len[1] || len[1] == r - l + 1) len[1] = r - l + 1, cnt[1]++; else len[2] = r - l + 1, cnt[2]++; return; } long long mid = l + r >> 1; gb(l, mid, h - 1), gb(mid + 1, r, h - 1); } inline long long work(long long x, long long y) { long long s = inv[2] * x % p * y % p; for (long long i = 1; i <= x; i++) s = (s - sum[i + y] + sum[i]) % p; return (s + p) % p; } signed main() { cin >> n >> k >> p; inv[1] = 1; for (long long i = 2; i <= max(n, 4ll); i++) inv[i] = inv[p % i] * (p - p / i) % p; for (long long i = 1; i <= n; i++) sum[i] = (sum[i - 1] + inv[i]) % p; gb(1, n, k); ans = (len[1] * (len[1] - 1) % p * cnt[1] % p * inv[4] + len[2] * (len[2] - 1) % p * cnt[2] % p * inv[4]) % p; ans = (ans + cnt[1] * (cnt[1] - 1) % p * inv[2] % p * work(len[1], len[1])) % p; ans = (ans + cnt[2] * (cnt[2] - 1) % p * inv[2] % p * work(len[2], len[2])) % p; ans = (ans + cnt[1] * cnt[2] % p * work(len[1], len[2])) % p; printf("%lld", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 100007; long long MOD; inline long long FST(long long base, int times) { long long ret = 1; while (times) { if (times & 1) ret = ret * base % MOD; times >>= 1; base = base * base % MOD; } return ret; } long long seg[MAXN], tot_seg; long long inv[MAXN], invS[MAXN]; void getSeg(const int &l, const int &r, const int &h) { if (h <= 1 || l == r) { seg[++tot_seg] = r - l + 1; return; } const int &mid = (l + r) >> 1; getSeg(l, mid, h - 1); getSeg(mid + 1, r, h - 1); return; } inline long long calc(long long max_i, long long max_j) { long long ret = max_i * max_j % MOD; for (int i = 1; i <= max_i; ++i) ret = (ret - (invS[i + max_j] - invS[i]) * 2) % MOD; return ret; } long long buc[2][2]; int main() { int n, k; scanf("%d%d%I64d", &n, &k, &MOD); inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = inv[i - 1] * i % MOD; inv[n] = FST(inv[n], MOD - 2); for (int i = n; i > 1; --i) { long long tmp_inv = inv[i]; inv[i] = inv[i - 1] * inv[i] % MOD; inv[i - 1] = tmp_inv * i % MOD; } for (int i = 1; i <= n; ++i) invS[i] = (invS[i - 1] + inv[i]) % MOD; inv[2] = FST(2, MOD - 2); long long ans = 0; getSeg(1, n, k); for (int i = 1; i <= tot_seg; ++i) { if (!buc[0][0]) buc[0][0] = seg[i]; if (seg[i] == buc[0][0]) ++buc[0][1]; else { if (!buc[1][0]) buc[1][0] = seg[i]; ++buc[1][1]; } ans = (ans + seg[i] * (seg[i] - 1) / 2 % MOD) % MOD; } for (int i = 0; i < 2; ++i) if (buc[i][1] >= 2) ans = (ans + calc(buc[i][0], buc[i][0]) * (buc[i][1] * (buc[i][1] - 1) / 2 % MOD) % MOD) % MOD; if (buc[0][0] && buc[0][1]) ans = (ans + calc(buc[0][0], buc[1][0]) * (buc[1][1] * buc[0][1] % MOD) % MOD) % MOD; ans = ans * inv[2] % MOD; printf("%I64d\n", (ans + MOD) % MOD); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } const int MAXN = 100010; const int INF = 2147483600; long long Mod; int N, K, Q; int mn = 100000; long long inv[MAXN << 1], sm[MAXN << 1]; long long cnt[MAXN << 1]; long long ans; inline void div(int l, int r, int x) { if (x == 1 || l == r) { mn = min(mn, r - l + 1); ++cnt[r - l + 1]; return; } int mid = (l + r) >> 1; div(l, mid, x - 1); div(mid + 1, r, x - 1); } inline void calc(int x) { (ans += cnt[x] * x % Mod * (x - 1) % Mod * inv[4] % Mod) %= Mod; } int main() { N = read(), K = read(), Mod = read(); div(1, N, K); inv[1] = 1; for (int i = 2; i <= 2 * N + 2; i++) inv[i] = (Mod - (Mod / i) * inv[Mod % i] % Mod) % Mod; for (int i = 1; i <= 2 * N + 2; i++) sm[i] = (sm[i - 1] + inv[i]) % Mod; calc(mn); calc(mn + 1); for (int i = 1; i <= mn; i++) { (ans += inv[2] * cnt[mn] % Mod * (cnt[mn] - 1) % Mod * inv[2] % Mod * (mn) % Mod) %= Mod; ans = (ans - cnt[mn] % Mod * (cnt[mn] - 1) % Mod * inv[2] % Mod * ((sm[mn + i] - sm[i] + Mod) % Mod) % Mod + Mod) % Mod; (ans += inv[2] * cnt[mn] % Mod * cnt[mn + 1] % Mod * (mn + 1) % Mod) %= Mod; ans = (ans - cnt[mn] * cnt[mn + 1] % Mod * ((sm[mn + i + 1] - sm[i] + Mod) % Mod) % Mod + Mod) % Mod; (ans += inv[2] * cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] % Mod * (mn + 1) % Mod) %= Mod; ans = (ans - cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] % Mod * ((sm[mn + i + 1] - sm[i] + Mod) % Mod) % Mod + Mod) % Mod; } int i = mn + 1; (ans += inv[2] * cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] % Mod * (mn + 1) % Mod) %= Mod; ans = (ans - cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] % Mod * ((sm[mn + i + 1] - sm[i] + Mod) % Mod) % Mod + Mod) % Mod; cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int N = 100005; int n, d, inv[N << 1], P, Inv2; int c[N]; void getp(int l, int r, int dep) { if (dep <= 1 || l == r) { ++c[r - l + 1]; return; } int mid = (l + r) >> 1; getp(l, mid, dep - 1); getp(mid + 1, r, dep - 1); } int ans; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> d >> P; inv[1] = 1; for (int i = 2; i <= n + n; ++i) { inv[i] = 1ll * (P - P / i) * inv[P % i] % P; } Inv2 = inv[2]; getp(1, n, d); for (int i = 1; i <= n; ++i) { if (c[i]) { ans = (ans + 1ll * i * (i - 1) / 2 % P * Inv2 % P * c[i]) % P; for (int j = i; j <= n; ++j) { if (c[j]) { int sum = 0; for (int k = 2; k <= i + j; ++k) { int t = std::min(i, k - 1) - std::max(1, k - j) + 1; sum = (sum + 1ll * t * (k - 2) % P * inv[k]) % P; } sum = 1ll * sum * Inv2 % P; int t = i == j ? 1ll * c[i] * (c[i] - 1) / 2 % P : 1ll * c[i] * c[j] % P; ans = (ans + 1ll * t * sum) % P; } } } } std::cout << ans << std::endl; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class t> inline t read(t &x) { char c = getchar(); bool f = 0; x = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); if (f) x = -x; return x; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } const long long N = 1e5 + 5; long long ans, a1, a2, b1, b2, n, k, inv[N << 1], mod; long long fpow(long long x, long long y) { long long res = 1; for (; y; y >>= 1, x = x * x % mod) if (y & 1) res = res * x % mod; return res; } void dico(long long l, long long r, long long h) { if (h == 1 || l == r) { l = r - l + 1; if (!a1) a1 = l, b1 = 1; else if (a1 == l) b1++; else a2 = l, b2++; return; } long long mid = l + r >> 1; dico(l, mid, h - 1); dico(mid + 1, r, h - 1); } long long calc(long long n, long long m) { long long res = 0; for (long long i = 2; i <= n + m; i++) res = (res + inv[i] * min(i - 1, n + m - i + 1) % mod) % mod; return mod - res; } void init(long long n) { inv[1] = 1; for (long long i = 2; i <= n; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; } signed main() { read(n, k, mod); ans = n * (n - 1) / 2 * (mod + 1 >> 1) % mod; init(n << 1); dico(1, n, k); ans = (ans + b1 * (b1 - 1) / 2 * calc(a1, a1) % mod) % mod; if (a2) { ans = (ans + b2 * (b2 - 1) / 2 * calc(a2, a2) % mod) % mod; ans = (ans + b1 * b2 % mod * calc(a1, a2) % mod) % mod; } write(ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline T gi() { T f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return f * x; } const int INF = 0x3f3f3f3f, N = 100003, M = N << 1; int n, k, mod; pair<int, int> p[2]; long long inv[N], sum[N]; inline long long qpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod, b >>= 1; } return res; } inline void pre() { for (int i = 1; i <= max(n, 4); i += 1) inv[i] = qpow(i, mod - 2), sum[i] = (sum[i - 1] + inv[i]) % mod; return; } inline long long solve(long long len1, long long len2) { long long res = 0; for (int i = 1; i <= len1; i += 1) res = ((res + len2 * inv[2] % mod - (sum[i + len2] - sum[i]) % mod) % mod + mod) % mod; return res; } int main() { n = gi<int>(), k = gi<int>(), mod = gi<int>(); --k; if (k > 20) k = 20; int u = min(n, 1 << k); int a = n / u, b = a + 1, cnta = b * u - n, cntb = u - cnta; p[0] = {a, cnta}, p[1] = {b, cntb}; pre(); long long ans = 0; for (int i = 0; i <= 1; i += 1) { int len = p[i].first, cnt = p[i].second; ans = (ans + 1ll * cnt * len % mod * (len - 1) % mod * inv[4] % mod) % mod; } for (int i = 0; i <= 1; i += 1) for (int j = 0; j <= 1; j += 1) { if (i == j) { long long len = p[i].first, cnt = 1ll * p[i].second * (p[i].second - 1) / 2 % mod; ans = (ans + 1ll * cnt * solve(len, len) % mod) % mod; } else if (p[i].first < p[j].first) { long long len1 = p[i].first, len2 = p[j].first, cnt = 1ll * p[i].second * p[j].second % mod; ans = (ans + 1ll * cnt * solve(len1, len2) % mod) % mod; } } printf("%lld\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0; char ch = getchar(); bool d = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') d = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return d ? x : -x; } inline unsigned long long rnd() { return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4; } const int N = 1e5 + 5; int a[N], mo, inv2; int C(int n) { return (long long)(n - 1) * n % mo * inv2 % mo; } int ksm(int x, int p) { int res = 1; for (; p; p >>= 1, x = (long long)x * x % mo) { if (p & 1) res = (long long)res * x % mo; } return res; } vector<long long> v; int sum[N], tong[N]; void solve(int l, int r, int k) { if (k <= 1 || l == r) { if (!tong[r - l + 1]) v.push_back(r - l + 1); tong[r - l + 1]++; return; } int mid = l + r >> 1; solve(l, mid, k - 1); solve(mid + 1, r, k - 1); } int calc(int x, int y) { int res = (long long)x * y % mo * inv2 % mo; for (int i = (int)(1); i <= (int)(x); i++) { int ssw = (sum[i + y] - sum[i] + mo) % mo; res = (res - ssw + mo) % mo; } return res; } int main() { int n = read(), k = read(); mo = read(); inv2 = (mo + 1) / 2; for (int i = (int)(1); i <= (int)(n); i++) { int inv = ksm(i, mo - 2); sum[i] = (sum[i - 1] + inv) % mo; } solve(1, n, k); int ans = 0; for (auto x : v) { ans = (ans + (long long)C(x) * inv2 % mo * tong[x]) % mo; ans = (ans + (long long)C(tong[x]) * calc(x, x)) % mo; } for (auto x : v) for (auto y : v) if (x > y) { ans = (ans + (long long)tong[x] * tong[y] % mo * calc(x, y)) % mo; } cout << ans; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 200005; long long n, k, mo, gs[3], len[3], top, inv[N], s[N], ans; map<long long, long long> ma; inline long long read() { long long ret = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -1; ch = getchar(); } while (isdigit(ch)) { ret = ret * 10 + (ch ^ 48); ch = getchar(); } return ret * ff; } void write(long long x) { if (x < 0) { x = -x, putchar('-'); } if (x > 9) write(x / 10); putchar(x % 10 + 48); } void writeln(long long x) { write(x), puts(""); } void writesp(long long x) { write(x), putchar(' '); } void mergesort(long long l, long long r, long long h) { if (l == r || h <= 1) { ma[r - l + 1]++; return; } long long mid = (l + r) >> 1; mergesort(l, mid, h - 1), mergesort(mid + 1, r, h - 1); } long long calc(long long x, long long y) { long long res = x * y % mo * inv[2] % mo; for (long long i = 1; i <= x; i++) res = (res - (s[i + y] - s[i]) % mo + mo) % mo; return res; } signed main() { n = read(), k = read(), mo = read(); mergesort(1, n, k); for (map<long long, long long>::iterator it = ma.begin(); it != ma.end(); it++) { len[++top] = it->first; gs[top] = it->second; } inv[0] = inv[1] = 1; s[0] = 1, s[1] = 2; for (long long i = 2; i <= 200000; i++) inv[i] = (mo - mo / i) * inv[mo % i] % mo, s[i] = (s[i - 1] + inv[i]) % mo; ans = (ans + gs[1] * len[1] % mo * (len[1] - 1) % mo * inv[4] % mo) % mo; ans = (ans + gs[2] * len[2] % mo * (len[2] - 1) % mo * inv[4] % mo) % mo; ans = (ans + gs[1] * (gs[1] - 1) % mo * inv[2] % mo * calc(len[1], len[1]) % mo) % mo; ans = (ans + gs[2] * (gs[2] - 1) % mo * inv[2] % mo * calc(len[2], len[2]) % mo) % mo; ans = (ans + gs[1] * gs[2] % mo * calc(len[1], len[2]) % mo) % mo; write(ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long Getint() { char ch = getchar(); long long x = 0, fh = 1; while (ch < '0' || ch > '9') { if (ch == '-') fh = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { (x *= 10) += ch ^ 48; ch = getchar(); } return fh * x; } const int N = 200005; int n, h; long long mod, fc[N], fiv[N], inv[N], sm[N]; int su[N]; struct nod { int a, l; nod(int x = 0, int y = 0) { a = x; l = y; } }; vector<nod> a; inline long long Solve(int l1, int l2) { long long Ans = 1ll * l1 * l2 % mod * inv[2] % mod; for (int i = 1; i <= l1; i++) (Ans += sm[i] - sm[i + l2] + mod) %= mod; return (Ans % mod + mod) % mod; } void Build(int l, int r, int h) { if (h == 1) { su[r - l + 1]++; return; } if (l == r) { su[1]++; return; } int mid = l + r >> 1; Build(l, mid, h - 1); Build(mid + 1, r, h - 1); } int main() { n = Getint(); h = Getint(); mod = Getint(); fc[0] = fc[1] = fiv[0] = fiv[1] = inv[0] = inv[1] = 1; for (int i = 2; i <= N - 1; i++) { fc[i] = fc[i - 1] * i % mod; inv[i] = (mod - mod / i) * inv[mod % i] % mod; fiv[i] = fiv[i - 1] * inv[i] % mod; } for (int i = 1; i <= N - 1; i++) { sm[i] = (sm[i - 1] + inv[i]) % mod; } Build(1, n, h); long long Ans = 0; for (int i = 1; i <= n; i++) { if (!su[i]) continue; a.push_back(nod(su[i], i)); (Ans += 1ll * su[i] * i % mod * (i - 1) % mod * inv[4]) %= mod; } for (int i = 0; i <= int(a.size()) - 1; i++) { (Ans += 1ll * a[i].a * (a[i].a - 1) % mod * inv[2] % mod * Solve(a[i].l, a[i].l)) %= mod; for (int j = i + 1; j <= int(a.size()) - 1; j++) { (Ans += 1ll * a[i].a * a[j].a % mod * Solve(a[i].l, a[j].l)) %= mod; } } cout << Ans % mod << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { char ch; x = 0; bool fl = false; while (!isdigit(ch = getchar())) (ch == '-') && (fl = true); for (x = (ch ^ '0'); isdigit(ch = getchar()); x = x * 10 + (ch ^ '0')) ; (fl == true) && (x = -x); } template <class T> inline void output(T x) { if (x / 10) output(x / 10); putchar(x % 10 + '0'); } template <class T> inline void ot(T x) { if (x < 0) putchar('-'), x = -x; output(x); putchar(' '); } template <class T> inline void prt(T a[], int st, int nd) { for (register int i = st; i <= nd; ++i) ot(a[i]); putchar('\n'); } namespace Modulo { int mod; inline int ad(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } inline int sub(int x, int y) { return ad(x, mod - y); } inline int mul(int x, int y) { return (long long)x * y % mod; } inline void inc(int &x, int y) { x = ad(x, y); } inline void inc2(int &x, int y) { x = mul(x, y); } inline int qm(int x, int y = mod - 2) { int ret = 1; while (y) { if (y & 1) ret = mul(x, ret); x = mul(x, x); y >>= 1; } return ret; } template <class... Args> inline int ad(const int a, const int b, const Args &...args) { return ad(ad(a, b), args...); } template <class... Args> inline int mul(const int a, const int b, const Args &...args) { return mul(mul(a, b), args...); } } // namespace Modulo using namespace Modulo; namespace Miracle { const int N = 1e5 + 5; int n, k; int iv[N], s[N]; int l1, l2, c1, c2; void divi(int l, int r, int d) { if (d == k || l == r) { if (!l1) { l1 = r - l + 1; ++c1; } else if (r - l + 1 == l1) ++c1; else if (!l2) l2 = r - l + 1, ++c2; else ++c2; return; } int mid = (l + r) >> 1; divi(l, mid, d + 1); divi(mid + 1, r, d + 1); } int calc(int l1, int l2) { if (!l1 || !l2) return 0; int ret = mul(l1, l2, qm(2)); for (register int i = 1; i <= l1; ++i) { ret = sub(ret, sub(s[i + l2], s[i])); } return ret; } int main() { rd(n); rd(k); rd(mod); iv[1] = 1; for (register int i = 2; i <= n; ++i) { iv[i] = mul(mod - mod / i, iv[mod % i]); } for (register int i = 1; i <= n; ++i) s[i] = ad(s[i - 1], iv[i]); divi(1, n, 1); int ans = ad(mul(c1, l1, (l1 - 1), qm(4)), mul(c2, l2, (l2 - 1), qm(4))); inc(ans, mul(c1, c1 - 1, qm(2), calc(l1, l1))); inc(ans, mul(c2, c2 - 1, qm(2), calc(l2, l2))); inc(ans, mul(c1, c2, calc(l1, l2))); ot(ans); return 0; } } // namespace Miracle signed main() { Miracle::main(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, sgn = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn; } const int N = 2e5 + 10; int n, k, mod; long long sum[N], inv[N]; map<int, int> m; map<int, int>::iterator it, it1; long long ans; long long qp(long long x, int t) { long long res = 1; for (; t; t >>= 1, x = x * x % mod) if (t & 1) res = res * x % mod; return res; } void divide(int l, int r, int k) { if (l == r || k <= 1) { m[r - l + 1]++; return; } int mid = l + r >> 1; divide(l, mid, k - 1); divide(mid + 1, r, k - 1); } long long calc(int x, int y) { long long res = 1ll * x * y % mod; for (int i = 1; i <= x; i++) res = (res - 2 * sum[i + y] + 2 * sum[i] + mod) % mod; return (res + mod) % mod; } int main() { n = read(), k = read(), mod = read(); for (int i = 1; i < N; i++) inv[i] = qp(i, mod - 2); for (int i = 1; i < N; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; divide(1, n, k); for (it = m.begin(); it != m.end(); it++) { long long x = it->first, s = it->second; ans = (ans + x * (x - 1) % mod * inv[2] % mod * inv[2] % mod * s % mod) % mod; ans = (ans + s * (s - 1) % mod * inv[2] % mod * inv[2] % mod * calc(x, x) % mod) % mod; } for (it = m.begin(); it != m.end(); it++) for (it1 = m.begin(); it1 != m.end(); it1++) { long long x1 = it->first, s1 = it->second, x2 = it1->first, s2 = it1->second; if (x1 >= x2) continue; ans = (ans + s1 * s2 % mod * inv[2] % mod * calc(x1, x2) % mod) % mod; } printf("%lld\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, mod; long long inv[100009], sum[100009], ans; map<int, int> tag; map<int, int>::iterator it1, it2; long long Read() { long long x = 0; char c = getchar(); bool f = 0; while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } long long Pow(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans; } void Fix(long long &x) { x = x >= mod ? x - mod : x; } long long C(long long n) { return n * (n - 1) / 2 % mod; } void Solve(int l, int r, int k) { if (k == 1 || l == r) { tag[r - l + 1]++; return; } int mid = (l + r) >> 1; Solve(l, mid, k - 1); Solve(mid + 1, r, k - 1); } long long Calc(int a, int b) { long long ans = 1ll * a * b % mod * inv[2] % mod; for (int i = 1; i <= a; ++i) Fix(ans = ans - (sum[i + b] - sum[i]) + mod); return ans; } int main() { n = Read(), k = Read(), mod = Read(); for (int i = 1; i <= n; ++i) inv[i] = Pow(i, mod - 2), Fix(sum[i] = sum[i - 1] + inv[i]); Solve(1, n, k); for (it1 = tag.begin(); it1 != tag.end(); ++it1) { Fix(ans += C(it1->first) * inv[2] % mod * it1->second % mod); Fix(ans += C(it1->second) * Calc(it1->first, it1->first) % mod); } for (it1 = tag.begin(); it1 != tag.end(); ++it1) for (it2 = tag.begin(); it2 != tag.end(); ++it2) { if (it1->first <= it2->first) break; Fix(ans += 1ll * it1->second * it2->second % mod * Calc(it1->first, it2->first) % mod); } printf("%lld\n", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int n, k, mod, inv2; int ans = 0; inline void add(int& a, int b) { a += b; if (a >= mod) a -= mod; if (a < 0) a += mod; } inline int ksm(int a, int b) { int ans = 1; for (; b; b >>= 1, a = (long long)a * a % mod) if (b & 1) ans = (long long)ans * a % mod; return ans; } inline void build(int l, int r, int h) { if (l < r) { if (h <= 1) { int len = r - l + 1; mp[len]++; add(ans, (long long)len * (len - 1) / 2ll % mod * inv2 % mod); } else { int mid = (l + r) >> 1; build(l, mid, h - 1); build(mid + 1, r, h - 1); } } else mp[1]++; } signed main() { cin >> n >> k >> mod; inv2 = ksm(2, mod - 2); build(1, n, k); for (auto i : mp) { for (auto j : mp) { int gs = (long long)i.second * (j.second - (i.first == j.first)) % mod; for (int l = 2; l <= i.first + j.first; ++l) { int minn = max(1, l - j.first); int maxx = min(i.first, l - 1); int tmp = (long long)gs * (maxx - minn + 1) % mod * (inv2 - ksm(l, mod - 2)) % mod * inv2 % mod; add(ans, tmp); } } } cout << ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using LL = long long; template <typename T> T inverse(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U& x) { value = normalize(x); } template <typename U> static Type normalize(const U& x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type& operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); } template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); } Modular& operator++() { return *this += 1; } Modular& operator--() { return *this -= 1; } Modular operator++(int) { Modular result(*this); *this += 1; return result; } Modular operator--(int) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) { long long q = static_cast<long long>(static_cast<long double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(value * rhs.value); return *this; } Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } friend const Type& abs(const Modular& x) { return x.value; } template <typename U> friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); template <typename U> friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); template <typename V, typename U> friend V& operator>>(V& stream, Modular<U>& number); private: Type value; }; template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> power(const Modular<T>& a, const U& b) { assert(b >= 0); Modular<T> x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } template <typename T> bool IsZero(const Modular<T>& number) { return number() == 0; } template <typename T> string to_string(const Modular<T>& number) { return to_string(number()); } template <typename U, typename T> U& operator<<(U& stream, const Modular<T>& number) { return stream << number(); } template <typename U, typename T> U& operator>>(U& stream, Modular<T>& number) { typename common_type<typename Modular<T>::Type, long long>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using ModType = int; struct VarMod { static ModType value; }; ModType VarMod::value; ModType& md = VarMod::value; using Mint = Modular<VarMod>; Mint Solve(int a, int b) { Mint ans = 0, p5 = Mint(1) / 2; for (int i = 2; i <= a + b; ++i) { int mina = max(1, i - b); int maxa = min(a, i - 1); ans += (p5 - Mint(1) / i) * (maxa - mina + 1); } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k >> md; if (n == 1) { cout << 0 << endl; return 0; } map<int, int> s; s[n] = 1; while (--k) { map<int, int> t; for (auto [a, b] : s) { if (a == 1) { t[a] += b; } else { t[a / 2] += b; t[a - a / 2] += b; } } s = std::move(t); if (s.size() == 1 && s.begin()->first == 1) break; } if (s.size() == 1) { int a1 = s.begin()->first, b1 = s.begin()->second; Mint c1 = Solve(a1, a1); cout << Mint(a1) * (a1 - 1) / 4 * b1 + c1 * b1 * (b1 - 1) / 2 << endl; } else { int a1 = s.begin()->first, b1 = s.begin()->second; int a2 = s.rbegin()->first, b2 = s.rbegin()->second; Mint c1 = Solve(a1, a1), c2 = Solve(a2, a2), c3 = Solve(a1, a2); cout << Mint(a1) * (a1 - 1) / 4 * b1 + Mint(a2) * (a2 - 1) / 4 * b2 + c1 * b1 * (b1 - 1) / 2 + c2 * b2 * (b2 - 1) / 2 + c3 * b1 * b2 << endl; } return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n, K, P, ans, cnt[maxn], inv[maxn], pre[maxn]; int qp(int x, int y) { int z = 1; for (; y; y >>= 1, x = 1LL * x * x % P) { if (y & 1) z = 1LL * z * x % P; } return z; } int main() { scanf("%d %d %d", &n, &K, &P); for (int i = 1; i <= n; i++) { inv[i] = qp(i, P - 2); pre[i] = (pre[i - 1] + inv[i]) % P; } int inv2 = (P + 1) >> 1; function<void(int, int, int)> solve = [&](int l, int r, int h) { if (l > r) return; if (h == 1 || l == r) { cnt[r - l + 1]++; ans = (ans + 1LL * (r - l) * (r - l + 1) / 2 % P * inv2) % P; } else { int mid = (l + r) >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } }; solve(1, n, K); auto calc = [&](int n, int m) { int ans = 0; auto solve = [&]() { for (int i = 1; i <= n; i++) { ans = (ans + 1LL * inv2 * i % P * (pre[i + m] - pre[i] + P)) % P; ans = (ans - 1LL * inv2 * (pre[i + m] - pre[i] + P) % P + P) % P; } }; solve(), swap(n, m), solve(); return ans; }; vector<int> V; for (int i = 1; i <= n; i++) { if (cnt[i]) V.push_back(i); } assert(V.size() <= 2); for (int x : V) { ans = (ans + 1LL * cnt[x] * (cnt[x] - 1) / 2 % P * calc(x, x)) % P; } if (V.size() == 2) { ans = (ans + 1LL * cnt[V[0]] * cnt[V[1]] % P * calc(V[0], V[1])) % P; } printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MN = 200000 + 5; using namespace std; template <typename T> inline T& IN(T& in) { in = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) in = in * 10 + c - '0', c = getchar(); return in *= f; } int n, k, P; long long ans; map<int, int> len; long long inv[MN], s[MN]; long long qp(long long a, long long b) { long long c = 1; for (; b; b >>= 1, a = a * a % P) if (b & 1) c = c * a % P; return c; } void build(int l, int r, int k) { if (k == 1 || l == r) return len[r - l + 1]++, void(); int mid = l + r >> 1; build(l, mid, k - 1), build(mid + 1, r, k - 1); } long long calc(long long x, long long y) { long long res = x * y % P * inv[2] % P; for (int i = 1; i <= x; ++i) res = (res - (s[i + y] - s[i]) % P + P) % P; return res; } void input() { IN(n), IN(k), IN(P); int N = 200000; inv[1] = 1, s[1] = 1; for (int i = 2; i <= N; ++i) inv[i] = (P - P / i) * inv[P % i] % P, s[i] = (s[i - 1] + inv[i]) % P; build(1, n, k); for (auto it : len) { long long x = it.first, y = it.second; ans = (ans + x * (x - 1) % P * inv[4] % P * y % P + y * (y - 1) % P * inv[2] % P * calc(x, x) % P) % P; } for (auto x : len) for (auto y : len) if (x.first < y.first) ans = (ans + calc(x.first, y.first) * x.second % P * y.second % P) % P; printf("%lld\n", ans); } int main() { input(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 100000; int n, mxdepth, MOD; int INV2; int inv[2 * MAXN + 1]; vector<int> parts; void rec(int l, int r, int h) { if (l > r) return; if (h == 1 || l == r) { parts.push_back(r - l + 1); return; } int m = (l + r) / 2; rec(l, m, h - 1); rec(m + 1, r, h - 1); } int cntpairs(int sz) { return (long long)sz * (sz - 1) / 2 % MOD; } int calc(int sz) { return (long long)cntpairs(sz) * INV2 % MOD; } int calc(int sza, int szb) { int ret = 0; for (int den = (2); den <= (sza + szb); ++den) { int lo = max(1, den - szb), hi = min(sza, den - 1), cnt = hi - lo + 1; if (lo > hi) continue; int cur = (long long)(den - 2) % MOD * inv[den] % MOD * INV2 % MOD; ret = (ret + (long long)cnt * cur) % MOD; } return ret; } int solve() { INV2 = (MOD + 1) / 2; inv[1] = 1; for (int i = (2); i <= (2 * n); ++i) inv[i] = (long long)(MOD - MOD / i) * inv[MOD % i] % MOD; parts.clear(); rec(1, n, mxdepth); int sz1 = -1, cnt1 = 0, sz2 = -1, cnt2 = 0; for (int i = (0); i < (((int)(parts).size())); ++i) { int x = parts[i]; if (x == sz1) ++cnt1; else if (x == sz2) ++cnt2; else if (sz1 == -1) sz1 = x, ++cnt1; else if (sz2 == -1) sz2 = x, ++cnt2; else assert(false); } int ret = 0; if (cnt1 != 0) ret = (ret + (long long)cnt1 * calc(sz1)) % MOD; if (cnt2 != 0) ret = (ret + (long long)cnt2 * calc(sz2)) % MOD; if (cnt1 != 0) ret = (ret + (long long)cntpairs(cnt1) * calc(sz1, sz1)) % MOD; if (cnt1 != 0 && cnt2 != 0) ret = (ret + (long long)cnt1 * cnt2 % MOD * calc(sz1, sz2)) % MOD; if (cnt2 != 0) ret = (ret + (long long)cntpairs(cnt2) * calc(sz2, sz2)) % MOD; return ret; } void run() { scanf("%d%d%d", &n, &mxdepth, &MOD); printf("%d\n", solve()); } int main() { run(); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long P; long long inv[500005], s[500005]; int n, m; int len[2], cnt[2]; void add(long long &x, long long y) { x += y; if (x >= P) x -= P; if (x < 0) x += P; } void init() { inv[0] = inv[1] = 1; for (int i = 2; i <= n + m; i++) inv[i] = (P - P / i) * inv[P % i] % P; for (int i = 1; i <= n + m; i++) s[i] = (s[i - 1] + inv[i]) % P; } long long calc(long long l1, long long l2) { long long ans = l1 * l2 % P * inv[2] % P; for (int i = 1; i <= l1; i++) add(ans, P - (s[i + l2] - s[i]) % P); return ans; } long long sum(long long x) { return x * (x - 1) / 2 % P; } void dfs(int l, int r, int h) { if (h <= 1 || l == r) { if (!len[0] || r - l + 1 == len[0]) len[0] = r - l + 1, cnt[0]++; else if (!len[1] || r - l + 1 == len[1]) len[1] = r - l + 1, cnt[1]++; return; } int mid = (l + r) >> 1; dfs(l, mid, h - 1), dfs(mid + 1, r, h - 1); } int main() { cin >> n >> m >> P; init(); dfs(1, n, m); long long ans = 0; for (int i = 0; i <= 1; i++) { add(ans, sum(len[i]) * inv[2] % P * cnt[i] % P); add(ans, sum(cnt[i]) * calc(len[i], len[i]) % P); } add(ans, cnt[0] * cnt[1] % P * calc(len[0], len[1]) % P); cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, K, P, I, ans, inv[N], H[N], len[2], cnt[2]; int ad(int k1, int k2) { return k1 += k2 - P, k1 += k1 >> 31 & P; } int su(int k1, int k2) { return k1 -= k2, k1 += k1 >> 31 & P; } int mu(int k1, int k2) { return 1LL * k1 * k2 % P; } int po(int k1, int k2) { int k3 = 1; for (; k2; k2 >>= 1, k1 = mu(k1, k1)) if (k2 & 1) k3 = mu(k3, k1); return k3; } void sol(int k1, int k2, int k3) { if (k1 >= K || k2 == k3) { int x = 0; if (len[x] && len[x] != k3 - k2 + 1) ++x; len[x] = k3 - k2 + 1; ++cnt[x]; ans = ad(ans, mu(mu(k3 - k2 + 1, k3 - k2 + 1 - 1), mu(I, I))); return; } int mid = (k2 + k3) >> 1; sol(k1 + 1, k2, mid); sol(k1 + 1, mid + 1, k3); } int calc(int x, int y) { if (!x || !y) return 0; int res = mu(mu(x, y), I); for (int i = (1); i <= (x); ++i) { res = su(res, su(H[i + y], H[i])); } return res; } int main() { scanf("%d%d%d", &n, &K, &P); I = po(2, P - 2); inv[0] = inv[1] = 1; for (int i = (2); i <= (N - 1); ++i) inv[i] = mu(P - P / i, inv[P % i]); for (int i = (1); i <= (N - 1); ++i) H[i] = ad(H[i - 1], inv[i]); sol(1, 1, n); auto C2 = [&](int x) { return mu(mu(x, x - 1), I); }; ans = ad(ans, mu(C2(cnt[0]), calc(len[0], len[0]))); ans = ad(ans, mu(C2(cnt[1]), calc(len[1], len[1]))); ans = ad(ans, mu(mu(cnt[0], cnt[1]), calc(len[0], len[1]))); printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> int n, m, t, x, y, mod, ans, v[200010]; std::map<int, int> map; void solve(int l, int r, int k) { if (l == r || k <= 1) { map[r - l + 1]++; return; } solve(l, l + r >> 1, k - 1), solve((l + r >> 1) + 1, r, k - 1); } int fpow(long long a, int b) { long long s = 1; for (; b; b >>= 1, a = a * a % mod) if (b & 1) s = s * a % mod; return s; } long long f(long long x) { return (long long)x * (x - 1) % mod * fpow(4, mod - 2) % mod; } long long f(long long x, long long y) { long long res = (long long)x * y % mod * fpow(2, mod - 2) % mod; for (int i = 1; i <= x; i++) res = (res - v[i + y] + v[i]); return (res % mod + mod) % mod; } int main() { scanf("%d%d%d", &n, &m, &mod), solve(1, n, m); for (int i = 0; i <= n + m; i++) v[i] = (v[i - 1] + fpow(i, mod - 2)) % mod; t = map.begin()->first, x = map[t], y = map[t + 1]; printf("%d\n", (x * f(t) + y * f(t + 1) + x * (x - 1) / 2 % mod * f(t, t) + x * y % mod * f(t, t + 1) + y * (y - 1) / 2 % mod * f(t + 1, t + 1)) % mod); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int cnt[maxn], mod; int Pow(int x, int p) { int r = 1; while (p) { if (p & 1) r = (long long)x * r % mod; p >>= 1; x = (long long)x * x % mod; } return r; } void dfs(int n, int k) { if (n == 1 || k == 1) { cnt[n]++; return; } dfs(n / 2, k - 1); dfs((n + 1) / 2, k - 1); } int calc(int x, int y) { int res = 0; for (int i = 2; i <= x + y; ++i) res = (res + (long long)min(x + y - i + 1, i - 1) * (i - 2) % mod * Pow(2 * i, mod - 2) % mod) % mod; return res; } int main() { int n, k; scanf("%d %d", &n, &k); scanf("%d", &mod); dfs(n, k); int ans = 0; for (int i = 1; i <= n; ++i) if (cnt[i]) { ans = (ans + (long long)cnt[i] * i % mod * (i - 1) % mod * Pow(4, mod - 2) % mod) % mod; ans = (ans + (long long)cnt[i] * (cnt[i] - 1) / 2 % mod * calc(i, i)) % mod; } for (int i = 1; i <= n; ++i) if (cnt[i]) for (int j = i + 1; j <= n; ++j) if (cnt[j]) ans = (ans + (long long)cnt[i] * cnt[j] % mod * calc(i, j)) % mod; cout << ans << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, mod; long long inv[100009], sum[100009], ans; map<int, int> tong; map<int, int>::iterator it1, it2; inline long long rd() { long long x = 0; char c = getchar(); bool f = 0; while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f ? -x : x; } inline long long power(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans; } inline void MOD(long long &x) { x = x >= mod ? x - mod : x; } inline long long C(long long n) { return n * (n - 1) / 2 % mod; } inline void solve(int l, int r, int k) { if (k == 1 || l == r) { tong[r - l + 1]++; return; } int mid = (l + r) >> 1; solve(l, mid, k - 1); solve(mid + 1, r, k - 1); } inline long long calc(int a, int b) { long long ans = 1ll * a * b % mod * inv[2] % mod; for (int i = 1; i <= a; ++i) MOD(ans = ans - (sum[i + b] - sum[i]) + mod); return ans; } int main() { n = rd(); k = rd(); mod = rd(); for (int i = 1; i <= n; ++i) inv[i] = power(i, mod - 2), MOD(sum[i] = sum[i - 1] + inv[i]); solve(1, n, k); for (it1 = tong.begin(); it1 != tong.end(); ++it1) { MOD(ans += C(it1->first) * inv[2] % mod * it1->second % mod); MOD(ans += C(it1->second) * calc(it1->first, it1->first) % mod); } for (it1 = tong.begin(); it1 != tong.end(); ++it1) for (it2 = tong.begin(); it2 != tong.end(); ++it2) { if (it1->first <= it2->first) break; MOD(ans += 1ll * it1->second * it2->second % mod * calc(it1->first, it2->first) % mod); } cout << ans; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAXN = 1e5 + 20; int n, k, M; int inv[MAXN], pre_inv[MAXN]; void math_pre() { inv[1] = 1; for (int i = 2; i <= ((n < 4) ? 4 : n); ++i) inv[i] = 1ll * (M - M / i) * inv[M % i] % M; for (int i = 1; i <= n; ++i) pre_inv[i] = (pre_inv[i - 1] + inv[i]) % M; } struct map { static const int MAXMap = 2; int tot; struct pad { int key, val; pad() {} pad(const int &KEY, const int &VAL) : key(KEY), val(VAL) {} } node[MAXMap + 1]; map() { tot = 0; } pad *find(const int &key) { pad *ret = node; while (ret - node < tot && ret->key != key) ++ret; return ret; } void insert(const pad &new_element) { node[tot++] = new_element; } pad *begin() { return &node[0]; } pad *end() { return &node[tot]; } } Map; void solve(const int &l, const int &r, const int &h) { if (l >= r || h <= 1) { int len = r - l + 1; map::pad *it = Map.find(len); if (it == Map.end()) Map.insert(map::pad(len, 1)); else ++it->val; return; } int mid = (l + r) >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } int calc(const int &len1, const int &len2) { int ret = 0; for (int i = 1; i <= len1; ++i) ret = ((ret + 1ll * inv[2] * len2 % M - (pre_inv[i + len2] - pre_inv[i + 1 - 1])) % M + M) % M; return ret; } int main() { scanf("%d%d%d", &n, &k, &M); math_pre(); solve(1, n, k); int ans = 0; for (map::pad *it = Map.begin(); it != Map.end(); ++it) { int len = it->key, cnt = it->val; ans = (ans + 1ll * cnt * len % M * (len - 1) % M * inv[4] % M) % M; } for (map::pad *it1 = Map.begin(); it1 != Map.end(); ++it1) for (map::pad *it2 = Map.begin(); it2 != Map.end(); ++it2) { if (it1 == it2) { int len = it1->key, cnt = 1ll * (0 + (it1->val - 1)) * it1->val / 2 % M; ans = (ans + 1ll * cnt * calc(len, len) % M) % M; } else if (it1->key < it2->key) { int len1 = it1->key, len2 = it2->key, cnt = 1ll * it1->val * it2->val % M; ans = (ans + 1ll * cnt * calc(len1, len2) % M) % M; } } printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using std::cerr; using std::cout; int mod; inline int add(int a, int b) { a += b - mod; return a + (a >> 31 & mod); } inline int dec(int a, int b) { a -= b; return a + (a >> 31 & mod); } inline int mul(int a, int b) { long long r = (long long)a * b; return r >= mod ? r % mod : r; } inline void Inc(int &a, int b) { a += b - mod; a += a >> 31 & mod; } const int N = 1e5 + 7; int n, k; int inv[N], H[N]; std::map<int, int> cnt; inline void solve(int l, int r, int d) { if (d == 1 || l == r) { cnt[r - l + 1]++; return; } int mid = l + r >> 1; solve(l, mid, d - 1); solve(mid + 1, r, d - 1); } inline int calc(int x, int y) { int ans = mul(x, y); ans = mul(ans, mod + 1 >> 1); for (int register i = 1; i <= x; ++i) Inc(ans, dec(H[i], H[i + y])); return ans; } signed main() { scanf("%d%d%d", &n, &k, &mod); int iv2 = mod + 1 >> 1, iv4 = mul(iv2, iv2); inv[0] = inv[1] = H[0] = H[1] = 1; for (int register i = 2; i <= n; ++i) H[i] = add(H[i - 1], inv[i] = mul(mod - mod / i, inv[mod % i])); solve(1, n, k); int ans = 0; for (auto t : cnt) { Inc(ans, mul(mul(t.first, t.first - 1), mul(iv4, t.second))); Inc(ans, mul(mul(t.second, t.second - 1), mul(iv2, calc(t.first, t.first)))); } for (auto i1 : cnt) for (auto i2 : cnt) if (i1.first < i2.first) { Inc(ans, mul(calc(i1.first, i2.first), mul(i1.second, i2.second))); } cout << ans << "\n"; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; int q; inline int add(int x, int y) { x += y; return x >= q ? x - q : x; } inline int sub(int x, int y) { x -= y; return x < 0 ? x + q : x; } inline int mul(int x, int y) { return (int)(1LL * x * y % q); } int mpow(int b, int p) { int ret; for (ret = 1; p; p >>= 1, b = mul(b, b)) if (p & 1) ret = mul(ret, b); return ret; } int a[MAXN + 5]; void get(int l, int r, int h) { if (l == r || h == 1) a[r - l + 1]++; else { int m = (l + r) >> 1; get(l, m, h - 1), get(m + 1, r, h - 1); } } int inv[MAXN + 5], fct[MAXN + 5], ifct[MAXN + 5]; int comb(int n, int m) { if (n < m || m < 0) return 0; else return mul(fct[n], mul(ifct[m], ifct[n - m])); } int si[MAXN + 5]; void init() { inv[1] = 1; for (int i = 2; i <= MAXN; i++) inv[i] = sub(0, mul(q / i, inv[q % i])); fct[0] = 1; for (int i = 1; i <= MAXN; i++) fct[i] = mul(fct[i - 1], i); ifct[MAXN] = mpow(fct[MAXN], q - 2); for (int i = MAXN - 1; i >= 0; i--) ifct[i] = mul(ifct[i + 1], i + 1); for (int i = 1; i <= MAXN; i++) si[i] = add(si[i - 1], inv[i]); } int b[MAXN + 5], cnt; int main() { int n, k; scanf("%d%d%d", &n, &k, &q), get(1, n, k), init(); for (int i = 1; i <= n; i++) if (a[i]) b[++cnt] = i; int ans = 0; for (int i = 1; i <= cnt; i++) ans = add(ans, mul(mul(mul(mul(b[i], b[i] - 1), inv[2]), inv[2]), a[b[i]])); for (int o1 = 1; o1 <= cnt; o1++) for (int o2 = 1; o2 <= cnt; o2++) { int coef = mul(a[b[o1]], o1 == o2 ? a[b[o2]] - 1 : a[b[o2]]), del = 0; if (coef == 0) continue; for (int i = 1; i <= b[o1]; i++) { int k = add(mul(inv[2], i - 1), 1), c = mul(inv[i + 1], b[o2]), d = sub(si[i + b[o2]], si[i]); del = add(del, mul(k, sub(c, d))); } ans = add(ans, mul(del, coef)); } printf("%d\n", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void minn(T &a, T b) { a = min(a, b); } template <class T> void maxx(T &a, T b) { a = max(a, b); } void io() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const long long MOD = 1000000007LL; const long long PRIME = 105943LL; const long long INF = 1e18; long long mod; inline long long add(long long a, long long b) { return (a + b) % mod; } inline long long mul(long long a, long long b) { return (1LL * a * b) % mod; } inline long long pow(long long a, long long p) { long long ret = 1LL; while (p) { if (p & 1LL) ret = mul(ret, a); a = mul(a, a), p >>= 1LL; } return ret; } inline long long inv(long long x) { return pow(x, mod - 2); } void go(int l, int r, int h, map<int, int> &cnt) { if (l <= r) if (h <= 1 || l == r) cnt[r - l + 1]++; else { int m = (l + r) / 2; go(l, m, h - 1, cnt); go(m + 1, r, h - 1, cnt); } } long long solve(int x) { return mul(x, mul(x - 1, inv(4))); } long long solve(int x, int y) { long long ret = 0; for (int sz = 2; sz <= (int)x + y; sz++) ret = add(ret, mul(mul(sz - 2, min(x, sz - 1) - max(1, sz - y) + 1), mul(inv(2), inv(sz)))); return ret; } int main() { io(); int n, k; cin >> n >> k >> mod; map<int, int> cnt; go(1, n, k, cnt); assert(cnt.size() < 3); int s = cnt.size(); vector<long long> len, num; for (auto en : cnt) len.push_back(en.first), num.push_back(en.second); long long ans = 0; for (int i = 0; i < (int)(s); i++) { long long temp = mul(num[i], solve(len[i])); ans = add(ans, temp); } for (int i = 0; i < (int)(s); i++) { long long temp = mul(num[i] * (num[i] - 1) / 2, solve(len[i], len[i])); ans = add(ans, temp); } for (int i = 0; i < (int)(s); i++) for (int j = i + 1; j < (int)(s); j++) { long long temp = mul(mul(num[i], num[j]), solve(len[i], len[j])); ans = add(ans, temp); } cout << ans << "\n"; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m, mod, inv[300000], ans, cnt[300000], pre[300000]; void upd(long long &x, long long y) { x = (x + y) % mod; } void solve(long long x, long long m) { if (!m || x == 1) { ++cnt[x]; return; } solve((x + 1) / 2, m - 1); solve(x / 2, m - 1); } int main() { scanf("%lld%lld%lld", &n, &m, &mod); m = min(m - 1, 20LL); solve(n, m); long long p = max(n, 4LL); inv[1] = 1; for (long long i = 2; i <= p; ++i) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (long long i = 1; i <= p; ++i) pre[i] = (pre[i - 1] + inv[i]) % mod; for (long long i : {n >> m, (n >> m) + 1}) { upd(ans, cnt[i] * i % mod * (i - 1) % mod * inv[4]); for (long long j : {n >> m, (n >> m) + 1}) { long long sum = 0; for (long long k = 1; k <= i; ++k) { upd(sum, (k - 1) * (pre[k + j] - pre[k])); } sum = sum * inv[2] % mod; upd(ans, sum * cnt[i] % mod * (i == j ? cnt[i] - 1 : cnt[j])); } } upd(ans, mod); printf("%lld\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch = getchar(); long long s = 0, w = 1; while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * w; } inline int lowbit(int x) { return x & (-x); } int mod, n, h; int k[2], m[2]; int ans, inv2; inline int Z(int x) { return (x >= mod ? x - mod : x); } inline int C2(int n) { return 1LL * n * (n - 1) % mod * inv2 % mod; } inline int ksm(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = 1LL * ans * a % mod; b >>= 1; a = 1LL * a * a % mod; } return ans; } void Solve(int l, int r, int h) { if (h == 1 || l == r) { if (!k[0]) { k[0] = r - l + 1; m[0]++; } else if (k[0] == r - l + 1) m[0]++; else k[1] = r - l + 1, m[1]++; ans = Z(ans + 1LL * inv2 * C2(r - l + 1) % mod); return; } Solve(l, ((l + r) >> 1), h - 1); Solve(((l + r) >> 1) + 1, r, h - 1); } inline int calc(int n, int m) { int s = 1LL * inv2 * n % mod * m % mod; int l = 1, r = 0; for (register int S = 2; S <= n + m; S++) { while (l + m < S) l++; while (r < S - 1 && r < n) r++; if (l > r) break; s = Z(s + mod - 1LL * ksm(S, mod - 2) * (r - l + 1) % mod); } return s; } int main() { n = read(), h = read(), mod = read(); inv2 = ksm(2, mod - 2); Solve(1, n, h); if (!k[1]) { ans = Z(ans + 1LL * C2(m[0]) * calc(k[0], k[0]) % mod); } else { ans = Z(ans + 1LL * C2(m[0]) * calc(k[0], k[0]) % mod); ans = Z(ans + 1LL * C2(m[1]) * calc(k[1], k[1]) % mod); ans = Z(ans + 1LL * m[0] * m[1] % mod * calc(k[0], k[1]) % mod); } cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } x *= f; } template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; } template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; } inline unsigned int R() { static unsigned int seed = 416; return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13; } const int N = 233333; int n, k, mo, s[N], len; long long res; void solve(int l, int r, int h) { if (l > r) return; if (h <= 1 || l == r) { s[++len] = r - l + 1; return; } int mid = (l + r) >> 1; solve(l, mid, h - 1); solve(mid + 1, r, h - 1); } inline int power(int a, int n) { int res = 1; while (n) { if (n & 1) res = 1LL * res * a % mo; a = 1LL * a * a % mo; n >>= 1; } return res; } long long solve(int n, int m) { long long res = 1LL * n * m % mo * power(2, mo - 2) % mo; for (register int c = (1); c <= (n + m); c++) { int l = max(1, c - m), r = min(n, c - 1); if (r - l + 1 >= 1) res = (res - 1LL * (r - l + 1) * power(c, mo - 2)) % mo; } return res; } int main() { read(n); read(k); read(mo); solve(1, n, k); sort(s + 1, s + len + 1); for (register int i = (1); i <= (len); i++) res += 1LL * s[i] * (s[i] - 1) % mo * power(4, mo - 2) % mo; static pair<int, int> a[N]; int tot = 0; for (register int i = (1); i <= (len); i++) if (a[tot].first == s[i]) a[tot].second++; else a[++tot] = pair<int, int>(s[i], 1); assert(tot <= 2); for (register int i = (1); i <= (tot); i++) res += 1LL * a[i].second * (a[i].second - 1) / 2 % mo * solve(a[i].first, a[i].first) % mo; if (tot == 2) res += 1LL * a[1].second * a[2].second % mo * solve(a[1].first, a[2].first) % mo; printf("%lld", (res % mo + mo) % mo); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, mod, ans; int inv[N], sum[N]; map<int, int> cnt; map<int, int>::iterator it1, it2; int read() { int ret = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 0; c = getchar(); } while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar(); return f ? ret : -ret; } void up(int &x, int y) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; } int qpow(int x, int y) { int res = 1; x %= mod; for (; y; y >>= 1, x = (long long)x * x % mod) if (y & 1) res = (long long)res * x % mod; return res; } void init() { n = read(); k = read(); mod = read(); for (int i = 1; i < N; ++i) sum[i] = inv[i] = qpow(i, mod - 2), up(sum[i], sum[i - 1]); } void divide(int l, int r, int dp) { if (dp <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; divide(l, mid, dp - 1); divide(mid + 1, r, dp - 1); } int calc(int x, int y) { int res = (long long)x * y % mod; for (int i = 1; i <= x; ++i) up(res, -(sum[i + y] - sum[i]) * 2 % mod); return res; } void solve() { for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) { int t = it1->first, s = it1->second; up(ans, (long long)t * (t - 1) % mod * inv[2] % mod * s % mod); up(ans, (long long)s * (s - 1) % mod * inv[2] % mod * calc(t, t) % mod); } for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) for (it2 = cnt.begin(); it2 != cnt.end(); ++it2) { int x = it1->first, y = it2->first; if (x >= y) continue; up(ans, (long long)calc(x, y) * it1->second % mod * it2->second % mod); } printf("%d\n", (long long)ans * inv[2] % mod); } int main() { init(); divide(1, n, k); solve(); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, k, p, cnt[N]; long long ifac[N]; inline void solve(int l, int r, int t) { if (t == 1 || l == r) { cnt[r - l + 1]++; return; } int d = (l + r) >> 1; solve(l, d, t - 1), solve(d + 1, r, t - 1); } inline long long getans(int a, int b) { long long sum = (p + 1) / 2, ans = sum * a % p * b % p; for (int i = 1; i <= a; i++) ans = (ans - ifac[i + b] + ifac[i]) % p; return (ans + p) % p; } int main() { cin >> n >> k >> p, ifac[0] = ifac[1] = 1; for (int i = 2; i <= n; i++) ifac[i] = ifac[p % i] * (p - p / i) % p; for (int i = 1; i <= n; i++) ifac[i] = (ifac[i] + ifac[i - 1]) % p; solve(1, n, k); int a = 0, b = 0; for (int i = 1; i <= n; i++) if (cnt[i] && !a) a = i; else if (cnt[i]) b = i; long long s = getans(a, a) * ((1ll * cnt[a] * (cnt[a] - 1) / 2) % p) % p; s = (s + getans(b, b) * ((1ll * cnt[b] * (cnt[b] - 1) / 2) % p)) % p; s = (s + getans(a, b) * cnt[a] % p * cnt[b] % p) % p; s = (s + (1ll * a * (a - 1) / 2 * cnt[a] % p + 1ll * b * (b - 1) / 2 * cnt[b] % p) % p * ((p + 1) / 2)) % p; cout << s << endl; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int md; inline void add(int &a, int b) { a += b; if (a >= md) a -= md; } inline void sub(int &a, int b) { a -= b; if (a < 0) a += md; } inline int mul(int a, int b) { return (int)((long long)a * b % md); } inline int power(int a, long long b) { int res = 1; while (b > 0) { if (b & 1) { res = mul(res, a); } a = mul(a, a); b >>= 1; } return res; } inline int inv(int a) { a %= md; if (a < 0) a += md; int b = md, u = 0, v = 1; while (a) { int t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += md; return u; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k >> md; if (k >= 20 || n <= (1 << (k - 1))) { cout << 0 << '\n'; return 0; } int bc = (1 << (k - 1)); int small_size = n / bc; int big_size = small_size + 1; int big_cnt = n % bc; int small_cnt = bc - big_cnt; vector<int> blocks(bc); for (int i = 0; i < n; i++) { blocks[i % (int)blocks.size()]++; } map<int, int> mp; for (int x : blocks) { mp[x]++; } vector<int> fact(n + 1), inv_fact(n + 1); fact[0] = inv_fact[0] = 1; for (int i = 1; i <= n; i++) { fact[i] = mul(fact[i - 1], i); inv_fact[i] = inv(fact[i]); } int ans = 0; for (int b1id = 0; b1id < bc; b1id++) { int b = blocks[b1id]; add(ans, mul(mul(b, b - 1), inv(4))); } vector<int> sum_inv(n + 1); for (int i = 0; i < n; i++) { sum_inv[i + 1] = sum_inv[i]; add(sum_inv[i + 1], inv(i + 1)); } for (int b1id = 0; b1id < bc; b1id++) { int b1 = blocks[b1id]; if (b1 == small_size) small_cnt--; else big_cnt--; int salt; for (int x = 2; x <= b1; x++) { if (small_cnt > 0) { int aux = sum_inv[x + small_size]; sub(aux, sum_inv[x]); int prob = mul(x - 1, aux); add(ans, mul(small_cnt, mul(prob, inv(2)))); } if (big_cnt > 0) { int aux = sum_inv[x + big_size]; sub(aux, sum_inv[x]); int prob = mul(x - 1, aux); add(ans, mul(big_cnt, mul(prob, inv(2)))); } } if (b1 == small_size) small_cnt++; else big_cnt++; } cout << ans << '\n'; return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200100; int mod, n, h; inline void Add(int &a, int b) { a = a + b >= mod ? a + b - mod : a + b; } int inv[maxn], sum[maxn]; inline int Qsum(int l, int r) { return (sum[r] - sum[l - 1] + mod) % mod; } int cnt[maxn], S, L; int ans; inline void getblock(int l, int r, int dep) { if (dep >= h || l == r) { int size = r - l + 1; cnt[size]++; if (L == 0) L = size; else if (L != size) S = size; if (S > L) swap(S, L); Add(ans, 1ll * size * (size - 1) % mod * inv[4] % mod); return; } int mid = (l + r) >> 1; getblock(l, mid, dep + 1); getblock(mid + 1, r, dep + 1); } inline int getans(int size1, int size2) { int ans = 1ll * size1 * size2 * inv[2] % mod; for (int i = 1; i <= size1; i++) Add(ans, mod - Qsum(i + 1, i + size2)); return ans; } int main() { scanf("%d%d%d", &n, &h, &mod); inv[1] = 1; for (int i = 2; i < maxn; i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i < maxn; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; getblock(1, n, 1); if (S == 0) S = L, L = 0; Add(ans, 1ll * getans(S, S) * cnt[S] % mod * (cnt[S] - 1) % mod * inv[2] % mod); if (L) Add(ans, 1ll * getans(L, L) * cnt[L] % mod * (cnt[L] - 1) % mod * inv[2] % mod); if (L) Add(ans, 1ll * getans(S, L) * cnt[S] % mod * cnt[L] % mod); printf("%d", ans); }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, mod, num[200005]; void Add(int &a, int b) { ((a += b) >= mod) && (a -= mod); } int ksm(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ans; } void dfs(int dep, int l, int r) { if (dep == 1) { ++num[r - l + 1]; return; } if (l == r) { ++num[1]; return; } int mid = l + r >> 1; dfs(dep - 1, l, mid); dfs(dep - 1, mid + 1, r); } int ans, sm[200005], inv[200005], ny[200005]; int main() { scanf("%d%d%d", &n, &k, &mod); n <<= 1; sm[0] = ny[0] = 1; for (int i = 1; i <= n; ++i) sm[i] = 1ll * sm[i - 1] * i % mod; inv[n] = ksm(sm[n], mod - 2); for (int i = n - 1; i >= 0; --i) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; for (int i = 1; i <= n; ++i) ny[i] = 1ll * inv[i] * sm[i - 1] % mod; n >>= 1; dfs(k, 1, n); for (int i = 1; i <= n; ++i) { if (num[i]) { Add(ans, 1ll * i * (i - 1) % mod * ksm(4, mod - 2) % mod * num[i] % mod); } } for (int i = 1; i <= n; ++i) { if (num[i]) for (int j = i; j <= n; ++j) { if (num[j]) { for (int k = 2; k <= i + j; ++k) { if (i == j) Add(ans, 1ll * (k - 2) * ny[2 * k] % mod * min(k - 1, min(k, i) - max(k - j, 1) + 1) % mod * (1ll * num[i] * (num[i] - 1) % mod * ny[2] % mod) % mod); else Add(ans, 1ll * (k - 2) * ny[2 * k] % mod * min(k - 1, min(k, i) - max(k - j, 1) + 1) % mod * num[i] % mod * num[j] % mod); } } } } printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &x, T y) { return (y < x) ? (x = y, 1) : 0; } template <typename T> inline bool chkmax(T &x, T y) { return (y > x) ? (x = y, 1) : 0; } inline int read() { int x; char c; int f = 1; while ((c = getchar()) != '-' && (c > '9' || c < '0')) ; if (c == '-') f = -1, c = getchar(); x = c ^ '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ '0'); return x * f; } inline long long readll() { long long x; char c; int f = 1; while ((c = getchar()) != '-' && (c > '9' || c < '0')) ; if (c == '-') f = -1, c = getchar(); x = c ^ '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1ll) + (x << 3ll) + (c ^ '0'); return x * f; } int n, m, mod, ans; int ksm(int x, int y) { int res = 1; while (y) { if (y & 1) res = (long long)res * x % mod; x = (long long)x * x % mod; y >>= 1; } return res; } int main() { n = read(), m = read() - 1, mod = read(); for (register int i = 1, iend = m; i <= iend; ++i) if (n / (1 << i) == 0) return printf("0\n"), 0; int u = n / (1 << m), v = u + 1; int t2 = n - ((n / (1 << m)) << m), t1 = (1 << m) - t2; ans = (ans + (long long)u * (u - 1) / 2 % mod * (mod + 1) / 2 % mod * t1) % mod; ans = (ans + (long long)v * (v - 1) / 2 % mod * (mod + 1) / 2 % mod * t2) % mod; for (register int i = 2, iend = u + v; i <= iend; ++i) ans = (ans + (long long)t1 * t2 % mod * (min(i - 1, u) - max(1, i - v) + 1) % mod * (i - 2) % mod * ksm(i * 2, mod - 2)) % mod; for (register int i = 2, iend = u * 2; i <= iend; ++i) ans = (ans + (long long)t1 * (t1 - 1) / 2 % mod * (min(i - 1, u) - max(1, i - u) + 1) % mod * (i - 2) % mod * ksm(i * 2, mod - 2)) % mod; t1 = t2, u = v; for (register int i = 2, iend = u * 2; i <= iend; ++i) ans = (ans + (long long)t1 * (t1 - 1) / 2 % mod * (min(i - 1, u) - max(1, i - u) + 1) % mod * (i - 2) % mod * ksm(i * 2, mod - 2)) % mod; printf("%d\n", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class S, class T> ostream& operator<<(ostream& o, const pair<S, T>& p) { return o << "(" << p.first << "," << p.second << ")"; } template <class T> ostream& operator<<(ostream& o, const vector<T>& vc) { o << "{"; for (const T& v : vc) o << v << ","; o << "}"; return o; } using ll = long long; template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } unsigned int mod = 1; struct ModInt { using uint = unsigned int; using ll = long long; using ull = unsigned long long; uint v; ModInt() : v(0) {} ModInt(ll _v) : v(normS(_v % mod + mod)) {} explicit operator bool() const { return v != 0; } static uint normS(const uint& x) { return (x < mod) ? x : x - mod; } static ModInt make(const uint& x) { ModInt m; m.v = x; return m; } ModInt operator+(const ModInt& b) const { return make(normS(v + b.v)); } ModInt operator-(const ModInt& b) const { return make(normS(v + mod - b.v)); } ModInt operator-() const { return make(normS(mod - v)); } ModInt operator*(const ModInt& b) const { return make((ull)v * b.v % mod); } ModInt operator/(const ModInt& b) const { return *this * b.inv(); } ModInt& operator+=(const ModInt& b) { return *this = *this + b; } ModInt& operator-=(const ModInt& b) { return *this = *this - b; } ModInt& operator*=(const ModInt& b) { return *this = *this * b; } ModInt& operator/=(const ModInt& b) { return *this = *this / b; } ModInt& operator++(int) { return *this = *this + 1; } ModInt& operator--(int) { return *this = *this - 1; } ll extgcd(ll a, ll b, ll& x, ll& y) const { ll p[] = {a, 1, 0}, q[] = {b, 0, 1}; while (*q) { ll t = *p / *q; for (int i = 0; i < (int)(3); i++) swap(p[i] -= t * q[i], q[i]); } if (p[0] < 0) for (int i = 0; i < (int)(3); i++) p[i] = -p[i]; x = p[1], y = p[2]; return p[0]; } ModInt inv() const { ll x, y; extgcd(v, mod, x, y); return make(normS(x + mod)); } ModInt pow(ll p) const { if (p < 0) return inv().pow(-p); ModInt a = 1; ModInt x = *this; while (p) { if (p & 1) a *= x; x *= x; p >>= 1; } return a; } bool operator==(const ModInt& b) const { return v == b.v; } bool operator!=(const ModInt& b) const { return v != b.v; } friend istream& operator>>(istream& o, ModInt& x) { ll tmp; o >> tmp; x = ModInt(tmp); return o; } friend ostream& operator<<(ostream& o, const ModInt& x) { return o << x.v; } }; using mint = ModInt; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int N, K; cin >> N >> K >> mod; K--; V<int> s = {N}; for (int i = 0; i < (int)(K); i++) { if ((int)s.size() == N) break; V<int> ns; for (int v : s) { ns.push_back((v + 1) / 2); if (v / 2 != 0) ns.push_back(v / 2); } s = ns; } true; sort(s.begin(), s.end()); V<int> v, n; { int c = 0; for (int x : s) if (s[0] == x) c++; v.push_back(s[0]); n.push_back(c); if (c != (int)s.size()) { v.push_back(s.back()); n.push_back((int)s.size() - c); } } mint res = 0; for (int v : s) res += mint(v) * (v - 1) / 4; for (int i = 0; i < (int)(v.size()); i++) for (int j = 0; j < (int)(i + 1); j++) { mint tmp = mint(v[i]) * v[j] / 2; for (int x = 2; x <= v[i] + v[j]; x++) { mint num = x - 1 - max(x - 1 - v[i], 0) - max(x - 1 - v[j], 0); tmp -= num / x; } res += tmp * (i == j ? n[i] * (n[i] - 1) / 2 : n[i] * n[j]); } cout << res << endl; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 0; inline long long pls(long long a, long long b) { return a + b < mod ? a + b : a + b - mod; } inline long long dec(long long a, long long b) { return a >= b ? a - b : a - b + mod; } int len1 = 0, len2 = 0, c1 = 0, c2 = 0; void dfs(int L, int R, int h) { if (L > R) return; if (h <= 1 || L == R) { int len = R - L + 1; if (len1 == 0) { len1 = len; c1 = 1; } else if (len1 == len) ++c1; else if (len2 == 0) { len2 = len; c2 = 1; } else ++c2; return; } int mid = (L + R) >> 1; dfs(L, mid, h - 1); dfs(mid + 1, R, h - 1); } long long inv[100003], sum[100003]; void pre() { inv[1] = inv[0] = 1; for (int i = 2; i <= 100000; ++i) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= 100000; ++i) sum[i] = pls(sum[i - 1], inv[i]); } long long calc(int A, int B) { if (A == 0 || B == 0) return 0; long long ret = (long long)A * B % mod * inv[2] % mod; for (int i = 1; i <= A; ++i) ret = dec(ret, dec(sum[i + B], sum[i])); return ret; } int main() { int n = 0, k = 0; scanf("%d %d %lld", &n, &k, &mod); dfs(1, n, k); pre(); long long ans = pls((long long)len1 * (len1 - 1ll) % mod * inv[4] % mod * c1 % mod, (long long)len2 * (len2 - 1ll) % mod * inv[4] % mod * c2 % mod); ans = pls(ans, calc(len1, len1) * c1 % mod * (c1 - 1ll) % mod * inv[2] % mod); ans = pls(ans, calc(len2, len2) * c2 % mod * (c2 - 1ll) % mod * inv[2] % mod); ans = pls(ans, calc(len1, len2) * c1 % mod * c2 % mod); printf("%lld", ans); return 0; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> int chkmax(T& a, T b) { if (b > a) { a = b; return 1; } return 0; } template <class T> int chkmin(T& a, T b) { if (b < a) { a = b; return 1; } return 0; } template <class iterator> void output(iterator begin, iterator end, ostream& out = cerr) { while (begin != end) { out << (*begin) << " "; begin++; } out << '\n'; } template <class T> void output(T x, ostream& out = cerr) { output(x.begin(), x.end(), out); } void fast_io() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n, k; long long MOD; long long power(long long a, long long deg) { long long res = 1; while (deg) { if ((deg & 1LL) == 0) { a = (a * a) % MOD; deg >>= 1; } else { res = (res * a) % MOD; deg -= 1; } } return res; } long long inv(long long a) { return power(a, MOD - 2); } long long calc(int len1, int len2) { long long res = 0; for (int i = 2; i <= len1 + len2; ++i) { long long cnt = (long long)(min(len1, i - 1) - max(1, i - len2) + 1); res = (res + cnt * inv(i)) % MOD; } long long p_len = ((long long)len1 * (long long)len2) % MOD; res = ((p_len * inv(2) - res) % MOD + MOD) % MOD; return res; } long long calc(int _len) { long long len = (long long)(_len); long long x = (len * (len - 1)) % MOD; x = (x * inv(4)) % MOD; return x; } const int LG = 20; signed main() { cin >> n >> k >> MOD; map<int, int> mp; mp[n] = 1; for (int i = 0; i < min(k - 1, LG); ++i) { map<int, int> new_mp; for (auto pp : mp) { int key = pp.first, val = pp.second; if (key == 1) { new_mp[key] += val; } else { new_mp[key / 2] += val; new_mp[key - key / 2] += val; } } mp = new_mp; } long long ans = 0; vector<pair<int, int> > v; for (auto pp : mp) { v.push_back(pp); } for (int i = 0; i < v.size(); ++i) { ans = (ans + calc(v[i].first) * v[i].second) % MOD; } for (int i = 0; i < v.size(); ++i) { long long cnt = ((v[i].second) * (v[i].second - 1)) % MOD; cnt = (cnt * inv(2)) % MOD; ans = (ans + calc(v[i].first, v[i].first) * cnt) % MOD; } if (v.size() >= 2) { long long cnt = (v[0].second * v[1].second) % MOD; ans = (ans + calc(v[0].first, v[1].first) * cnt) % MOD; } cout << ans << '\n'; }
1081_G. Mergesort Strikes Back
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: <image> Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation a of 1, 2, …, n the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime q, suppose the answer can be denoted by u/d where gcd(u,d)=1, you need to output an integer r satisfying 0 ≤ r<q and rd ≡ u \pmod q. It can be proved that such r exists and is unique. Input The first and only line contains three integers n, k, q (1 ≤ n, k ≤ 10^5, 10^8 ≤ q ≤ 10^9, q is a prime). Output The first and only line contains an integer r. Examples Input 3 1 998244353 Output 499122178 Input 3 2 998244353 Output 665496236 Input 9 3 998244353 Output 449209967 Input 9 4 998244353 Output 665496237 Note In the first example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]. With k=1, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is 9/6=3/2, and you should output 499122178 because 499122178 × 2 ≡ 3 \pmod {998244353}. In the second example, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] and the corresponding outputs of MergeSort(a, 1, n, k) are [1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2] respectively. Thus the answer is 4/6=2/3, and you should output 665496236 because 665496236 × 3 ≡ 2 \pmod {998244353}.
{ "input": [ "3 2 998244353\n", "9 3 998244353\n", "3 1 998244353\n", "9 4 998244353\n" ], "output": [ "665496236\n", "449209967\n", "499122178\n", "665496237\n" ] }
{ "input": [ "53812 4 967428361\n", "7 2 400166453\n", "75727 16 485722667\n", "65536 10 802338989\n", "65535 12 196344479\n", "5000 4 961162523\n", "13694 5 579788161\n", "99999 14 746231791\n", "14823 8 622667251\n", "65536 1 262776883\n", "65535 4 585040979\n", "1 2 932173633\n", "65535 13 543456539\n", "56907 7 653135281\n", "65535 16 589256509\n", "79602 9 341282581\n", "65535 15 148502831\n", "91299 13 883710911\n", "65536 7 999999937\n", "65535 3 200770211\n", "4558 9 768001957\n", "78790 14 947580449\n", "11045 4 779484089\n", "65536 7 474924587\n", "100000 1 327496733\n", "7 4 674998729\n", "93705 8 728681249\n", "65535 7 775068599\n", "93014 3 464769397\n", "65536 9 512750233\n", "65536 8 624488609\n", "2 2 105534269\n", "4 2 717931793\n", "29670 1 798626077\n", "1 100000 355399153\n", "4866 5 828460181\n", "5000 3 947484677\n", "4862 11 340369703\n", "67260 11 159230609\n", "96560 6 621206447\n", "6 4 142235399\n", "319 6 736338271\n", "99999 4 721319531\n", "5000 5000 824957897\n", "95449 16 477786341\n", "65536 4 530056207\n", "5 2 488196377\n", "99999 10 201673531\n", "8 2 401001541\n", "65536 2 547031129\n", "65535 6 100000007\n", "87440 14 373345151\n", "99999 5 950991961\n", "65535 10 764125471\n", "39062 3 557718113\n", "100000 4 866430809\n", "99999 7 612486629\n", "65610 7 576223171\n", "3 3 537728333\n", "79173 7 329778431\n", "19679 2 978579983\n", "65535 1 969378797\n", "8 4 617453693\n", "99999 2 594212063\n", "99999 3 538530137\n", "99999 15 385602223\n", "65535 2 332622313\n", "31581 2 803297119\n", "65536 16 307380313\n", "5 4 294228373\n", "12657 1 328355033\n", "4 3 691608353\n", "65536 17 355422121\n", "2 3 738541207\n", "68102 2 409693891\n", "65535 14 379941571\n", "65536 12 883299773\n", "59614 14 431666281\n", "99999 11 739822453\n", "20621 4 420701179\n", "65536 14 292184353\n", "23880 14 515153497\n", "99999 8 616151843\n", "33727 15 177545087\n", "8 3 930233189\n", "65536 6 526215803\n", "9292 12 386116849\n", "3 2 457143689\n", "5 3 698057369\n", "64554 13 711786883\n", "99999 18 278747437\n", "6 3 706327789\n", "6 2 126580711\n", "100000 3 372547751\n", "99999 17 222262553\n", "7 3 957060541\n", "99999 6 769267349\n", "58791 1 627994511\n", "92275 9 505206379\n", "65535 9 939195329\n", "65535 8 629794369\n", "65536 11 506680939\n", "99999 1 501051697\n", "5000 2 444286949\n", "99999 12 608975467\n", "99999 16 424240459\n", "65535 5 492219967\n", "9569 7 974022443\n", "100000 2 330782867\n", "65536 5 347538067\n", "99999 9 543989543\n", "93976 8 747153793\n", "42288 6 367611719\n", "100000 100000 658399519\n", "65536 3 759400619\n", "65536 13 543490043\n", "99999 13 838056061\n", "65535 6 563701807\n", "65535 17 131827369\n", "1 1 807831149\n", "65536 15 568071787\n", "65535 11 390043253\n", "58370 15 756534617\n", "74973 12 872697443\n" ], "output": [ "950881274\n", "37158321\n", "166058860\n", "462855383\n", "7405077\n", "935148925\n", "20837734\n", "534083991\n", "282687828\n", "22617908\n", "73478343\n", "0\n", "170536956\n", "367828981\n", "362272581\n", "15283453\n", "46429722\n", "238048909\n", "195101941\n", "26568059\n", "338635790\n", "804769289\n", "766560946\n", "244871950\n", "207497869\n", "0\n", "90464274\n", "580904942\n", "3096497\n", "56371267\n", "456424095\n", "0\n", "59827651\n", "619382846\n", "0\n", "236115936\n", "453430334\n", "187513462\n", "38214063\n", "336730170\n", "0\n", "133698563\n", "482453887\n", "0\n", "181225428\n", "175616225\n", "455649955\n", "6497465\n", "108365903\n", "68232417\n", "19616415\n", "58174995\n", "236965854\n", "44493100\n", "311741364\n", "315155497\n", "160702769\n", "475692890\n", "0\n", "112881569\n", "166411803\n", "589003274\n", "0\n", "241186421\n", "420705596\n", "286320285\n", "23332505\n", "335334542\n", "210721421\n", "0\n", "40046748\n", "0\n", "0\n", "0\n", "248567049\n", "364848655\n", "385022293\n", "382826545\n", "286705678\n", "289746143\n", "211044160\n", "28372663\n", "506564910\n", "137396822\n", "465116600\n", "158448501\n", "339357599\n", "304762460\n", "1\n", "501856006\n", "0\n", "529745844\n", "61180682\n", "341122978\n", "24806593\n", "239265139\n", "537638613\n", "550083467\n", "372045131\n", "374972142\n", "563763277\n", "206613192\n", "245192364\n", "161627985\n", "317105066\n", "81192002\n", "1097877\n", "297115301\n", "20709968\n", "50947333\n", "330542468\n", "239386990\n", "235655808\n", "0\n", "725177449\n", "510005251\n", "772746099\n", "478190145\n", "0\n", "0\n", "2593916\n", "2137720\n", "174119749\n", "741474461\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const int INF = 0x3f3f3f3f; const int N = 1e5 + 10; int cnt[N]; int mod; inline int read() { int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') flag = -1, ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar(); return x * flag; } inline int fpm(int a, int b) { int res = 1; while (b) { if (b & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod, b /= 2; } return res; } inline void Dfs(int n, int k) { if (n == 1 || k == 1) { cnt[n]++; return; } Dfs(n / 2, k - 1), Dfs((n + 1) / 2, k - 1); } inline int Calc(int x, int y) { int res = 0; for (int i = (2), iend = (x + y); i <= iend; i++) res = (res + 1ll * min(x + y - i + 1, i - 1) * (i - 2) % mod * fpm(2 * i, mod - 2)) % mod; return res; } int main() { int n = read(), k = read(), ans = 0; mod = read(); Dfs(n, k); for (int i = (1), iend = (n); i <= iend; i++) if (cnt[i]) { ans = (ans + 1ll * cnt[i] * i % mod * (i - 1) % mod * fpm(4, mod - 2)) % mod; ans = (ans + 1ll * cnt[i] * (cnt[i] - 1) / 2 % mod * Calc(i, i)) % mod; } for (int i = (1), iend = (n); i <= iend; i++) if (cnt[i]) for (int j = (i + 1), jend = (n); j <= jend; j++) if (cnt[j]) ans = (ans + 1ll * cnt[i] * cnt[j] % mod * Calc(i, j)) % mod; printf("%d\n", ans); return 0; }