Round Numbers
rndnum.pas/c/cpp
正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如谁先挤奶的顺序。她们甚至也不能通过扔硬币的方式。
所以她们通过"round number"竞赛的方式。第一头牛选取一个整数,小于20亿。第二头牛也这样选取一个整数。如果这两个数都是 "round numbers",那么第一头牛获胜,否则第二头牛获胜。如果一个正整数N的二进制表示中,0的个数大于或等于1的个数,那么N就被称为"round number" 。例如,整数9,二进制表示是1001,1001 有两个'0'和两个'1'; 因此,9是一个round number。26 的二进制表示是 11010 ; 由于它有2个'0'和3个'1',所以它不是round number。
很明显,奶牛们会花费很大精力去转换进制,从而确定谁是胜者。 Bessie 想要作弊,而且认为只要她能够知道在一个指定区间范围内的"round numbers"个数。
帮助她写一个程序,能够告诉她在一个闭区间中有多少round numbers。区间是[start, finish],包含这两个数。 (1 <= Start < Finish <= 2,000,000,000)
输入格式:
Line 1: 两个用空格分开的整数,分别表示Start 和 Finish。
输入样例 (文件名 rndnum.in):
2 12
输出格式:
Line 1: Start..Finish范围内round numbers的个数
输出样例 (文件名 rndnum.out):
6
输出解释:
2 10 1x0 + 1x1 ROUND
3 11 0x0 + 2x1 NOT round
4 100 2x0 + 1x1 ROUND
5 101 1x0 + 2x1 NOT round
6 110 1x0 + 2x1 NOT round
7 111 0x0 + 3x1 NOT round
8 1000 3x0 + 1x1 ROUND
9 1001 2x0 + 2x1 ROUND
10 1010 2x0 + 2x1 ROUND
11 1011 1x0 + 3x1 NOT round
12 1100 2x0 + 2x1 ROUND
这一题,打表,前面也说过方法,就直接上代码了,注释部分就是打表的部分
/*****************************
C++ Code
http://oijzh.cnblogs.com
by jiangzh
*****************************/
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int a[]={0,
483212,428796,584039,440663,560339,456787,418541,293669,558563,663826,582252,514517,488304,524202,409411,352767,354358,726956,577713,570084,398704,587599,426649,423239,268182,553737,443182,423493,292639,367549,304401,247803,176683,464101,834581,749207,707619,610617,724630,581424,565325,408623,737260,602686,602451,429888,578690,444221,414723,290612,589816,660124,583649,484625,507689,516038,412126,348083,345527,548234,409419,375711,262392,391453,237812,262449,138120,805169,782159,745849,645623,693915,652486,587801,485378,570943,690397,588690,527732,459699,533765,415092,375270,290436,734922,574328,596055,428383,585160,442405,416406,276953,518535,470190,412331,305556,337462,331068,256821,188362,
351512,708854,584226,554568,424082,568355,398515,393492,262893,592651,433714,418499,275925,388789,292584,251253,165001,409579,493367,420131,338386,321803,349630,243727,213155,195380,378280,244533,233378,123519,261425,138671,136027,62614,756286,906407,868389,808186,798532,813332,753334,692679,666600,844491,742474,715507,605456,728969,579429,570904,392562,883780,768520,754334,622832,713155,637790,594139,469566,609793,663833,579072,515393,489425,523250,409187,353718,368527,856747,739395,736778,575870,749036,605860,600630,436070,698394,625373,599349,467529,525946,481708,405931,320353,481902,694471,589483,532541,444493,555633,405896,382237,259351,572702,417250,425478,262933,411107,272924,253662,
154795,634443,809610,744448,663942,665861,693072,586793,530182,485740,719689,584217,557942,416061,571470,396035,428035,263301,715333,622147,588977,459889,544468,467883,415288,307042,428874,513117,415980,344251,309629,351366,254703,216897,197984,735312,574495,592606,427422,588151,439892,417324,275136,521668,468909,412295,304575,340496,327389,257452,187460,314777,532933,405918,371925,274840,385269,241529,231866,136218,421627,264809,258823,144463,240040,154871,133434,72312,555688,916748,872319,835723,774155,843575,740683,723770,607730,858112,741191,737493,586191,759736,611664,596898,443218,752220,802588,749485,655240,668781,665721,590941,515099,531845,706431,582234,537878,444534,558546,403110,
389617,236395,881851,768479,753053,623903,714318,635541,593887,470582,610843,663567,576125,516288,490665,522135,410189,353964,356703,726439,577221,570604,400635,579709,427755,423540,266632,557360,445186,419695,292465,367297,306314,247585,172669,472093,835775,747795,707465,608173,726578,581335,563360,412269,738085,595052,603396,429475,581106,443274,411951,291493,596370,657682,584182,485139,505192,516149,410033,349995,349358,543885,409208,377917,262266,391348,231792,262904,137753,714015,621684,590897,457607,545838,468089,415223,306998,428182,514251,415693,342894,310664,352326,252238,217078,190293,569853,396632,407274,261980,418344,268671,257467,143222,372960,292496,252540,163507,209002,177636,
135622,87762,350452,844491,743838,724435,595107,733775,576377,571876,422961,750006,611494,597817,444760,553188,465225,413991,301942,531000,666226,599645,516359,477045,529013,405586,365678,312733,557066,406038,391663,238863,426615,264202,258638,142526,640923,646780,592032,470637,517661,485791,410509,334592,399427,528446,408305,352207,295077,375676,244195,228432,126587,596810,428499,418955,272876,399005,277859,258640,156909,337996,306138,243405,183537,204626,188489,133550,93467,240341,725667,578622,568958,402246,575333,428280,423609,266067,558724,446090,417606,292373,367218,307025,248048,171201,373820,519377,412366,349951,292130,372819,248943,219457,153880,393567,245414,261569,138605,254397,
143140,131710,68788,469761,474665,409313,309904,342215,336165,250549,197332,234813,359298,251198,219795,146590,230913,116707,134849,60440,409822,272051,255636,147634,238321,156786,133411,74217,180191,187231,134593,88808,97815,94639,56585,41670,138890,978247,939675,946240,881658,934515,891873,870384,783274,870363,907254,867169,810895,793284,826837,752288,693973,668130,931154,865049,855598,749311,860530,739409,737472,599772,864797,769753,757574,625291,706869,645596,590448,477640,671358,912635,875374,834466,775354,842568,742464,720182,611326,856955,742324,737043,578127,764950,610194,597072,440915,759474,799331,751127,650855,674177,664415,586424,513023,539424,702474,584323,532747,449854,
556929,402702,388617,242129,949190,884274,871250,781957,835887,788501,754899,650939,736310,811892,740531,687916,651101,694893,588865,533452,489343,855271,741571,734502,578364,736967,608644,600785,432996,704939,627833,593544,466514,528521,482416,407024,311923,530995,835872,747172,706304,610665,724442,582915,559247,417599,737579,587523,603312,431353,580896,443022,409114,292362,602263,655696,584670,486920,501494,515365,410798,349125,354253,538990,411799,377308,263681,389695,234554,255919,137763,875656,893758,868388,793474,822038,803867,748040,663617,702626,833893,749159,699310,624349,709628,585789,556577,438421,862214,738481,746678,606629,748320,617140,593964,444704,666150,649383,585848,482451,
502018,498323,418534,338639,450918,844455,740487,726311,596376,732100,576089,573440,414973,754659,608926,600266,443239,552307,465533,415763,299346,537362,664254,597564,515952,477438,530965,406022,361377,316956,558644,403963,391488,232532,428421,262970,258546,142122,732897,796546,752456,649444,676510,663848,584338,513860,541375,702055,584378,533296,448975,556761,401261,389610,243890,754492,606292,598482,438442,567993,444600,423076,292749,480389,480603,405320,325094,339904,336649,255741,197575,287166,724237,581459,565900,405192,571721,424428,424703,264049,564234,444837,415592,291002,373046,302712,249981,166520,380294,516905,412211,347892,298264,366383,250521,216799,160628,391725,240221,262036,
138477,254832,142653,133016,67141,677350,909794,865895,813503,796193,832976,746469,705418,638772,847574,746996,727951,587842,735825,573267,598269,428544,828951,782691,749090,634406,701000,652312,587844,483522,568566,691611,591695,520511,467620,534775,407948,373337,309164,861875,738747,742699,607582,749476,615894,593866,445942,666234,649159,584421,483501,502998,495215,418856,339387,427809,707427,576726,556073,431440,562070,401063,394022,246319,600810,430182,421887,273079,387390,291981,255586,158933,562013,813053,755055,692987,635509,706752,581325,542219,460290,728728,579509,571438,391816,607644,428656,419865,271771,663227,639287,594014,468818,522639,483831,403038,335703,407068,524034,409257,
353165,293888,374926,241716,231026,135574,750970,605380,600192,436566,569778,443485,423461,292549,481787,480995,405986,322345,340958,337553,254319,197449,259390,554820,405965,383323,251413,393070,254444,261472,137833,404300,273420,253642,154509,232501,160604,131565,74348,462015,834482,747314,704733,614384,720558,584265,557543,421793,737077,579597,606092,429952,582582,441482,413187,286849,609715,651569,588564,484755,499086,513270,414973,344704,360374,532608,415486,375340,265845,388386,238110,248744,137544,723548,623688,591654,448251,551931,469651,412190,305171,431665,510438,417710,337482,318450,351808,246679,215043,203670,567655,398858,392198,262904,421127,266001,258029,145045,373620,292031,
252017,164119,213475,169280,136494,87717,341518,706451,578000,553204,433646,560024,403095,392985,243302,601585,431073,421087,272967,386738,292352,255544,158512,427654,487122,414226,335935,324969,349895,248481,201285,207965,377742,244356,229989,119184,256983,137753,135657,62582,531011,453720,418590,294853,364294,307034,241654,187312,281967,341302,250538,200337,170595,216882,125388,114474,71903,401365,262399,260633,139586,250643,144254,135163,68802,215557,164935,129771,78369,108323,88291,58500,35481,264174,977431,940221,938942,863425,939607,878873,877979,768776,902197,894888,870198,799813,813305,809402,744457,664099,727595,925785,866960,842902,761542,852478,744421,727835,589361,862245,
741221,766918,609298,737053,621811,589726,458704,750948,907276,868258,812678,794753,831524,749461,702288,642655,844531,749159,727033,588731,735469,574458,591093,427306,836564,780978,750811,628168,707030,650294,587614,481085,574561,685413,593726,516173,473609,532389,406013,372414,319477,939115,863066,862248,767190,866852,773499,754060,627168,783821,801370,746030,661831,662135,668721,598391,517277,550097,843190,741120,722899,600463,727837,580556,571546,403796,759209,612084,597248,442777,551598,466081,416477,295788,602067,813592,751647,691622,638513,705681,582579,536951,466836,728155,579463,569517,394981,599675,428579,418599,272860,669308,634597,593838,470875,522865,483928,399941,334929,411374,
521204,410917,353599,292991,373708,244050,228440,143280,943652,883153,875395,776587,841136,784083,755855,648998,741338,811876,742405,676296,656466,695654,587529,532640,493951,856856,742475,731788,581188,738049,593725,603381,429972,716038,624954,587424,465981,537261,475666,409313,309176,541086,834355,745239,706485,615530,717233,584076,559463,423066,736923,571297,608580,428776,583954,440783,415798,282536,615954,649915,590254,483886,495514,514511,415637,342535,364757,534032,412276,375115,265558,390430,237851,242545,137391,834712,778931,751841,624964,709716,649427,587773,480764,577262,682962,594084,516815,474159,532618,403536,373414,312737,733507,576264,572118,421505,598999,429825,422596,273610,
527184,464867,414707,301248,352345,307309,260536,187324,378924,704633,581501,548074,437365,557214,405729,391537,237565,605165,430273,420621,271725,391321,287795,257083,156215,436061,485363,409515,334393,330509,345461,250346,197645,213600,375151,244140,228294,125123,250046,137607,136180,62013,793846,900104,871151,804394,805483,812240,739975,689712,684344,837407,746504,708826,611210,725248,579161,568167,405398,864832,767959,758521,615081,724569,627622,594188,467515,621671,663650,581666,493157,501861,518880,412386,349545,387897,856302,742840,730190,582887,737809,590052,603297,430933,717193,624814,585378,467019,538310,474196,409180,310164,498148,692770,584515,532537,455139,541037,410441,378043,
276462,570953,392509,424301,262949,413130,272200,255544,147543,668857,803417,748539,663799,653919,691880,590757,523144,503265,708949,586452,556635,418691,571372,396748,402832,262300,736351,617944,594014,444115,555955,467514,410839,305466,440070,500172,418694,338240,320733,350700,242353,216547,227704,732759,576426,572729,417010,600152,428599,423492,273093,528030,464827,415818,300186,353624,307784,258293,187424,333115,530167,405987,362652,286432,376699,246226,231521,116720,428083,263229,258546,142189,244494,149986,135558,68813,565684,812786,747642,689690,644223,702534,584391,532897,472291,726984,578927,568962,398012,592424,427366,421421,269993,676271,629273,598083,468818,524908,481366,403899,
327248,416896,516770,415001,351045,292766,371896,247773,225128,150624,737097,607545,601142,432114,575056,445936,416090,291300,488916,481436,407422,310926,346117,337363,252611,195762,270045,551398,408367,376256,260623,392061,241623,261539,138754,406932,273212,251198,154760,235560,157601,132810,75433,429886,692056,586085,530497,457004,537681,412486,376957,278592,569958,393999,419954,262915,413120,272817,255586,146611,496105,470631,412275,306824,333789,336178,256042,189670,250366,353873,248451,216091,152025,229273,122994,118061,60416,589473,434948,418696,275124,389671,292492,250840,165296,309229,317684,257546,189270,192815,197342,124569,105153,143578,381458,241964,234764,126266,261304,138250,
136492,62624,234409,155195,134586,70463,116159,75714,57700,33297,386219,930537,864421,851839,754129,857376,741867,736966,572283,884083,768422,755700,621678,712305,640458,594224,468125,664612,812641,746105,690578,644751,701671,584237,533632,472132,727067,578001,569838,398585,588651,426749,422920,268538,766539,784435,757903,649349,679057,660517,581511,504529,556612,693272,590653,532577,445900,554366,406057,383910,259289,737215,603606,601968,430652,577891,444669,415017,290707,492605,479153,408641,308007,347664,336338,252549,195341,325791,853482,744456,727095,586871,737095,582404,604964,430819,720323,623827,586212,463881,541868,470498,412793,308820,500311,690719,588263,528299,459097,534592,
414692,375586,280671,568648,395911,415630,262991,413718,272730,256048,145549,605636,652018,587697,483157,493813,512732,417100,338445,372978,534209,407751,373148,271300,387748,240240,233222,137742,591568,433905,418513,275847,388793,292372,250529,165765,311169,315441,258470,188888,193565,196600,126635,102595,195702,859364,739968,737912,590974,758796,610473,598593,443128,669651,648066,590582,474720,512480,486231,415290,336173,442700,707130,581236,541942,440746,559885,403103,391074,232751,606267,428706,419569,272045,396737,281224,257545,156885,530998,663827,579995,515275,489138,523445,409153,353627,330334,556691,399665,391108,242710,403753,262201,260690,139899,547896,443794,422467,292415,367473,
305165,247788,174790,290091,337840,253877,197582,168958,216427,129651,108488,98360,737471,599584,603012,428894,580301,443373,414125,290362,495664,477583,409474,307413,347417,336272,251848,196287,276996,546812,409208,376510,262281,391438,235379,262699,137937,409429,271343,254664,150448,237385,155730,134485,74243,362672,512966,415451,344237,309538,351135,255362,217022,171041,388672,237936,247513,137491,257399,141359,135274,63001,362158,293906,252702,164023,206292,180302,135503,87444,158906,197754,127671,104121,79434,110180,54045,47357,21380,850091,774670,752960,627785,707079,648438,585819,482284,584187,671046,597031,517864,475659,530797,402934,369380,325458,729211,579287,572170,405912,
602188,431347,420701,272925,527672,467372,416008,297203,357007,310331,251034,186883,390824,706869,582003,538795,443722,558960,402893,389596,235803,602403,428600,418920,272666,397613,279688,257582,157432,446002,483760,399602,336120,334899,341375,251127,200423,214603,374633,242607,229924,130075,237445,137665,136942,60966,676410,625990,597607,467584,526016,482436,406057,318332,422121,518740,412997,350700,289928,374839,247593,221818,157069,573502,414791,425555,263616,411254,273066,252297,155129,355072,299205,250652,167157,207323,187365,131863,92606,243895,544619,409223,377560,262105,391614,232369,262892,137765,409923,270923,255533,149623,237904,156028,134258,74251,272446,334921,255783,191462
};
const int maxn=1000000;
int l,r,ans;
bool convert(int x)
{
int one=0,zero=0;
do{
if(x%2==0)zero++;else one++;
x/=2;
}while(x);
return zero>=one;
}
int work(int x)
{
int l=x/maxn;
int r=x;
int ans=0;
for(int i=1;i<=l;i++) ans+=a[i];
for(int i=l*maxn+1;i<=r;i++)
ans+=convert(i);
return ans;
}
int main()
{
freopen("rndnum.in","r",stdin);
freopen("rndnum.out","w",stdout);
/*****************打表******************
int ans=0;
for(int i=1;i<=2000000000;i++)
{
ans+=convert(i);
if(i%maxn==0)
{
printf("%d,",ans);
ans=0;
if(i%100000000==0)printf("\n");
}
}
*****************打表******************/
scanf("%d%d",&l,&r);
ans=work(r)-work(l-1);
printf("%d",ans);
return 0;
}