【分块打表】bzoj 3758 数数

【题目描述】

Description

神犇最近闲来无事,于是就思考哲学,研究数字之美。在神犇看来,如果一个数的各位能够被分成两个集合,而且这两个集合里的数的和相等,那么这个数就是优美的(具体原因就只有神犇才知道了)。现在神犇在思考另一个问题,在区间[a,b]中有多少个数是优美的?这个问题对于神犇来说很简单,相信对于你来说也不难。 
Input

输入只有一行,包含两个整数a和b。 
Output

输出只有一行,包含一个整数,代表区间[a,b]中优美的数的个数。

Sample Input

1 11 
Sample Output


HINT

1<=A<=B<=10^9

【思路】

  • 对于每个数x,可以log(x)复杂度内(二进制背包问题,状压,long long f初始化为1,f|=f<<d,d是x的每一位数)求出这个数是不是完美数
  • 对于[a,b]内有多少个完美数,可以由前缀和sum[a]-sum[b-1]得出
  • 考虑到a=1,b=1e9时不能在线算,所以要打表预处理
  • 考虑到打表要打1e9,显然不允许
  • 因为要求的是前缀和,所以我们可以分块求和,整块的由表得出,最右边不在整块的暴力(前面已经说了单个复杂度很小)
  • 综合考虑时间和空间,1e9个数分成1e3块(要考虑到编译超时的问题?),空间1e3可以,时间1e6logx也可以

 【AC】

http://blog.csdn.net/PoPoQQQ/article/details/41551913

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<vector>
 9 
10 using namespace std;
11 typedef long long ll;
12 const int maxn=1e9;
13 int table[1003];
14 bool check(int x)
15 {
16     int sum=0;
17     for(int i=x;i;i/=10)
18     {
19         sum+=i%10;
20     }
21     if(sum&1) return false;
22     sum>>=1;
23     ll f=1ll;
24     for(int i=x;i;i/=10)
25     {
26         f|=f<<(i%10);
27     }
28     return f&(1<<sum);
29 }
30 int main()
31 {
32     freopen("data.txt","w",stdout);
33     memset(table,0,sizeof(table));
34     int cnt=1;
35     for(int i=1;i<=maxn;i++)
36     {
37         if(check(i))
38         {
39             table[cnt]++;
40         }
41         if(i%1000000==0)
42         {
43             cnt++;
44             table[cnt]=table[cnt-1];
45         }
46     }
47     for(int i=1;i<=1000;i++)
48     {
49         printf("%d,",table[i]);
50     }
51     return 0;
52 }
本地打表
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<vector>
 9 
