题意:如果一个数字用十进制表示,有大于等于1个1,或者大于等于2个2,或者大于等于3个3,或者大于等于4个4,或者大于等于5个5,或者大于等于6个6,或者大于等于7个7,或者大于等于8个8,或者大于等于9个9 则称这个数字为好数字,问 $ l,r $ 区间内有多少个好数字。
数据范围 $ l,r <=10^9 $
对于这一题,我们可以分段打表,这题在 $ 1e6 $ 的范围内是可以无压力跑过的,于是我们将 $ 1e9 $ 分成 $ 1e3 $ 个 $ 1e6 $ 打表出整数部分,再去跑小的部分,然后就可以求解。
并且题目要求求好数字,我们可以转换为求出不是好数字的数字,然后拿 $ n $ 减去即可。(据说直接暴力枚举 $ + O_2 $ 是能 $ AC $ 的。
正解是数位 $ dp $ ,从左到右逐位确定当前位置的数字的值,并记录每个数字出现次数,当符合条件 (后⾯⽆论接什么数字,都⼩于 R) 的时候,剩下位数使⽤多重集的排列(组合数)计算。
打表程序
#include<bits/stdc++.h>
using namespace std;
long long a[20];
long long l,r;
long long ans;
bool cheak(int n){
int num[10];
for(int i=0;i<=9;++i) num[i]=0;
while(n){
int x=n%10;
num[x]++;
if(x&&num[x]>=x) return true;
n/=10;
}
return false;
}
int main(){
freopen("txt.out","w",stdout);
for(int i=1;i<=1e9;++i){
if(cheak(i)) ans++;
if(i>=1000000&&(!(i%1000000))) printf("%lld,",ans);
}
return 0;
}
$ AC $ 代码
#include<bits/stdc++.h>
using namespace std;
long long a[2000]={0,553087,1553086,2299402,2909068,3471934,4025965,4579099,5132186,5685272,6238359,7238359,8238359,9238359,10238359,11238359,12238359,13238359,14238359,15238359,16238358,16984675,17984675,18984674,19766990,20520146,21267197,22013555,22759872,23506188,24252504,24862171,25862170,26644486,27426802,28045828,28656424,29266138,29875805,30485471,31095137,31658004,32658003,33411159,34030185,34649211,35213022,35775936,36338803,36901669,37464535,38018567,39018566,39765617,40376213,40940024,41503835,42057914,42611946,43165977,43720008,44273143,45273142,46019500,46629214,47192128,47746207,48300286,48853421,49406555,49959689,50512777,51512776,52259093,52868760,53431627,53985659,54538794,55091929,55645016,56198103,56751190,57751189,58497505,59107171,59670037,60224068,60777202,61330289,61883376,62436462,62989549,63989548,64735864,65345530,65908396,66462427,67015561,67568648,68121734,68674821,69674821,70674821,71674821,72674821,73674821,74674821,75674821,76674821,77674821,78674821,79674821,80674821,81674821,82674821,83674821,84674821,85674821,86674821,87674821,88674821,89674821,90674821,91674821,92674821,93674821,94674821,95674821,96674821,97674821,98674821,99674821,100674821,101674821,102674821,103674821,104674821,105674821,106674821,107674821,108674821,109674821,110674821,111674821,112674821,113674821,114674821,115674821,116674821,117674821,118674821,119674821,120674821,121674821,122674821,123674821,124674821,125674821,126674821,127674821,128674821,129674821,130674821,131674821,132674821,133674821,134674821,135674821,136674821,137674821,138674821,139674821,140674821,141674821,142674821,143674821,144674821,145674821,146674821,147674821,148674821,149674821,150674821,151674821,152674821,153674821,154674821,155674821,156674821,157674821,158674821,159674821,160674821,161674821,162674821,163674821,164674821,165674821,166674821,167674821,168674820,169421137,170421137,171421136,172203452,172956608,173703659,174450017,175196334,175942650,176688967,177688967,178688967,179688967,180688967,181688967,182688967,183688967,184688967,185688967,186688967,187688967,188688967,189688967,190688967,191688967,192688967,193688967,194688967,195688967,196688966,197471283,198471283,199471282,200354248,201143044,201926080,202708438,203490755,204273071,205055387,205808544,206808544,207808543,208597339,209386135,210140026,210893224,211646381,212399537,213152693,213899745,214899745,215899744,216682780,217436671,218190562,218937655,219684707,220431758,221178809,221925168,222925168,223925167,224707525,225460723,226207816,226954909,227701268,228447626,229193984,229940302,230940302,231940301,232722618,233475775,234222827,234969186,235715545,236461862,237208179,237954496,238954496,239954495,240736811,241489967,242237018,242983376,243729693,244476010,245222326,245968643,246968643,247968642,248750958,249504114,250251165,250997523,251743840,252490156,253236472,253846139,254846138,255628454,256410770,257029796,257640392,258250106,258859773,259469439,260079106,261079106,262079106,263079106,264079106,265079106,266079106,267079106,268079106,269079106,270079105,270861422,271861422,272861421,273744387,274533183,275316219,276098577,276880894,277663210,278445526,279227843,280227842,281110809,282110808,282899604,283682640,284464998,285247315,286029631,286811947,287430974,288430973,289219769,290008565,290679431,291299387,291918461,292537488,293156514,293775540,294386137,295386136,296169172,296952208,297572164,298192120,298802764,299413361,300023957,300634553,301244268,302244267,303026625,303808983,304428057,305038701,305649345,306259060,306868774,307478488,308088156,309088155,309870472,310652789,311271816,311882413,312492128,313101843,313711510,314321177,314930844,315930843,316713159,317495475,318114501,318725097,319334811,319944478,320554145,321163811,321773478,322773477,323555793,324338109,324957135,325567731,326177445,326787112,327396778,328006444,328569311,329569310,330322466,330941492,331560518,332124329,332687243,333250110,333812976,334375843,335375843,336375843,337375843,338375843,339375843,340375843,341375843,342375843,343375843,344375842,345128999,346128999,347128998,347917794,348706590,349460481,350213679,350966836,351719992,352473148,353092175,354092174,354880970,355669766,356340632,356960588,357579662,358198689,358817715,359436741,360055768,361055767,361844563,362515429,363304225,363924181,364543255,365162282,365781308,366400334,366964146,367964145,368718036,369337992,369957948,370531519,371095378,371659190,372223001,372786812,373349727,374349726,375102924,375721998,376341072,376904931,377468790,378031705,378594619,379157533,379720401,380720400,381473557,382092584,382711611,383275423,383838338,384401253,384964120,385526987,386089854,387089853,387843009,388462035,389081061,389644872,390207786,390770653,391333520,391896386,392459253,393459252,394212408,394831434,395450460,396014271,396577185,397140052,397702918,398265784,398819816,399819815,400566866,401177462,401741273,402305084,402859163,403413195,403967226,404521258,405521258,406521258,407521258,408521258,409521258,410521258,411521258,412521258,413521258,414521257,415268309,416268309,417268308,418051344,418805235,419559126,420306219,421053271,421800322,422547373,423157970,424157969,424941005,425724041,426343997,426963953,427574597,428185194,428795790,429406386,429970198,430970197,431724088,432344044,432964000,433537571,434101430,434665242,435229053,435792864,436356676,437356675,438110566,438730522,439304093,439924049,440487908,441051720,441615531,442179342,442733422,443733421,444480514,445091158,445655017,446218876,446773900,447327980,447882059,448436138,448990171,449990170,450737222,451347819,451911631,452475443,453029523,453583603,454137635,454691667,455245699,456245698,456992749,457603345,458167156,458730967,459285046,459839078,460393110,460947141,461501173,462501172,463248223,463858819,464422630,464986441,465540520,466094552,466648583,467202614,467755749,468755748,469502106,470111820,470674734,471228813,471782892,472336027,472889161,473442296,474442296,475442296,476442296,477442296,478442296,479442296,480442296,481442296,482442296,483442295,484188654,485188654,486188653,486971011,487724209,488471302,489218395,489964754,490711112,491457470,492067185,493067184,493849542,494631900,495250974,495861618,496472262,497081977,497691691,498301405,498864320,499864319,500617517,501236591,501855665,502419524,502983383,503546298,504109212,504672126,505226206,506226205,506973298,507583942,508147801,508711660,509266684,509820764,510374843,510928922,511483002,512483001,513230094,513840738,514404597,514959621,515523480,516077560,516631639,517185718,517738854,518738853,519485212,520094927,520657842,521211922,521766002,522319185,522872320,523425455,523978590,524978589,525724947,526334661,526897575,527451654,528005733,528558868,529112003,529665137,530218272,531218271,531964629,532574343,533137257,533691336,534245415,534798550,535351684,535904818,536457906,537457905,538204222,538813889,539376756,539930788,540483923,541037058,541590145,542143233,543143233,544143233,545143233,546143233,547143233,548143233,549143233,550143233,551143233,552143232,552889550,553889550,554889549,555671866,556425023,557172075,557918434,558664793,559411110,560157427,560767095,561767094,562549411,563331728,563950755,564561352,565171067,565780782,566390449,567000116,567562984,568562983,569316140,569935167,570554194,571118006,571680921,572243836,572806703,573369570,573923603,574923602,575670654,576281251,576845063,577408875,577962955,578517035,579071067,579625099,580178235,581178234,581924593,582534308,583097223,583651303,584205383,584758566,585311701,585864836,586417972,587417971,588164330,588774045,589336960,589891040,590444223,590998303,591551438,592104573,592657661,593657660,594403977,595013644,595576511,596130543,596683678,597236813,597789901,598342988,598896076,599896075,600642392,601252059,601814926,602368958,602922093,603475228,604028315,604581402,605134489,606134488,606880804,607490470,608053336,608607367,609160501,609713588,610266675,610819762,611819762,612819762,613819762,614819762,615819762,616819762,617819762,618819762,619819762,620819761,621566078,622566078,623566077,624348393,625101549,625848600,626594958,627341275,628087592,628833908,629443575,630443574,631225890,632008206,632627232,633237828,633847542,634457209,635066876,635676542,636239409,637239408,637992564,638611590,639230616,639794427,640357341,640920208,641483075,642045941,642599973,643599972,644347023,644957619,645521430,646085241,646639320,647193352,647747384,648301415,648854550,649854549,650600907,651210621,651773535,652327614,652881693,653434828,653987963,654541097,655094185,656094184,656840501,657450168,658013035,658567067,659120202,659673337,660226425,660779512,661332600,662332599,663078916,663688583,664251450,664805482,665358617,665911705,666464840,667017927,667571014,668571013,669317329,669926995,670489861,671043892,671597026,672150113,672703200,673256286,673809373,674809372,675555688,676165354,676728220,677282251,677835385,678388472,678941558,679494645,680494645,681494645,682494645,683494645,684494645,685494645,686494645,687494645,688494645,689494644,690240961,691240961,692240960,693023276,693776432,694523483,695269841,696016158,696762474,697508790,698118457,699118456,699900772,700683088,701302114,701912710,702522424,703132091,703741757,704351423,704914290,705914289,706667445,707286471,707905497,708469308,709032222,709595089,710157955,710720821,711274853,712274852,713021903,713632499,714196310,714760121,715314200,715868232,716422263,716976294,717529429,718529428,719275786,719885500,720448414,721002493,721556572,722109707,722662841,723215975,723769063,724769062,725515379,726125046,726687913,727241945,727795080,728348215,728901302,729454389,730007476,731007475,731753791,732363457,732926323,733480354,734033488,734586575,735139662,735692748,736245835,737245834,737992150,738601816,739164682,739718713,740271847,740824934,741378020,741931108};
long long l,r;
long long size=1000000;
long long ans,tmp1,tmp2;
bool cheak(int n){
int num[10];
for(int i=0;i<=9;++i) num[i]=0;
while(n){
int x=n%10;
num[x]++;
if(x&&num[x]>=x) return true;
n/=10;
}
return false;
}
long long solve(int n){
long long ans=a[n/size];
for(int i=size*(n/size)+1;i<=n;++i){
if(cheak(i)) ans++;
}return ans;
}
int main(){
scanf("%lld %lld",&l,&r);
printf("%lld",solve(r)-solve(l-1));
return 0;
}