2019 Multi-University Training Contest 9

传送门

A.Rikka with Quicksort

前来填坑= =
题解不想再码一遍了,戳这看题解
思路挺清晰的,推式子需要用到高中数列的技巧,还是有点巧妙。之后分段打表来搞就行。
注意一点就是最终推出来的式子中:

\[f(n)=\frac{n - 1}{(n + 1)(n + 2)}+f(n - 1),n>m \]

所以我们对于不同的\(m\),应该从\(m\)开始计算递推,因为\(n-1\geq m\)(我一开始就是这里被坑了好久)
打表代码如下:

Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;

ll qp(ll a, ll b) {
    ll ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ans;
}

int main() {
    //ios::sync_with_stdio(false); cin.tie(0);
    ll sum = 0;
    for(int i = 1; i <= 1000000000; i++) {
        sum += 1ll * (i - 1) * qp(1ll * (i + 1) * (i + 2) % MOD, MOD - 2) % MOD;
        sum %= MOD;
        if(i % 250000 == 0) cout << sum << ',';
    }
    return 0;
}

AC代码:

Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7, base = 250000;
const int biao[] = {0,430276075,367631060,82156064,416495341,103778161,312866495,372614758,864422505,135244213,839083452,75149398,12274495,929472113,296922560,545282131,165736797,207238703,178687671,463030618,45513528,848929864,959617059,911407582,907969740,659095042,209690930,432634376,998842553,549632212,465169763,553024204,265853810,491139524,657766420,388161912,234752773,983353163,689334482,17344532,211535148,748104578,220532609,893786629,218147054,81483556,534793035,959881708,977778800,371118427,59125624,173486302,155307713,743188128,5109642,569236876,504218398,41781867,558032090,466830542,355562371,238124425,78281979,169970521,657514755,310361120,136221681,254472731,734214987,682656054,135048532,20534979,698555650,958079874,874131761,942466030,428951372,301204149,907140402,547633082,249033323,618079471,342474578,819713719,285148375,865954550,953181098,80929740,274605739,895491124,448568388,123805205,338808663,472404161,476020936,196163506,994414699,240239382,853282425,645869452,608885782,498702455,960456812,1423691,56743518,969402887,588202804,789396184,402157044,673238460,662478809,109749470,832425005,992864284,9066205,767346863,829118770,729394799,928753244,867501430,354377916,386809612,318976895,725629204,435655567,131291942,410301260,221244928,159046301,359593982,137135076,523089785,162937071,833886248,928999173,631291292,184431844,842527267,312900794,894396299,756250834,393384865,889234789,635122403,200861698,759211229,996606186,167312697,901660772,865559209,510861323,611120390,972009731,386146829,293148505,38055023,869619483,610471486,424395325,573870351,927422185,273878248,95462189,668729212,135734976,187016670,418685792,505156759,435907069,463951019,124095618,497942800,741119707,828565633,751342140,923109706,489957190,982707589,912092561,316639334,906042258,225957004,12909523,47279004,791698432,415185110,732306601,529986698,306799763,347159022,120648379,776190492,508431604,772672805,494594060,682589755,108542774,557802368,129661807,580786551,240263064,719310714,897535368,372559676,576987022,10234687,317917754,147614861,485649473,461147490,785051077,752522019,680622326,768136403,377595485,460035981,717071900,434044420,937381849,920977703,624151210,596586624,119953887,711446600,983811862,944916225,586679874,188016035,707663439,66584585,436611606,979994948,521406514,913902044,502310459,412389409,975029971,998539050,504025836,278566363,587205928,33556674,410828125,242968511,608133297,573996044,421860420,999429443,296993162,394220938,867622445,96647644,721500554,263031174,655855168,695111461,993238219,431519266,914118475,317251351,51262505,323301407,589135201,112395006,71578037,429705756,247240521,106637126,360906467,491004779,594945904,33463242,184731867,80889757,805760293,831050394,833028231,686520512,910357398,693116921,82224834,756552235,975660951,594266001,268408365,720155309,847449717,650583739,146356515,987759346,562620479,77703814,929728098,537673876,852004855,758918848,206473409,432003382,946394676,435562245,863220082,97939517,649322791,461140689,280428957,161242774,357678382,85908285,226748229,664229757,807653212,450115089,877487170,918238788,778693149,72817560,994174733,880374288,945894295,37595677,27648222,534300197,931682819,911395094,808783571,429882934,635791874,190617949,74852114,825127333,550341803,343365291,775821876,4236874,921901579,864694234,135151256,386713078,377664017,93035598,927497455,105307879,398306653,740225505,765008343,257047250,959715351,448603188,887165205,867427501,108229174,433415600,519437663,265577808,885845356,825251947,957846611,231305947,630517004,118126575,528227783,740683510,231078471,68992178,414230982,344400899,53219084,724736420,666735328,12104846,768839578,990121728,597180915,250870380,609184788,517652538,42744559,126393448,914722723,647067478,380932379,337266904,271768052,444799367,348308161,279469291,320228531,96507612,492753477,586197744,393150554,402687584,583552826,379190699,684709488,418863144,53005468,172741582,972949808,296586001,846505392,929476843,286515437,373984842,685010707,444942883,276211775,904128865,383307622,325276416,177895317,293546816,56790451,257386998,743152811,883231752,684014983,173870794,435380032,968483153,340540340,329230477,129698818,821278359,621001948,971036594,170167081,118656519,816303098,658091678,871518425,43270904,29935919,584192702,88492245,815508333,73162324,213380044,438852764,260979292,967960859,70989479,855761802,945337755,46102073,707240065,42991241,838021956,899095549,628310720,839358117,197102910,991028554,58802467,648713407,622315385,346421501,487692702,199916211,814337702,502873189,235396696,37986776,941524078,478280026,294678109,924420678,213394313,600984357,24389351,973132888,981199442,49449177,220502353,287659969,685522669,659480720,954473502,260894110,821294025,313954869,256989151,419149432,902768142,302216867,916023211,66645682,741140752,651621754,424645267,232509196,337989297,545261467,720537822,81151904,451310447,451666784,517572146,782505112,868327896,229887728,208315451,214742552,302477527,245148458,82971608,18460011,992220175,429220589,43479197,139166348,40494390,590531119,543358335,613272257,838766738,10415563,961496397,304031038,538427445,444303571,370290122,262544284,884916855,911668327,482981164,640533692,812253212,967438882,805169282,146157642,488278218,221166498,608389865,829502868,105942829,988472937,716705099,895958477,774705441,964954742,172678195,338299229,631430161,27762076,551173713,666901962,764147036,149734779,450535695,362469232,435539874,436593785,127314458,386731988,49706941,703357790,111491236,912029883,148513579,337793070,182489643,733783956,535090850,149070049,288554490,939179123,72936262,679839510,226462809,363577837,32434052,898569029,184614791,993896698,254057127,202666767,176290404,272165631,445690812,452686251,528032256,138797446,469247940,304533025,475697266,706767770,545270672,767505123,49032751,952259,406053409,452091527,194438335,11340134,209036970,114178294,177240026,620211698,312882791,148360414,131197852,716571312,261444024,349961983,410871863,878845019,553078118,110161307,837407287,195945016,267177246,425800918,125256272,741838235,361566034,106197609,109587623,468505032,883492738,745421655,367005657,20250220,130297161,493665953,858583110,945442925,668151888,599523469,931169213,826563319,705743756,225245427,454972380,185933861,965216189,125639917,269390061,717863354,649833626,37662220,718249068,475418818,640946759,727733521,428056344,771976318,836031260,832022217,474125422,404474696,212573169,208826213,614259727,604554531,431436007,347457723,799245670,623443580,485092005,238266927,117832636,678344632,142827252,955556994,473908065,468612166,981554109,462747556,148997139,181675575,115521253,849791429,347853965,469852509,357062487,764669391,776876682,765683456,863904168,79342097,602586115,136333814,720915106,946379206,842622536,495902542,792643865,546738897,862020265,81505766,46751026,990009683,861567909,740540153,604400588,663448222,610125820,29667239,828509916,415050990,590424729,481056440,165542119,527292495,90167358,378128657,615481063,728965062,680496439,355597416,21639032,809000210,655810204,128828520,447305692,491444733,267503266,25750245,206983203,183240845,411275706,271664677,634241590,742940734,763875921,428414536,72410788,741430771,248880352,418660210,163047663,936440362,305558208,571765458,552698174,811725513,468826254,788838977,166122471,392931391,7471020,19972942,691044429,921412895,908060872,641651192,359913919,908855177,790431097,845621407,832261132,648040522,90833877,455865701,415172082,434985504,766651523,814436488,688658863,866273132,940445720,28707773,516277328,343962733,993825727,579164592,497670871,910126292,238315176,270486870,704945145,748987208,925532987,209942708,402292425,27314353,535746516,879315022,199470075,797193258,704564845,514725618,732693122,857990177,104530729,361146875,401353157,101839528,861997742,248136117,561546792,21009799,911693254,897910726,69521324,484861129,128813396,355365178,749572267,197002259,62448709,238249723,205646561,967332432,276017177,855955538,349049070,828415100,851388063,28076346,572247440,60587384,135265460,998147119,494245066,929088576,655997903,143865921,771391531,987862713,883531544,789262846,358787735,622523197,162380135,135739915,974269062,68174041,86774153,954901038,955086583,673037844,457597432,157597580,929351207,510231805,754618001,994799035,464704293,402518746,583574921,228762260,238620291,718111142,846279710,190649946,406734428,803764385,575015087,838039622,171570966,626464450,566549058,690972145,753143082,783940885,524464700,146672980,402650138,241697627,213792920,959447338,137077215,655763355,728199521,725979146,667856719,249686601,971904247,626978673,624934806,27013634,283077581,422431380,43319452,993052378,848423574,575245965,918898104,677676661,613809364,948222281,659635281,654806062,837875524,138423121,883161457,345342759,594299610,103514144,387007351,76560912,754267693,580930991,508531779,336846243,361041907,909143152,933141440,504900833,365890248,481809591,819605833,467599291,896931170,889664741,700416497,486363126,196023332,480059505,602810816,505674937,915654611,22863933,121940552,810478974,760247851,851051661,984858502,486169036,791795989,832071674,947508209,435995126,432676802,757795262,803103622,206065693,855736175,627859082,983656724,754579310,351651209,747999769,329674295,424932027,483054022,240647901,147685597,821057065,573238704,439600200,51267212,16841983,161473741,234322750,45859558,467416458,115560010,635442586,515008333,224639808,303447038,915834445,205018031,496500261,571371570,173760431,395508265,396386378,474856881,195566946,198072476,843391857,402449438,63135224,63505018,185023592,843065314,564880916,110012097,868735536,542141035,239904773,530777259,59443498,811200784,744970071,419485814,415214776,913998629,876787630,956503300,713878227,538835149,3548881,467458159,80081937,160951801,343419524,527603964,941921899,349043988,642244898,392596538,907875236,374471903,73993879,726547302,30474449,512614503,488260188,69140669,541625898,306092241,134531127,897258795,18555543,982677872,935586923,909084873,866529789,138418419,329571386,19911522,946449209,674087062,225657357,875478890,313289176,364201636,268875669,793308244,704037713,479424070,206325156,796457818,993464992,392737953,545045674,170010992,992142239,121379406,488334594,134278413,385907738,197926608,282485797,193102030,195754071,822760173,426001600,944287929,574095062,218539506,622785805,118315496,928001586,849220429,167198,616006161,200828805,799114761,48042310,729547802,569901214,133605378,306002386,110583398,923020893,544545526,744000884,722303867,235592387,724135192,596314300,214362869,417786774,964941259,913607475,329054684,646965910,99679448,889519087,878304847,300527691,15492154,460270155,849128657,598277451,755541319,528998152,344059559,903719769,555718240,158954433,132523720,400360200,587676131,192617510,522481194,176330189,623755528,704725994,534063891,977995940,715068484,241107792,30065719,900731769,745621954,928034230,947064882,765393155,286410439,636090197,345832553,834428885,922516809,309080997,36056191,960491115,886923581,627059614,410983906,371053449,192529478,387267837,124605770,369600855,40787177,413884868,394605095,821419066,770824416,392681073,959852080,181951446,407689265,533003607,575464356,836645477,576876439,926000450,118164894,327943908,744828107,805939155,451856605,225497109,697184078,608749449,120354052,217873149,396277306,188022022,302184592,798982491,857825611,78923028,189776158,690639229,395450751,428901205,586415933,668444088,193408389,259885124,106075397,416732765,83518450,217046194,570975308,360907121,692024051,17674850,611264017,404657053,237345769,504213326,266394367,857855358,415473044,30246705,506457435,907376164,728191492,305623913,742435517,882165967,613596427,412105896,2529190,565705803,854538353,107278705,231332525,880420249,330681530,413858671,988238105,462249231,581999896,999395472,960365309,365594257,127463523,435874449,181157358,856380759,516230820,823549972,414677852,686010347,885100332,829142400,640258498,93299085,856818039,149954593,58461076,267861840,441942967,348296723,356020280,250359250,737784296,678216041,519876577,134511915,304776109,886438446,126024434,258258190,147878054,464631952,163875155,528547327,505465317,101844184,816556188,496780158,355137173,888574683,251203581,97068043,117847724,316579676,446020593,327328903,277586343,877506141,230589060,796896389,670202218,258606768,495602746,856959992,100539236,816843732,783460253,160624176,728690282,198441531,281050326,13162534,462566696,308076271,997800190,58998984,31366459,411457456,835435336,537838870,136662898,80690691,920613671,141201203,59491007,357896264,472626961,864827061,423718232,869486989,943198453,72773576,129769600,922334408,926766422,884552674,877942486,92179962,495100191,902066633,238741167,808005352,249784538,290147569,880686361,570550185,245947208,406891331,110368657,49964156,607057722,834581901,882496729,321591489,82424403,751394100,601449911,461716814,6666486,866706973,442239639,983938110,668285600,493213426,671818338,703915772,752510900,196449911,747301190,165303852,55130347,392833791,228364719,967785586,621898649,372048175,812768333,913098445,458557802,385557741,602226086,41189814,507163882,296868137,567455548,273170750,924112075,262047493,71370451,583900,293613616,814838044,830984056,735308342,519906815,817121161,514840165,213807188,426413492,388092904,655148325,360302052,802002892,425723697,894698116,778845154,501612021,61152056,510443134,717471515,897529032,344963122,169157902,713981802,254439545,310770590,829722326,179345174,640738255,166700291,497710026,578918322,247575565,32046135,242203774,423652711,15150626,348002400,760675993,745717220,110186356,159074417,2372676,792465087,302585356,76001329,235819786,749880484,653481853,370825730,29794539,163198071,568446281,729425617,314410379,166628933,146757591,817307299,815079905,779287037,578512351,480183886,227396399,895013004,319774965,613957195,934616680,959233684,583971442,215332041,190876071,11500307,43538920,406858885,935425156,208378494,791049033,581842088,765358037,46184539,800256464,654396202,826388675,153143295,752601153,243822516,3790690,892726733,50004798,456271527,170582734,603280914,889658504,115987925,373095871,457069973,802971022,201326849,714050451,190158567,27410641,704197848,342624334,459416379,521055417,376475963,683369712,424315232,889105770,50481422,507360049,778135378,312739227,176824048,482280235,111127104,369059675,347877558,390469455,956945085,556929526,494413110,548489736,491149149,240975374,458075614,842819580,418242318,397501911,240216718,755297430,325145304,452547790,116953132,326400030,258467131,981266003,393907014,4205438,374448105,659066353,26309771,899617295,839923154,839403633,328100674,145241017,870818065,298503593,358478362,279512962,50100645,971964378,850119988,63797820,616855336,498463752,719122543,658380587,25070608,584342240,284509118,477468185,515336985,804868032,118044115,86411447,13864026,41378963,755121031,271396733,50766451,225107326,9591961,96449453,255003348,250405410,900745565,427140477,157061255,654415377,746462772,867457161,387677480,750666941,956291955,691789057,994000203,7221465,154297905,530710667,563460296,294430229,618625545,248782885,248074442,699830813,875395390,890126284,547761836,525882643,24065033,414555467,383891165,32146490,784512103,771018901,54422306,195493778,245339529,573405927,649360545,195803745,950312920,279115885,295682540,391635736,476541450,629314549,902916248,427197398,444885323,441648750,161900135,371412215,243499504,282140499,462094329,30244302,286605520,497976286,734747963,205317867,122936675,146605325,122897779,635109149,418115012,177376701,43173018,679108630,155537681,532222536,457794287,537827575,887376444,337496859,451677935,302495511,489942561,262502395,582582252,234713596,262241368,210472264,292329925,814020796,32940126,699432217,659169751,56581808,256018275,538101227,412294818,196595053,16533364,457396805,698219573,367068069,769740953,285347256,526651745,731419241,34278243,33705145,572436423,264976298,797440365,880510192,376598572,912103037,386343218,739300710,956079492,131012149,495922329,204219583,931506761,753351028,95888947,80054860,904003200,727273764,717775706,717831003,867720538,391284387,488424521,755770575,217196050,660935625,957285374,966153072,518967734,864920203,477781406,79057835,125390372,219649354,182030165,968051767,29145964,878135488,199326026,978528431,52109641,750742373,317953786,200846949,395714630,653492638,797523980,778225388,278444669,244858589,704849983,587768495,808661584,622658313,659412184,387276065,914983305,687535854,933083468,134030690,986914808,877654049,935596739,745629939,594221633,887095489,591885438,951756534,238733077,430335980,816703021,953024712,11475646,30634950,407577363,88290695,879755940,809241143,392846069,245443587,473504354,589110116,530778971,991107586,114598423,356424018,814306836,805663938,539304115,315020185,488035574,91128961,829828434,299364315,527111693,856366840,730928047,927682704,871789017,473990628,179394183,407560943,671576260,368625307,905638502,833404016,395567982,828731767,178157359,852674709,665233199,646771720,149507922,88270079,421600713,378725478,761022104,547968065,971876411,45099571,240951807,880922484,92790105,601700970,813928383,221821987,571732912,641690881,83227863,787188494,736688884,891791585,718265831,605127573,787385469,456081075,876124354,877193364,311914217,34809846,202796102,554949302,144039909,390309056,907348322,541947124,711654018,34460196,41711032,325552254,60179451,486468804,257578225,833004297,667707551,892063960,351814799,275696579,392142918,453544063,571992140,840286036,453951422,563249735,71013578,842535319,433379379,811191946,84849046,428526088,150258716,773060979,857895793,192122293,520513498,634631428,978150578,713267460,704000206,925685789,977038065,580404728,9501527,101764644,959539836,500623425,773292246,900305086,742742207,750687792,117636819,861553992,625666208,942886322,325795686,306095296,81819585,693368731,35560036,642069976,773578817,657892807,960413034,245027281,918683748,262229194,655231228,903056088,228636082,816049027,734661983,55675533,525369423,422176015,753084894,29601890,828556963,235195445,805992947,518466807,351679493,347792610,688232255,613829246,776133719,46909401,797096827,370031195,173500995,43320833,169116553,425456698,263137800,417346640,936151086,886433772,242945239,788351837,186694260,365666379,701929696,720385963,972318087,44448160,328756978,143483696,988625917,330517431,532363824,456710412,974040511,422668886,977566161,578773985,161162403,34614567,244209154,954628261,647145485,401173031,33493746,200927174,478757143,111083607,753634838,610559347,604681466,780135402,98489532,647669569,370355616,819088349,3124232,759322520,855342148,405798756,300354953,986112792,577680835,424527842,958270875,569242530,647761214,946394994,839111719,208090774,75842155,938847171,972469053,501558728,527208096,974449006,638937259,45668214,94157560,328576087,571522237,169270969,349401358,349979544,149560122,927234084,716947545,922363868,14816691,752284832,768139322,193135818,291728547,732730339,719327102,731861197,646219780,710417360,656532108,122349353,847844821,960053648,781751022,881129101,826375350,161463154,551653718,864580373,247466509,824111748,683985780,864386788,480496725,981519015,820795864,840147684,158115345,725818445,56864274,604284867,511171776,39375022,813090398,891894739,161786048,580655896,242653988,828163933,806619515,191176339,898710629,900923512,11594049,16076676,561286260,549228971,305351145,520157996,860903714,866007718,879175016,795316158,358954947,3495039,914231845,395531648,45391892,287279410,145501286,477607587,956358581,439694164,335413536,170627917,971884604,457134489,982877360,126287839,24026430,893853146,406939109,171831793,627961412,751273227,852825055,202784654,489382745,558686050,26830952,6293541,472454659,14045608,227417280,360480722,186982959,631975552,944412956,736668617,641057174,247687826,54707850,504808782,3429505,820733479,67501109,485397413,102972380,942482669,810205275,574796409,530397550,455458482,459888806,325930121,429000182,111394209,829186424,799444199,80086169,907745857,502843885,816544787,690251818,80406380,431178888,914515041,177920599,903534678,866625505,262435929,723704760,690432920,970519058,139150124,266651281,861646852,977652770,521479469,485318105,80130055,663917420,964398366,150698687,779520435,509152287,940303860,967754496,416286011,242834007,364879032,709592864,992053617,398177013,471222926,745997503,839136644,924212758,271238081,583712235,141343628,853436605,54166184,433022142,182551772,846889591,364778426,553384376,941935514,223313302,344242000,776090649,945968049,716215286,973759956,900749468,50077771,774315657,919750030,835563214,549307445,275751649,781316767,709754178,812503381,200108504,663772528,422679695,985626893,53747298,288708521,433047385,598868426,326863150,703564132,791105510,218111853,121892685,615882605,819058654,459409171,592140752,875202980,496493936,147241136,429247279,51114178,921823631,586649464,814684003,870797295,802569306,591588368,896406682,103516159,993036578,920872813,267518107,949532647,815952279,980791896,557067462,141050908,676225788,35026657,269658194,677395167,748725309,434575564,960850842,18830700,193379366,216235703,46691407,100046862,495814369,395652014,831091846,123462944,917437299,24820143,675400258,448424203,556350645,863005512,26377701,902923320,375952770,666907670,530320213,638768933,984502648,56910366,447147692,331501046,436886849,389771684,647951073,198598956,514898236,292226392,401097736,567130464,94493256,763747291,346141911,151846435,90106929,476412105,591578794,49293670,943680838,157766805,587679711,744270212,658569976,152178157,334986678,48161533,151559091,455212935,570164256,840990503,566119441,238573820,646188623,480489137,413717532,411073757,850963097,935512821,574808848,43140493,936025926,900323438,210146204,520224347,751845276,464142625,540672891,782304021,369503335,639604864,507683361,690745681,660175545,442820506,943351157,255547327,19244052,98109277,285171125,877138928,925322168,401061808,390914238,538507559,11322882,257908356,977946598,279162696,723349895,364163764,991441525,853131850,14061891,677188112,970840666,130528079,729453049,405489656,600258014,483462430,950368548,862705963,515292165,426162980,495167173,337934796,110487493,973993992,221054355,959045108,545775687,931724107,834548148,270989060,759608332,184114709,364808147,630073236,552997318,246334434,56552965,533670320,566219778,234716128,80144008,182283491,662179047,358134796,768070579,219042373,460391429,623032220,602218623,716094729,918145881,98181085,640403805,418862188,896669452,398691468,449715971,213692332,922971942,32641909,378254479,14337689,439454302,176268823,187386207,3084567,817446823,49445022,33388289,297967782,783006350,13663230,540423125,9676400,217034153,768310826,864847842,710745942,388853256,415471691,41450978,247969150,335270569,944263930,764397448,800502047,742258241,161372283,666325716,901291495,453493821,436195550,180765341,600654981,581884114,844215125,199320668,704556272,457536900,368560961,63827505,638000240,487234870,657697267,342478211,567118200,703039876,202394816,933845533,499110645,533339601,936520090,134059946,118158117,161329116,416185064,684742746,333717290,880893256,437074616,889195644,823289964,771492049,997544587,205504302,28987646,122525643,371122929,157505348,841745219,20028966,163508536,449799481,647083454,142439894,108337144,921113084,314984636,244870463,567780631,954247482,531390945,276027945,589347104,446899201,492983654,118166587,303335232,59220220,425919814,903022834,626916097,765333936,374201191,593229838,767574023,552736242,787948644,68188188,230973297,698749811,167359604,437708859,932348715,444146761,572392058,471204977,919404690,750894664,164116985,632470439,478713649,633324632,556543330,417291492,290011951,803309283,830015494,168048524,707881400,739603593,248404235,679497446,624890703,529155096,338576518,574110201,694715959,822508695,804109646,76154184,560404422,743615441,815813801,522999247,455829524,937404209,13541709,151805270,313710591,255276219,847090452,833287857,732807874,971821426,522622918,173292556,156264867,84239980,132118835,181533796,434113576,452647561,992017977,877008536,881328598,1017974,94098311,455355284,426274408,413345300,184071176,568367282,172606479,383996953,892831005,109341373,466170578,951101208,730005498,421086494,312756372,960605128,310134051,836570111,455602245,545306843,352741923,324467158,170085367,112068213,432873512,781098666,571065881,809408527,313810600,883162393,28293027,566173275,340923455,769043159,885249050,792784383,473778260,481035782,656887302,11270791,452218230,939479001,189075133,942898028,755123362,476219898,757638929,420279420,962201657,561475190,491780200,175981201,563484180,363576979,187641233,566131217,818419822,73213601,251986960,612009706,460522815,203849544,898072035,165259690,347327708,691701965,849177421,752996728,765384597,881346240,535595524,730145075,715595024,218391685,837783216,93293452,603546836,217175385,590541421,566637470,357278992,498627218,745531191,27467423,320014654,471425801,410728016,607514518,389271088,208575389,718729130,89405729,962278630,390498738,952510458,476029887,71265558,92645550,324385814,619321649,402085043,999313551,849894937,664249999,513919536,23752354,710672971,649060197,79225753,470011911,706781312,115066385,439551720,642913355,370097369,430476187,804034306,928239001,627203968,528880077,628349051,466851084,817231884,371206087,153062325,806508537,13718375,248552924,388938942,331836205,931471218,930612223,920839656,99646847,149462358,303635564,80325682,585526269,408408026,450993938,617277197,473896250,455152790,564808433,85713257,939524733,630056346,416326982,383055957,14567458,590279843,937707842,771223281,332170930,691119004,515611038,840278466,574783214,823000526,737071366,354055497,630134151,64434531,626684438,361990248,580794823,674318965,41288769,312969954,293439121,813377805,535983815,231359191,369440165,858597855,615288389,692749769,394440896,568196751,123011408,901573355,118819165,381626018,878558378,147661559,315018821,709420646,179757556,725309379,565620953,580497299,648918490,190067119,249406817,198587157,73341627,274490257,834081565,914639228,902627868,162858217,640727227,882062763,228119562,766295217,877503539,422626535,258839704,549201580,664470910,463107373,762846179,122163436,843246240,300296942,517817751,297409712,902541296,838459687,105261759,379626894,670996757,623631765,952291865,290198380,972058439,221273889,51894511,152095143,32064642,628681213,818432236,767905774,966578203,116561365,612639494,578380534,498462174,296007743,29795989,930917318,710984562,187724012,733239559,42954165,632912980,62949124,898407271,449267670,168483667,534177845,613859872,207352002,577599637,958358420,21480288,115281963,208659669,789572793,632483368,564048210,5227987,602514066,420046598,374635636,400902540,488663139,824935057,243999810,755929364,951183285,154724167,947343086,818296547,171387175,213091347,396010160,559391719,288945574,670747275,574671169,293383551,922928847,400430553,154049234,603103761,507446254,773410547,593052372,358916546,180637209,255332521,580724149,165204310,776338801,258991714,923736030,275528682,140847605,758681052,79360742,418971562,97137263,510905690,505365287,220871088,574579146,485355755,553676176,202322377,372714198,771868795,242784516,172417427,887096915,569112961,923014470,201477755,136614143,462350640,654541634,598116468,424934498,549350782,250324835,236401392,23240347,932807150,235154317,324866420,649355077,193158456,208791071,229458914,280636130,105647498,173510719,18000934,118184257,806201212,292881641,575911027,286461278,541439096,669230338,206432713,418615066,196042533,777167313,512195510,938850071,817579011,644702081,34091833,980083398,61865876,343599961,372223852,509310169,893616172,696685064,374025463,883221202,131958459,305850769,810874768,32515153,105806574,848462998,349131958,755412272,721988555,597385465,28993999,35932660,827282997,493376900,275064421,315290610,350537117,699017152,883779249,497556663,433522596,604095151,540813309,103457723,673278188,333009164,550373058,412144751,480321655,69176787,10135013,424470416,853019695,437661542,348672108,136728987,178850670,447206970,744194836,318135004,254877131,257070269,694886864,453935393,94942859,140931871,332678387,732973376,270665646,71173400,875104419,278465738,720123228,302687767,871144191,582486529,595160754,36010151,599742223,145688271,451843726,751124974,159622049,760742830,708433842,358581378,479000011,301311371,879703005,467821523,157439021,500385913,569919562,661406622,497404852,515411837,807493848,788913886,426836656,751375286,340514857,711792819,461755907,366658634,537379218,422781729,52439006,757189819,116901770,568372227,46032175,818429665,683767505,53066676,3116917,76650372,365799897,227278916,527203595,708848854,844231059,27349430,253420470,360708534,247780764,25661156,688525283,732457522,541778284,690862153,589643470,971171401,584346763,479833411,135177460,843076998,479531299,996134761,797770426,618078322,106910975,339788415,191161599,785028122,238648045,74706449,484205379,677256046,390328610,544357150,147462265,85683471,80845838,604570254,755121732,568999912,415348822,251011102,350954664,568651383,260693706,581755238,208182117,979871709,150049272,514439607,697186034,682840456,790892169,723624242,358422042,75845658,304173600,304620969,21991191,795544841,354580817,365997051,532272310,615713154,786539700,781321247,650517134,512240817,327866580,132634492,832033094,965594763,876479074,267273425,185041486,972156212,377263352,562454701,429275074,582874123,758280320,79978815,926412295,280543573,605373457,238786404,626220710,951599457,632575817,108773881,807996795,867096890,926948037,166999088,685700223,570227759,881315291,452719777,156896108,629013073,537198089,462002054,844149661,199289857,318979352,462233896,14027100,534928343,632224434,789802089,263945771,612638005,853157943,111086482,906559847,486206688,262666411,527478230,944318457,661108532,818088747,488645744,234384821,913533758,218688313,720105623,152740901,37281026,97953872,446997080,21141353,951698013,459440587,737264879,51844983,283517207,42107255,413643056,277497997,398579523,628585939,575941906,397126008,407063465,932422337,459760322,886356967,382956347,420678206,713940652,335723903,349178521,351032465,694705665,848614979,760211554,552945351,83269647,803663745,870466993,820687594,279011516,980682860,927955767,687034052,593496081,823791272,240398531,561782293,331096608,290572118,460907592,296137267,866614826,176989829,953837548,947104704,238396436,611040452,155540936,499499234,991478813,107865603,264080188,772242660,797887681,14995929,692801262,111826496,534095989,188659265,925261507,693705964,524174173,427625833,429520983,262826995,915282388,576560978,936996623,226531774,998576476,758319842,7277220,172915615,910086022,687723639,590581095,7838170,734135667,800557524,645695385,611372950,392555597,842894838,873109363,246849793,416851583,929248982,989161398,891087536,981762051,430770027,837280333,830621492,882208353,396906341,665907729,335344517,287462900,183188683,44332257,233826816,665118240,382041323,879037101,578349125,895408078,612062168,216952837,473899641,111651013,572932599,618017304,983589332,100324971,36926133,783004520,313497551,121036007,119227170,577938445,804172391,195560711,169067462,232776882,5298974,562830906,543249481,902323901,50784182,296352044,423504317,989085577,13973713,303796082,208761441,827659635,993427632,354833771,738661683,921338061,896918534,341525457,299656786,17787071,817262507,232217666,67615522,593208367,375784303,758736499,51495046,482272891,535059784,63057900,891389020,239021795,659479020,218047244,190282030,665551312,918659875,477144409,435808243,89051808,539430604,514544461,543162161,185341662,636642133,825881206,702344140,318744156,607541118,170742058,545129197,392724214,82184410,940487227,585674527,404103993,637010029,515548415,584155250,288928562,31259359,190010260,252979653,624899693,330276140,238781487,219984121,731457024,961914859,767102385,799591551,754459004,503970645,899032843,615721648,75146643,287609736,271809203,333221125,805244503,287249350,962944894,594890278,223427096,608346916,787997365,804960918,529652946,598306898,505651161,382914836,331800540,703484850,66644519,124357458,494469178,288191896,949329212,158576280,89447802,166692755,574858863,200696683,374116079,239090197,535160338,784427709,610598609,344692177,836751489,77939121,732916985,902966769,329491985,505863700,563860263,261226246,834692146,394782589,132795002,761567779,895777650,372897653,338788313,583168050,42419518,830454180,37789753,279591365,956755382,795647523,117620,214146310,346270986,473955184,291827401,404798867,282549419,85321830,383223132,515598774,138932431,699872308,844710330,10945342,642671451,315590580,747069143,394445105,973339060,835043338,272042865,59235944,531740513,252425923,387565694,117322526,986346520,34051620,815839478,278591764,472724053,727830902,276615773,86841763,664519043,623273441,111995188,912807902,698078133,418248460,458792894,760193802,25570304,370144654,859778404,260593248,59850575,786400703,70943234,764255212,685797381,142983398,567066567,227250026,532962314,585693765,722473415,586471396,117375722,175849205,507286278,427204948,477119969,686772959,637196171,793361853,587348646,932467799,466470299,240884242,91458124,208239873,8716902,359148725,952854368,634660113,825469945,969142672,851377349,632494065,292587361,702552678,397717987,407805857,638343046,326346111,673653403,354908864,779654270,278712333,931989942,782069256,469779610,404514302,173892989,207330417,483049572,221811229,95425554,434087202,905506221,913092879,471761843,562881334,769144096,877748897,244800028,273680469,43661889,760533451,909506972,1476007,112412187,171730544,72368651,107228627,898409494,975882541,466364525,443539006,471252851,657661077,452863898,742391670,231761530,34349682,813829819,336339512,699813393,995192994,492237663,451335529,822740900,864713888,29823224,130106184,433430461,922580386,370504515,115197012,420280862,472746078,664086534,656999381,246971923,861938045,750653770,937959675,651215612,884043437,116659001,939114314,790742108,159881188,538769198,222736780,335496499,337301102,741052874,716955114,707523273,30451046,96855165,6887981,695310992,215445933,633729011,284819515,556469548,646516324,654580856,485374446,859676717,356343817,782381126,689826325,513652830,499977846,844134626,765154676,847176125,173478866,471511034,929022030,492768142,92956403,115925246,383481804,986919907,201553556,959250078,722470696,90709066,995644583,321686074,970735768,868244015,736291062,121872168,555952434,612615014,123762499,508964913,205652849,494753872,426681963,767762904,115406183,437627811,84815967,141822902,692828674,700940482,619447218,149850308,485336332,230380781,908345900,747813312,224849709,582585892,549852185,924449971,205074064,126746972,869842842,279182321,818163427,595500830,679836965,89947396,278094956,772266896,352020254,15693646,864302696,955759245,29534418,399700904,988373066,878882549,857676755,64901437,108812807,506477209,192695878,512300069,764026945,422332856,228034105,421191239,28931487,476170499,113241064,579274618,940344278,854375483,591191063,854822706,98571231,230354832,353741806,809624133,494604648,85468206,598254280,971618638,429870287,932471713,208594548,464632481,701397095,444725423,824119287,546865979,57112750,849711635,762314539,992997062,656136957,93354177,723394828,240236521,321068604,59743405,994965506,345697389,511541509,957653452,19472615,193279292,11528419,968051219,60956427,22870672,219395758,192054095,766023810,932551794,92263051,463100888,160630725,395772754,13661294,17188549,200759507,267044612,607704378,488103621,293181544,803057813,737017446,382964687,210945278,59466155,647849310,569502061,955105061,407972259,714494182,120045641,328480292,604801816,609734543,784437257,430667049,795949826,639086166,660899035,723028817,742882262,378031344,985533981,449213625,394574174,740970020,907686346,346797118,193078322,450969940,703304724,870448946,944872202,630781791,157879785,598452123,405969765,371809157,147600945,14986711,140036497,253211514,331144913,222752455,434602532,816538087,689960290,79944043,489791125,493428287,321569085,28950836,53731588,145165113,874102513,389549479,100208454,80792511,569247287,683446993,121246951,142976596,372235970,635699414,49015359,755293812,60286669,192825423,64768645,346519201,681142539,746766735,208555631,319382985,48195443,571871485,174033642,617875864,256059500,245065261,311131848,496481086,822301293,233519838,528488344,100040858,583351834,506806302,760680133,400107466,38192665,903602162,135107412,637748994,374814558,699581100,586619492,468699525,276648201,146079631,897228212,99634520,555799809,370764786,386753329,966336005,161577502,944874794,321814674,953490990,32890613,312862274,468560591,434601721,648295073,378609456,759811227,333011689,945981960,910681250,18138838,559846301,769348714,626185224,35608990,874782636,655749228,948410650,356227410,753960885,628651137,779662166,772488262,686163748,964437389,591970524,874826991,179504813,261946563,715359226,629696149,245424024,200022820,650902976,214456061,335599952,845367339,597274496,205136129,382981253,781890236,951654236,361531210,256037618,234941146,468961472,49399125,404095953,643485223,383542152,637811712,589378271,450769717,529203031,944697332,865192188,89088803,491877495,598102975,684198593,445017807,280070987,540111420,944726828,338873078,769989269,571575269,868345122,408253359,363266849,883667844,36318272,7084992,82611264,845567013,652793445,898118382,331168734,332783140,761840598,655060407,728302738,960663194,594283679,448868601,985880030,136936972,892385075,646272182,670069766,811667950,726505952,313386533,989404323,176232197,295328934,58282068,467594587,95692020,369938893,152718034,216940118,63342224,689931874,555229606,677149517,427235245,11589397,41726047,162083979,150299369,786051532,582650621,468532238,705453944,24795698,218006589,312967478,592159770,513093994,641748648,541725392,638688803,251073938,768449794,438207102,723785252,72288304,312344991,764498001,705386492,421875936,222623518,877100538,694834516,919945053,862901467,713910916,601742374,342116348,867697508,934376371,222745427,206391445,654721171,523404828,385272429,236378337,444312778,132316023,626231755,756306808,960241383,601205847,50789798,909130510,755666177,465369427,537542005,8869718,931795329,838377183,994138650,88217792,772874065,156071642,914694204,206676817,724575367,167495408,684077794,546837483,24341178,812170465,599366721,492847403,523125681,821731704,872279725,576154202,264910857,813956348,336005890,644104457,430777221,707193,199049812,529489672,965238725,809234147,633116722,679731656,23612152,230990968,934790179,731194065,851781231,226823190,832824211,392646422,343003925,372139925,262309367,681306445,851049629,941686624,907716193,511904564,798884595,215846088,553267370,54186796,909002185,974801918,614055511,489482566,337546047,98982625,864171722,393178217,542434301,616517733,444190170,245921995,919230153,969795820,132691982,790485595,742625230,19309225,384244825,256151540,37808500,135974600,728044427,661122182,591010675,868266,325106029,149833465,914477926,421758533,120333166,39204599,954975822,288932677,137984444,176665131,725583205,264162972,458476388,292634998,265009433,389552917,656381235,359176597,938508826,191867792,963266161,56877706,826808061,293459671,909491228,492435644,418061378,855311521,428720788,899501973,957503054,895433393,29554373,552970302,256387888,323768233,998652754,223540988,856872939,768331790,896412830,377903138,871206288,663043497,282793022,184324864,130862536,116336592,244860065,163648402,929782816,955518127,343203297,7539992,303581952,976513088,825774409,783333338};
int n, m, T;
ll qp(ll a, ll b) {
    ll ans = 1;
    while(b) {
        if(b & 1) ans = ans * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return ans;
}
int calc(int k) {
    int tmp = k / base;
    int ans = biao[tmp];
    for(int i = base * tmp + 1; i <= k; i++) {
        ans = (ans + 1ll * (i - 1) * qp(1ll * (i + 1) * (i + 2) % MOD, MOD - 2) % MOD) % MOD;
    }
    return ans;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> T;
    while(T--) {
        cin >> n >> m;
        if(n <= m) {
            cout << 0 << '\n';
            continue;
        }
        int ans = ((calc(n - 1) - calc(m)) % MOD + MOD ) % MOD;
        ans = 1ll * ans * (n + 1) % MOD * n % MOD;
        ans = (n - 1 + 2ll * ans * qp(n, MOD - 2) % MOD) % MOD;
        cout << ans << '\n';
    }
    return 0;
}

B.Rikka with Cake

题意:
现在有一块大小为\(n*m\)的蛋糕,并且会切\(k\)刀,对于每一刀,都会有\((x,y,d)\)来表示起点位于\((x,y)\),方向为\(d\)一直到边界的一刀。
现在问最后蛋糕会被分为多少块。

思路:
手玩一下样例即可发现答案为交点数\(+1\)
扫描线+树状数组统计即可。

Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int T;
int n, m, k;
int b[N], c[N];
struct point{
    int x, y;
    char d;
}a[N];
void Hash() {
    sort(b + 1, b + b[0] + 1);
    sort(c + 1, c + c[0] + 1);
    b[0] = unique(b + 1, b + b[0] + 1) - b - 1;
    c[0] = unique(c + 1, c + c[0] + 1) - c - 1;
    for(int i = 1; i <= k; i++) {
        a[i].y = lower_bound(b + 1, b + b[0] + 1, a[i].y) - b;
        a[i].x = lower_bound(c + 1, c + c[0] + 1, a[i].x) - c;
    }
}
int p[N];
int lowbit(int x) {
    return x & -x;
}
void upd(int x, int v) {
    for(; x < N; x += lowbit(x)) p[x] += v;
}
int query(int l, int r) {
    if(l > r) return 0;
    int ans = 0;
    for(int i = r; i; i -= lowbit(i)) ans += p[i];
    for(int i = l; i; i -= lowbit(i)) ans -= p[i];
    return ans;
}
vector <int> v[N], add[N], del[N];
int L[N], R[N], U[N], D[N];
int main() {
    cin >> T;
    while(T--) {
        scanf("%d%d%d", &n, &m, &k);
        b[0] = c[0] = 0;
        for(int i = 1; i <= k; i++) {
            int x, y; char ch;
            scanf("%d %d %c", &a[i].x, &a[i].y, &a[i].d);
            b[++b[0]] = a[i].y; c[++c[0]] = a[i].x;
        }
        Hash();
        for(int i = 1; i <= c[0]; i++) add[i].clear(), del[i].clear(), v[i].clear(), U[i] = N, D[i] = 0;
        for(int i = 1; i <= b[0]; i++) L[i] = 0, R[i] = N;
        ll ans = 1;
        for(int i = 1; i <= k; i++) {
            if(a[i].d == 'L') {
                L[a[i].y] = max(L[a[i].y], a[i].x);
            }
            if(a[i].d == 'R') {
                R[a[i].y] = min(R[a[i].y], a[i].x);
            }
            if(a[i].d == 'U') {
                U[a[i].x] = min(U[a[i].x], a[i].y);
            }
            if(a[i].d == 'D') {
                D[a[i].x] = max(D[a[i].x], a[i].y);
            }
        }
        for(int i = 1; i <= k; i++) {
            if(a[i].d == 'U' || a[i].d == 'D') v[a[i].x].push_back(i);
            else {
                if(a[i].d == 'L') {
                    if(a[i].x == L[a[i].y]) {
                        add[1].push_back(i);
                        del[a[i].x].push_back(i);
                    }
                } else {
                    if(a[i].x == R[a[i].y]) {
                        add[a[i].x].push_back(i);
                        del[c[0]].push_back(i);
                    }
                }
            }
        }
        for(int i = 1; i <= c[0]; i++) {
            for(auto it : add[i]) {
                upd(a[it].y, 1);
            }
            for(auto it : v[i]) {
                char d = a[it].d;
                if(d == 'U' && a[it].y == U[i]) {
                    ans += query(a[it].y - 1, N - 1);//[y,N-1]
                } else if(d == 'D' && a[it].y == D[i]){
                    ans += query(0, a[it].y);//[1,y]
                }
            }
             for(auto it : del[i]) {
                upd(a[it].y, -1);
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

C.Rikka with Mista

题意:
给出\(n\)个数\((n\leq 40)\),每个数都有取或者不取两种情况,之后将取了的数加起来。
现在问所有的\(2^n\)种情况中,\(4\)出现的次数为多少。

思路:

  • 看到数据范围,考虑meet in the middle,折半枚举情况。
  • 直接算显然是不可能的,考虑每一位的贡献。
  • 怎么考虑?
  • 取模运算即可。对于\(a_i,b_j\),如果\((a_i+b_j)\% 10^k=4\),那么它们在\(k\)位有贡献。
  • 容易想到要对后缀排序,之后就能二分加速,或者直接two pointer。
  • 每次排序显然复杂度过高,因为到我们是按位考虑贡献的,所以...
  • 基数排序!每次可以优化一个\(log\)

总时间复杂度为\(O(100n)\)

Code
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
const int N = 45, MAX = (1 << 20) + 5;
int T, n;
int a[N];
vector <pli> x, y, A[10], B[10];
void solve(int pos, int r, ll sum, int op) {
    if(pos == r) {
        if(op) x.push_back(MP(sum, 0));
        else y.push_back(MP(sum, 0));
        return;
    }
    solve(pos + 1, r, sum, op);
    solve(pos + 1, r, sum + a[pos + 1], op);
}
ll work1(int k1, int k2, ll lim) {
    ll res = 0;
    int now = 0, sz = B[k2].size();
    for(int i = A[k1].size() - 1; i >= 0; i--) {
        while(now < sz && A[k1][i].se + B[k2][now].se < lim) now++;
        res += now;
    }
    return res;
}
ll work2(int k1, int k2, ll lim) {
    ll res = 0;
    int sz = B[k2].size(), now = sz - 1;
    for(int i = 0; i < A[k1].size(); i++) {
        while(now >= 0 && B[k2][now].se + A[k1][i].se >= lim) now--;
        res += sz - now - 1;
    }
    return res;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> T;
    while(T--) {
        cin >> n;
        for(int i = 1; i <= n; i++) cin >> a[i];
        int mid = n / 2;
        x.clear(), y.clear();
        solve(0, mid, 0, 0); solve(mid, n, 0, 1);
        ll base = 1, ans = 0;
        for(int t = 1; t <= 11; t++) {
            for(int i = 0; i < 10; i++) A[i].clear(), B[i].clear();
            for(auto it : x) A[it.fi % 10].push_back(MP(it.fi / 10, it.se));
            for(auto it : y) B[it.fi % 10].push_back(MP(it.fi / 10, it.se));
            for(int i = 0; i < 10; i++) {
                int k1 = (14 - i) % 10, k2 = (13 - i) % 10;
                ans += work1(i, k1, base);
                ans += work2(i, k2, base);
            }
            int now = 0;
            for(int i = 0; i < 10; i++) {
                ll tmp = i * base;
                for(auto it : A[i]) {
                    x[now++] = MP(it.fi, tmp + it.se);
                }
            }
            now = 0;
            for(int i = 0; i < 10; i++) {
                ll tmp = i * base;
                for(auto it : B[i]) {
                    y[now++] = MP(it.fi, tmp + it.se);
                }
            }
            base *= 10;
        }
        cout << ans << '\n';
    }
    return 0;
}

E.Rikka with Game

题意:
给出一个字符串,两个人在玩博弈游戏。两个人依次执行操作,要么中止游戏,要么将某个数往后移动一位:\(a->b,\cdots,z->a\)
现在第一个人想要字符串最小,而第二个人想要字符串最大。
问最终局面是什么。

思路:
贪心考虑:

  • 对于第一个人来说,目前局面不存在\(z\)或者在第一个\(z\)之前有某个字符小于\(y\),那么他就应该立即中止游戏;
  • 同理,对于第二个人来说,如果目前字符全不小于\(y\),或者在一个较小字符前面存在\(z\),那么他就应该立即终止游戏;
  • 其余情况,贪心考虑即可。

代码如下:

Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
const int N = 105;
char s[N];
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> T;
    while(T--) {
        cin >> s + 1;
        int f = 0;
        int n = strlen(s + 1);
        while(1) {
            f = 0;
            for(int i = 1; i <= n; i++) {
                if(s[i] == 'z') {
                    s[i] = 'a';
                    f = 1; break;
                } else if(s[i] < 'y') break;
            }
            if(!f) {
                cout << s + 1 << '\n';
                break;
            }
            for(int i = 1; i <= n; i++) {
                if(s[i] < 'y') {
                    s[i]++;
                    f = 2;
                    break;
                } else if(s[i] == 'z') {
                    break;
                }
            }
            if(f < 2) {
                cout << s + 1 << '\n';
                break;
            }
        }
    }
    return 0;
}

F.Rikka with Coins

题意:
现在有面值为\(10,20,50,100\)的纸币,每种都有无限多张。
现在有\(n\)个物品,每种物品都有价值\(a_i\),问最少带多少张纸币,对于每个物品都能刚好给够。

思路:
简单分析就会发现:

  • \(10\)元纸币最多一张;
  • \(20\)元纸币最多四张;
  • \(50\)元纸币最多一张。

那么就直接暴力枚举前三种面额多少张即可,\(100\)元纸币的情况可以直接计算出来。

Code
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 105;
int T, n;
int w[N];
int main() {
    cin >> T;
    while(T--) {
        cin >> n;
        for(int i = 1; i <= n; i++) cin >> w[i];
        int ans = INF;
        for(int i = 1; i <= n; i++) {
            if(w[i] % 10 != 0) ans = -1;
        }
        if(ans == -1) {
            cout << ans << '\n';
            continue;
        }
        int f;
        for(int a = 0; a <= 1; a++) {
            for(int b = 0; b <= 4; b++) {
                for(int c = 0; c <= 1; c++) {
                    int sum, mx, tmp = 0;
                    for(int i = 1; i <= n; i++) {
                        f = 1; mx = INF;
                        for(int aa = 0; aa <= a; aa++) {
                            for(int bb = 0; bb <= b; bb++) {
                                for(int cc = 0; cc <= c; cc++) {
                                    sum = aa * 10 + bb * 20 + cc * 50;
                                    if(w[i] >= sum && (w[i] - sum) % 100 == 0) {
                                        mx = min(mx, (w[i] - sum) / 100);
                                    }
                                }
                            }
                        }
                        if(mx == INF) {
                            f = 0; break;
                        } else tmp = max(tmp, mx);
                    }
                    if(f) ans = min(ans, a + b + c + tmp);
                }
            }
        }
        if(ans == INF) ans = -1;
        cout << ans << '\n';
    }
    return 0;
}

G.Rikka with Travels

题意:
定义\(L(a,b)\)为一棵树上从\(a\)\(b\)经过的点的数量。
求出所有的点对\((L(a,b),L(c,d))\)并且满足\(a->b\)不与\(c->d\)相交。

思路:
比较直接的想法就是换根树\(dp\)
另外还有比较巧妙的想法:树的直径。
因为树的直径具有最长性,那么:

  • 对于点对\((a,b)\),如果\(a\)没在直径上,那么\(b\)一定是整个直径;
  • 如果路径\(a,b\)都在直径上面,最优情况肯定是它们的一端都在直径的某一端(最长性);
  • 如果都没在直径上面,这种情况一定可以被前两种情况覆盖。

形象来说,就是考虑如下两种情况:

有一点需要注意的是,最后得到答案的时候要倒着扫一遍才能得到所有的\(ans\)
详见代码:

Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int T;
int n;
struct Edge{
    int v, next;
}e[N << 1];
int head[N], tot;
void adde(int u, int v) {
    e[tot].v = v; e[tot].next = head[u]; head[u] = tot++;
}
int d[N], fa[N];
void dfs(int u, int pa) {
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(v == pa) continue;
        d[v] = d[u] + 1; fa[v] = u;
        dfs(v, u);
    }
}
bool chk[N];
int f[N], g[N];
void dfs2(int u, int pa) {
    f[u] = g[u] = 1;
    for(int i = head[u]; i != -1; i = e[i].next) {
        int v = e[i].v;
        if(v == pa || chk[v]) continue;
        dfs2(v, u);
        g[u] = max(g[u], f[u] + f[v]);
        f[u] = max(f[u], f[v] + 1);
    }
}
int L[N], R[N], ans[N];
vector <int> l;
void upd(int k1, int k2) {
    ans[k1] = max(ans[k1], k2);
    ans[k2] = max(ans[k2], k1);
}
void work1() {
    int sz = l.size();
    for(int i = 0; i < sz; i++) {
        L[i] = f[l[i]] + i;
        R[i] = sz - i + f[l[i]] - 1;
    }
    for(int i = 1; i < sz; i++) L[i] = max(L[i], L[i - 1]);
    for(int i = 1; i < sz; i++) {
        upd(L[i - 1], R[i]);
    }
}
void work2() {
    for(int i = 1; i <= n; i++) {
        if(!chk[i]) upd(g[i], (int)l.size());
    }
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> T;
    while(T--) {
        memset(head, -1, sizeof(head)); tot = 0;
        cin >> n;
        for(int i = 1; i < n; i++) {
            int u, v; cin >> u >> v;
            adde(u, v); adde(v, u);
        }
        d[1] = 0; dfs(1, 0);
        int u = 1;
        for(int i = 1; i <= n; i++) if(d[i] > d[u]) u = i;
        d[u] = 0; dfs(u, 0);
        int v = u;
        for(int i = 1; i <= n; i++) if(d[i] > d[v]) v = i;
        memset(chk, 0, sizeof(chk));
        chk[u] = 1; l.clear();
        for(int i = v; i != u; i = fa[i]) chk[i] = 1, l.push_back(i);
        l.push_back(u);
        for(int i = 1; i <= n; i++) if(chk[i]) dfs2(i, 0);
        work1(); work2();
        for(int i = n - 1; i >= 1; i--) ans[i] = max(ans[i], ans[i + 1]);
        ll res = 0;
        for(int i = 1; i <= n; i++) res += ans[i], ans[i] = 0;
        cout << res << '\n';
    }
    return 0;
}

H.Rikka with Stable Marriage

题意:
给出\(a_i\)\(b_i\),求其两两匹配异或和的最大值。

思路:
其实原题求的是稳定婚姻匹配问题,但可以将问题转化为这个。
因为贪心匹配过后,就不能再交换伴侣了,剩下的在一起也只能这样了。想找更优秀的也没法= =优秀的可能早就和别人在一起了。就算优秀的跑来找你,那另一个人也会剩下来,所以何必呢。(口胡)
解法就类似于传送门中的\(B\)题。

Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 2;
int T, n;
int a[N], b[N];
inline char gc() {
    static char buf[131072], *p1, *p2;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 131072, stdin), p1 == p2) ? EOF : *p1++;
}
int read() {
    int x = 0;
    char c = gc();
    for (; c < 48 || c > 57; c = gc())
        ;
    for (; c > 47 && c < 58; x = x * 10 + c - 48, c = gc())
        ;
    return x;
}
struct Trie{
    int ch[N * 30][2], cnt[N * 30];
    int tot;
    void init() {
        tot = 0;
        ch[tot][0] = ch[tot][1] = 0;
    }
    int New_node() {
        ch[++tot][0] = ch[tot][1] = 0;
        return tot;
    }
    void Insert(int x) {
        int u = 0;
        for(register int i = 29; ~i; --i) {
            int p = ((x >> i & 1) ? 1 : 0);
            if(!ch[u][p]) ch[u][p] = New_node();
            u = ch[u][p];
            ++cnt[u];
        }
    }
}t1, t2;
ll work() {
    int u1, u2;
    ll ans = 0;
    while(n--) {
        u1 = u2 = 0;
        int x = 0;
        for(register int p = 29; ~p; --p) {
            if(t1.cnt[t1.ch[u1][0]] && t2.cnt[t2.ch[u2][1]]) {
                --t1.cnt[t1.ch[u1][0]];
                --t2.cnt[t2.ch[u2][1]];
                u1 = t1.ch[u1][0]; u2 = t2.ch[u2][1];
                x ^= (1 << p);
            }else if(t1.cnt[t1.ch[u1][1]] && t2.cnt[t2.ch[u2][0]]){
                --t1.cnt[t1.ch[u1][1]];
                --t2.cnt[t2.ch[u2][0]];
                u1 = t1.ch[u1][1]; u2 = t2.ch[u2][0];
                x ^= (1 << p);
            }else if(t1.cnt[t1.ch[u1][0]] && t2.cnt[t2.ch[u2][0]]) {
                --t1.cnt[t1.ch[u1][0]];
                --t2.cnt[t2.ch[u2][0]];
                u1 = t1.ch[u1][0]; u2 = t2.ch[u2][0];
            } else {
                --t1.cnt[t1.ch[u1][1]];
                --t2.cnt[t2.ch[u2][1]];
                u1 = t1.ch[u1][1]; u2 = t2.ch[u2][1];
            }
        }
        ans += x;
    }
    return ans;
}
int main() {
    //ios::sync_with_stdio(false); cin.tie(0);
    T = read();int i;
    while(T--) {
        n = read();
        for(i = 1; i <= n; ++i) a[i] = read();
        for(i = 1; i <= n; ++i) b[i] = read();
        t1.init(); t2.init();
        for(i = 1; i <= n; ++i) t1.Insert(a[i]);
        for(i = 1; i <= n; ++i) t2.Insert(b[i]);
        printf("%lld\n", work());
    }
    return 0;
}

P.S:dfs版本感觉写起来挺容易的,就是时间稍微有点慢,也放出来吧:

Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int T, n;
int a[N], b[N];
int tot;
int rt[2], ls[N * 70], rs[N * 70], cnt[N * 70];
int newnode() {
    ls[++tot] = rs[tot] = cnt[tot] = 0;
    return tot;
}
void insert(int &o, int v, int d) {
    if(!o) o = newnode();
    cnt[o]++;
    if(d < 0) return;
    if(v >> d & 1) {
        insert(rs[o], v, d - 1);
    } else insert(ls[o], v, d - 1);
}
ll query(int o1, int o2, int d, int w) {
    if(!cnt[o1] || !cnt[o2]) return 0;
    ll res = 0;
    if(d == -1) {
        int num = min(cnt[o1], cnt[o2]);
        cnt[o1] -= num; cnt[o2] -= num;
        res += 1ll * w * num;
        return res;
    }
    res = query(ls[o1], rs[o2], d - 1, w + (1 << d)) + query(rs[o1], ls[o2], d - 1, w + (1 << d));
    res += query(ls[o1], ls[o2], d - 1, w) + query(rs[o1], rs[o2], d - 1, w);
    cnt[o1] = cnt[rs[o1]] + cnt[ls[o1]];
    cnt[o2] = cnt[rs[o2]] + cnt[ls[o2]];
    return res;
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> T;
    while(T--) {
        cin >> n;
        for(int i = 1; i <= n; i++) cin >> a[i];
        for(int i = 1; i <= n; i++) cin >> b[i];
        rt[0] = rt[1] = tot = 0;
        for(int i = 1; i <= n; i++) insert(rt[0], a[i], 31);
        for(int i = 1; i <= n; i++) insert(rt[1], b[i], 31);
        cout << query(rt[0], rt[1], 31, 0) << '\n';
    }
    return 0;
}
posted @ 2019-08-20 19:30  heyuhhh  阅读(409)  评论(0编辑  收藏  举报