10 using namespace std;
11 typedef long long ll;
12 const int maxn=1e9;
13 int table[]={0,376413,832547,1288828,1744956,2196800,2647716,3090920,3526440,3951373,4366887,4823022,5304773,5797151,6290679,6782011,7272537,7758917,8238404,8710550,9182294,9638575,10130954,10610611,11103565,11590781,12080549,12565131,13045259,13523168,13996174,14452302,14945830,15438785,15913732,16404034,16892855,17372077,17858463,18339343,18811084,19262928,19754260,20241476,20731779,21199764,21685062,22170839,22655897,23129390,23601780,24052696,24543222,25032990,25521811,26007110,26480989,26968221,27450848,27929964,28401878,28845082,29331462,29816044,30295266,30781043,31268276,31728520,32208082,32682169,33151885,33587405,34066892,34547020,35033406,35518464,36001091,36480654,36929715,37399425,37866198,38291131,38763277,39241186,39722066,40195559,40674675,41148762,41618473,42052010,42515564,42931078,43402822,43875828,44347569,44819959,45291873,45761589,46228362,46691917,47114684,47570819,48052570,48544948,49038476,49529808,50020334,50506714,50986201,51458347,51930092,52411843,52904222,53401323,53900407,54399282,54897570,55393683,55885467,56373459,56861733,57354112,57851213,58350298,58850079,59350047,59849892,60348950,60846162,61341250,61836279,62329807,62828892,63328673,63826254,64325517,64825113,65323004,65821802,66319294,66814940,67306272,67805147,68305116,68804379,69298934,69796423,70295897,70794698,71290464,71785496,72276022,72774310,73274155,73773752,74271241,74767192,75265147,75763323,76260402,76755388,77241768,77737881,78236939,78734830,79234305,79732260,80224687,80718415,81213225,81708052,82187539,82679323,83176535,83675333,84174134,84672311,85166039,85650688,86137969,86630665,87102811,87590803,88085891,88583383,89079149,89576228,90071039,90558320,91033697,91518754,91990498,92478772,92973801,93469447,93964479,94459465,94954292,95446989,95932046,96407164,96863445,97355824,97835481,98328435,98815651,99305419,99790001,100270129,100748038,101221044,101713423,102210524,102709609,103209390,103709358,104209203,104708261,105205473,105700561,106195591,106675248,107174332,107661548,108160447,108650610,109147486,109638976,110131980,110622168,111111314,111604269,112104050,112602948,113100172,113599955,114098566,114596338,115094596,115592360,116087995,116575211,117075180,117565343,118065125,118556615,119055700,119547706,120045020,120537157,121032371,121522139,122021984,122518861,123017472,123516556,124012409,124511572,125006400,125503960,125998776,126483358,126982416,127473906,127971679,128463685,128962847,129451634,129949211,130439289,130934527,131414655,131911867,132404871,132903129,133400444,133895272,134392848,134877835,135372590,135860157,136338066,136833154,137323342,137821106,138313243,138810804,139300882,139795636,140274932,140767838,141240844,141735873,142225019,142720654,143215868,143710684,144205923,144693490,145186396,145661085,146117213,146610741,147103696,147578643,148068945,148557766,149036988,149523374,150004254,150475995,150969523,151468608,151968389,152465970,152965233,153464829,153962720,154461518,154959010,155454656,155947611,156447392,156946290,157443514,157943297,158441908,158939680,159437938,159935702,160431338,160906285,161403865,161901089,162380311,162877143,163372866,163853746,164349018,164841568,165323060,165813363,166312626,166812408,167309240,167804358,168303264,168801031,169297990,169793980,170289262,170778083,171277680,171776291,172272013,172770919,173266383,173763638,174261745,174755993,175251151,175730373,176228264,176726037,177206917,177704683,178201938,178683430,179180453,179676122,180157572,180643958,181142756,181641014,182136287,182633246,183131352,183628375,184118597,184615149,185109199,185590079,186087571,186585335,187077885,187573876,188068124,188563792,189060344,189543474,190036480,190508221,191003867,191499502,191980994,192476276,192971435,193452885,193946935,194439941,194912165,195364009,195855341,196342557,196832860,197300845,197786143,198271920,198756978,199230471,199702861,200194193,200693068,201193037,201692300,202186855,202684344,203183818,203682619,204178385,204673417,205160633,205660602,206150765,206650547,207142037,207641122,208133128,208630442,209122579,209617793,210108096,210607359,211107141,211603973,212099091,212597997,213095764,213592723,214088713,214583996,215051981,215546535,216038025,216533143,217006636,217498838,217990001,218483454,218958939,219445651,219930950,220428439,220927523,221426429,221918631,222412162,222910868,223408636,223903111,224391693,224877470,225376945,225868951,226366717,226857880,227356586,227845406,228343740,228835129,229329711,229814769,230313570,230810885,231307844,231801296,232299064,232797398,233286744,233781326,234274205,234747698,235243464,235735601,236231592,236707077,237201551,237692940,238187522,238662418,239152212,239624602,240119634,240614848,241110130,241596843,242085425,242580007,243072886,243562680,244033987,244484903,244975429,245465197,245954018,246439317,246913196,247400428,247883055,248362171,248834085,249324611,249822899,250322744,250822341,251319830,251815781,252313736,252811912,253308991,253803977,254293745,254793590,255290467,255789078,256288162,256784015,257283178,257778006,258275566,258770382,259259203,259758800,260257411,260753133,261252039,261747503,262244758,262742865,263237113,263732271,264217570,264715059,265214143,265713049,266205251,266698782,267197488,267695256,268189731,268678314,269152193,269648143,270143996,270639460,271132991,271614409,272109875,272603279,273094508,273581598,274068831,274566786,275065948,275563203,276061909,276557375,277049723,277546475,278042383,278535969,279018596,279516773,280011601,280509707,281007475,281500879,281997631,282483155,282977906,283468360,283947476,284444555,284942116,285436364,285930838,286422067,286917975,287412726,287893746,288382874,288854788,289349774,289844590,290339749,290828331,291315421,291809007,292299461,292788589,293258019,293701223,294187603,294672185,295151407,295637184,296124417,296584661,297064223,297538310,298008026,298494406,298990519,299489577,299987468,300486943,300984898,301477325,301971053,302465863,302960690,303445272,303944330,304435820,304933593,305425599,305924761,306413548,306911125,307401203,307896441,308375663,308873554,309371327,309852207,310349973,310847228,311328720,311825743,312321412,312802862,313288639,313788114,314280120,314777886,315269049,315767755,316256575,316754909,317246298,317740880,318228113,318726068,319225230,319722485,320221191,320716657,321209005,321705757,322201665,322695252,323155496,323647922,324136709,324618201,325107021,325599369,326062799,326551877,327036591,327515439,327995002,328488730,328986306,329483329,329981663,330478415,330967493,331451484,331941412,332430723,332904810,333399621,333889699,334385367,334876756,335372664,335857378,336347306,336822688,337308604,337778320,338273147,338768386,339249836,339744418,340238004,340716852,341206163,341692079,342157550,342593070,343072557,343552685,344039071,344524129,345006756,345486319,345935380,346405090,346871863,347351350,347843134,348340346,348839144,349337945,349836122,350329850,350814499,351301780,351794476,352274604,352771816,353264820,353763078,354260393,354755221,355252797,355737784,356232539,356720106,357206492,357705290,358203548,358698821,359195780,359693886,360190909,360681131,361177683,361671733,362156791,362655592,363152907,363649866,364143318,364641086,365139420,365628766,366123348,366616227,367098854,367597031,368091859,368589965,369087733,369581137,370077889,370563413,371058164,371548618,372028181,372521909,373019485,373516508,374014842,374511594,375000672,375484663,375974591,376463903,376912964,377397612,377882599,378372821,378862167,379347691,379831682,380283332,380760951,381235937,381705648,382192929,382687683,383184235,383678817,384173568,384663496,385141115,385610263,386089154,386555927,387048624,387536191,388030241,388523120,389013574,389502885,389977871,390456762,390919694,391344627,391816773,392294682,392775562,393249055,393728171,394202258,394671969,395105506,395569060,396041206,396529198,397024286,397521778,398017544,398514623,399009434,399496715,399972092,400457149,400935058,401430146,401920334,402418098,402910235,403407796,403897874,404392628,404871924,405364830,405845710,406343202,406840966,407333516,407829507,408323755,408819423,409315975,409799105,410292111,410765604,411261370,411753507,412249498,412724983,413219457,413710846,414205428,414680324,415170118,415649234,416146313,416643874,417138122,417632596,418123825,418619733,419114484,419595504,420084632,420558719,421053530,421543608,422039276,422530665,423026573,423511287,424001215,424476597,424962513,425432224,425919505,426414259,426910811,427405393,427900144,428390072,428867691,429336839,429815731,430249268,430724644,431203940,431687070,432161966,432642986,433118368,433587516,434016740,434479138,434942693,435427750,435920656,436413662,436903456,437392584,437878500,438357391,438819789,439278123,439693637,440165381,440638387,441110128,441582518,442054432,442524148,442990921,443454476,443877243,444348987,444837261,445332290,445827936,446322968,446817954,447312781,447805478,448290535,448765653,449238659,449733688,450222834,450718469,451213683,451708499,452203738,452691305,453184211,453658900,454130641,454626287,455121922,455603414,456098696,456593855,457075305,457569355,458062361,458534585,459006975,459502007,459997221,460492503,460979216,461467798,461962380,462455259,462945053,463416360,463888274,464383260,464878076,465373235,465861817,466348907,466842493,467332947,467822075,468291505,468761221,469256048,469751287,470232737,470727319,471220905,471699753,472189064,472674980,473140451,473607224,474099921,474587488,475081538,475574417,476064871,476554182,477029168,477508059,477970991,478434546,478919603,479412509,479905515,480395309,480884437,481370353,481849244,482311642,482769977,483192744,483667862,484142551,484614775,485086082,485555512,486020983,486483915,486942249,487355811};
14 const int block=1000000;
15 bool check(int x)
16 {
17     int sum=0;
18     for(int i=x;i;i/=10)
19     {
20         sum+=i%10;
21     }
22     if(sum&1) return false;
23     sum>>=1;
24     ll f=1ll;
25     for(int i=x;i;i/=10)
26     {
27         f|=f<<(i%10);
28     }
29     return f&(1<<sum);
30 }
31 
32 int query(int x)
33 {
34     int res=table[x/block];
35     for(int i=x/block*block+1;i<=x;i++)
36     {
37         res+=check(i);
38     }
39     return res;
40 }
41 
42 int main()
43 {
44     int x,y;
45     while(~scanf("%d%d",&x,&y))
46     {
47         int ans=query(y)-query(x-1);
48         printf("%d\n",ans);
49     }
50     return 0;
51 }
提交代码

 

posted @ 2017-08-06 21:00  shulin15  阅读(375)  评论(0编辑  收藏  举报