【Foreign】数数 [打表][DP]
数数
Time Limit: 10 Sec Memory Limit: 128 MBDescription
Input
仅一行两个整数L,R
Output
仅一行一个整数表示答案。
Sample Input
9 28
Sample Output
2
HINT
L,R <= 10^9,代码长度限制10k。
Source
我们首先想到了如何判断一个数字是否可行,考虑到了一个DP,令 f[i][j] 表示前 i 位,其中一个集合和为 j 的方案,那么 f[i+1][j] 可以转移到 f[i][j+a[i]] 或 f[i+1][j],这样做就可以把 j 倒着跑,省去 i 这一维,又由于跑不满,判断很快,可以过100w左右的点。
然后我们就考虑用这个判断来打表,每隔100w输出一个表,无脑记录前缀和,这样做发现代码>10k,于是我们一波计算,决定每隔110w打一个表,成功解决该题。
(代码长度限制是最不资磁的!但是打表是坠妙的!)
Code
1 #include<iostream>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstring>
5 #include<cstdlib>
6 #include<cmath>
7 using namespace std;
8 #define F(a,l,b) for(a=l;a<=b;a++)
9 int O=1100000,PD,L,R,y,i,j,u,S,N,T,a[12];
10 bool f[83];
11 int A[]={0,417135,921196,1425525,1926975,2423909,2917309,3401276,3872306,4331719,4823015,5352689,5894945,6437883,6978430,7516599,8049356,8573184,9092000,9594778,10130918,10658563,11200581,11736837,12274031,12806029,13333390,13857060,14363295,14896960,15438658,15960968,16500679,17037082,17565866,18098734,18623728,19128643,19656534,20192492,20731414,21245673,21781052,22315272,22845867,23365117,23872020,24395840,24934809,25472688,26006639,26526949,27062648,27593322,28118614,28624452,29137846,29669869,30199691,30731746,31267680,31772810,32299965,32821899,33324971,33827744,34355569,34887771,35420514,35952223,36479854,36972119,37489205,37989663,38478643,39001274,39528965,40052923,40579765,41099794,41617398,42092876,42592046,43068754,43589544,44110251,44629148,45149912,45667893,46182485,46689616,47152129,47661051,48195992,48738719,49280613,49820868,50358260,50888139,51408801,51926786,52456460,52999692,53547584,54096689,54645189,55192450,55736130,56274694,56811168,57350715,57897490,58446794,58996667,59546575,60096069,60644314,61190387,61734851,62277428,62825256,63374376,63922347,64471716,65020424,65568716,66116836,66662833,67205163,67751655,68301368,68849106,69394298,69942443,70491626,71038425,71583548,72125327,72670941,73220387,73769968,74316166,74862538,75410556,75958312,76504125,77044901,77585553,78133332,78681501,79230464,79776270,80318375,80862363,81406961,81945232,82480015,83024038,83571628,84120211,84668372,85208190,85742095,86280792,86815075,87343421,87884503,88430397,88976334,89522375,90066835,90598395,91123717,91653061,92180265,92721398,93266487,93811099,94356027,94900328,95441662,95970330,96489104,97003435,97540755,98075404,98614365,99152512,99686888,100216596,100741300,101262167,101805399,102353291,102902702,103452575,104002467,104551830,105099513,105644497,106188552,106717883,107264345,107804017,108349397,108892112,109435409,109977980,110517526,111056988,111597125,112146505,112695033,113242921,113792152,114340302,114888232,115435975,115982063,116519335,117067923,117607996,118156061,118698940,119245227,119789180,120333055,120877199,121417198,121964769,122511630,123059909,123607967,124154768,124702079,125247948,125793867,126330507,126875384,127416896,127964407,128506057,129052907,129593317,130137960,130679890,131216971,131756296,132299463,132846412,133393411,133937387,134481410,135018650,135560474,136092313,136629027,137168994,137714953,138256343,138803944,139342680,139883113,140415625,140950056,141482335,142023629,142565980,143110960,143655744,144199732,144735473,145273212,145791589,146308068,146851180,147383869,147916177,148454578,148984640,149517152,150041668,150567365,151112659,151662070,152210739,152759084,153308559,153856817,154405172,154952535,155496101,156040961,156590357,157138373,157686864,158235869,158783717,159331694,159879211,160424682,160948968,161496438,162038787,162572148,163118393,163656066,164193830,164737021,165270083,165806545,166355718,166904677,167450863,167997136,168545310,169092400,169638630,170184012,170722751,171271000,171818383,172364924,172912514,173458326,174006100,174551535,175096355,175629017,176172405,176719660,177249913,177797509,178340590,178875500,179422043,179960223,180493005,181036892,181585212,182130169,182676509,183224488,183768974,184311663,184857544,185393607,185934316,186481651,187024005,187569465,188112656,188658618,189201225,189738034,190270841,190804215,191349265,191883438,192425599,192969700,193499764,194044065,194581753,195095087,195610326,196148883,196687187,197210409,197739928,198272822,198802115,199323521,199850870,200394579,200943851,201493380,202039640,202585980,203134697,203683416,204227733,204771232,205311381,205857354,206401422,206946272,207492320,208035528,208581865,209123355,209665946,210208615,210758043,211306431,211851888,212399190,212947139,213493810,214039765,214584925,215101385,215645657,216187395,216724407,217253376,217794438,218336843,218866454,219399579,219932026,220479189,221028202,221575026,222117342,222662644,223210807,223756495,224297118,224830643,225378825,225920240,226466634,227009279,227553855,228096725,228641311,229185056,229721524,230267511,230814810,231359910,231904592,232452600,232997270,233538986,234084222,234613686,235152376,235693160,236239191,236763216,237307744,237849988,238388277,238919180,239448861,239984700,240529566,241074762,241610353,242148052,242693296,243236954,243771749,244283171,244801315,245341074,245879098,246414272,246935264,247468037,247998865,248524713,249052474,249595787,250144771,250694463,251242630,251788437,252335686,252882884,253429196,253971982,254515583,255064043,255611495,256160505,256706584,257255169,257799856,258345975,258889039,259430922,259980026,260527203,261074321,261620674,262167392,262714961,263259139,263800745,264338882,264886842,265435946,265981103,266523123,267070031,267618054,268163053,268701319,269223901,269769784,270315255,270860067,271397028,271934373,272478790,273020755,273559060,274093440,274641038,275189801,275737315,276284734,276828704,277373444,277920051,278465652,278998810,279545055,280089207,280637303,281183370,281728408,282270543,282811303,283355708,283887956,284429201,284976467,285519350,286062998,286606084,287152736,287692419,288230333,288759385,289296220,289840664,290384616,290920421,291459927,292003847,292547195,293080313,293586959,294101773,294635128,295164599,295696523,296225206,296737047,297263791,297784117,298311280,298851777,299399131,299947174,300496200,301044069,301584657,302128160,302673128,303213323,303753961,304298400,304843286,305386338,305933964,306472648,307018031,307559024,308100191,308633999,309181829,309720579,310259907,310807337,311341795,311885102,312427159,312958907,313498096,314044588,314588675,315132290,315677809,316218550,316765198,317307067,317849058,318388984,318937414,319485606,320033632,320579960,321122581,321668729,322215596,322760681,323268741,323810635,324347160,324878793,325418960,325944684,326471389,327007682,327539305,328065016,328607934,329155553,329702947,330251010,330795015,331332363,331870810,332412769,332937147,333479831,334018862,334564280,335106711,335649069,336187992,336722514,337258540,337783121,338320936,338865048,339395297,339940651,340482130,341015203,341558278,342089278,342587050,343096916,343625005,344158186,344688785,345218938,345736962,346242038,346758510,347281236,347816030,348360113,348908128,349456836,350003180,350543188,351078194,351617341,352154177,352692132,353236598,353782122,354329769,354873891,355417669,355954484,356496482,357032964,357574754,358123248,358669229,359214970,359763020,360309992,360849513,361396015,361937175,362476905,363024965,363571737,364116460,364662343,365210648,365750358,366294429,366835420,367372321,367919052,368465241,369013056,369557931,370103365,370640823,371185902,371723827,372256004,372801148,373348413,373896214,374444299,374985823,375520522,376061228,376604382,377099492,377633637,378169537,378708961,379246966,379782470,380295030,380812915,381341814,381858362,382394166,382939485,383486319,384031862,384576739,385113618,385639719,386170713,386688953,387225366,387761476,388306143,388849436,389392358,389929928,390463802,390991357,391476240,391981184,392505333,393030392,393553377,394078994,394600414,395099788,395596807,396114052,396643944,397185227,397731855,398276631,398822628,399364301,399896301,400424875,400954779,401492143,402032110,402577520,403119584,403664957,404205395,404744246,405278790,405815553,406354621,406901956,407445871,407989949,408534106,409079047,409622203,410158908,410695521,411226112,411769688,412313995,412844637,413383525,413924768,414465277,414994263,415531988,416066905,416614039,417159248,417703791,418245977,418790724,419337098,419868649,420406564,420935930,421478913,422021124,422564234,423108976,423647176,424187315,424716013,425251647,425773479,426313255,426859086,427404830,427951287,428495753,429027901,429550682,430085916,430563636,431088517,431618412,432149120,432675796,433206436,433733025,434224613,434733023,435242599,435775424,436318558,436862299,437404608,437947050,438487483,439016301,439535995,440006606,440513489,441030131,441550217,442070862,442588713,443106159,443619290,444100813,444605003,445136305,445678354,446222495,446767198,447311991,447855357,448394200,448921724,449444009,449980085,450519779,451062523,451606254,452151102,452692943,453232422,453765968,454287678,454821888,455367134,455901036,456442551,456984032,457517856,458062157,458596096,459117387,459650425,460195876,460740603,461278848,461814988,462358955,462902613,463438624,463960866,464491068,465035642,465580193,466121161,466658276,467200660,467741751,468279757,468801744,469328574,469873471,470411392,470949639,471494837,472028017,472567860,473104159,473623377,474146190,474686713,475227085,475771318,476314917,476857401,477389075,477923878,478439065,478954763,479492000,480035598,480578351,481120904,481663904,482203213,482725580,483243978,483709766,484232528,484755717,485277065,485798555,486320719,486842248,487363131,487840732};
12 int C(int x){for(N=T=0;x;)T+=a[++N]=x%10,x/=10;if(T&1)return 0;memset(f,0,sizeof(f));f[0]=1;T>>=1;F(i,0,N-1)for(int j=T-a[i];j>=0;j--){if(f[j]) f[j+a[i]]=1;if(f[T]) return 1;}return 0;}
13 int D(int x){y=x/O;S=A[y];F(u,y*O+1,x)S+=C(u);return S;}
14 int main(){;scanf("%d%d",&L,&R);printf("%d",D(R)-D(L-1));}