【分块打表】bzoj3758 数数

验证一个数是不是优美的:设数位之和为sum,若sum mod 2 != 0,则不优美。否则考虑枚举这个数的每一位,将之前所有位任意相加产生的所有 数字和 和 当前位 的和塞到集合里,最终判断集合中是否存在sum/2即可,若暴力,复杂度是1+2+4+...,无法承受,所以可以压位。

然后对10^9分块,暴力处理每个块内的答案,然后打个表,对零散的部分暴力即可。

#include<cstdio>
using namespace std;
const int table[]={0,376413,832547,1288828,1744956,2196800,2647716,3090920,3526440,3951372,4366880,4823015,5304766,5797144,6290672,6782004,7272530,7758910,8238396,8710536,9182258,9638539,10130918,10610575,11103529,11590745,12080513,12565094,13045216,13523103,13996047,14452175,14945703,15438658,15913605,16403907,16892727,17371943,17858307,18339125,18810719,19262563,19753895,20241111,20731414,21199399,21684697,22170473,22655525,23128982,23601310,24052226,24542752,25032520,25521340,26006639,26480512,26967738,27450344,27929454,28401296,28844500,29330880,29815461,30294677,30780453,31267680,31727883,32207445,32681531,33151141,33586661,34066147,34546269,35032633,35517685,36000291,36479854,36928875,37398605,37865237,38290169,38762309,39240196,39721014,40194471,40673581,41147667,41617398,42050819,42514127,42929635,43401357,43874301,44345895,44818223,45290065,45759675,46226307,46689616,47111407,47567542,48049293,48541671,49035199,49526531,50017057,50503437,50982923,51455063,51926786,52408537,52900916,53398017,53897101,54395976,54894264,55390376,55882154,56370124,56858336,57350715,57847816,58346901,58846682,59346650,59846494,60345546,60842736,61337762,61832643,62326171,62825256,63325037,63822618,64321881,64821477,65319367,65818159,66315614,66811192,67302524,67801399,68301368,68800631,69295186,69792675,70292149,70790949,71286703,71781713,72272239,72770527,73270371,73769968,74267457,74763402,75261351,75759527,76256605,76751555,77237935,77734047,78233099,78730989,79230464,79728413,80220834,80714563,81209373,81704149,82183635,82675413,83172603,83671395,84170195,84668372,85162101,85646750,86134011,86626601,87098741,87586711,88081737,88579192,89074946,89572024,90066835,90554096,91029271,91513942,91985664,92473876,92968757,93464335,93959345,94454295,94949071,95441662,95926333,96400455,96856736,97349115,97828772,98321726,98808942,99298710,99783291,100263413,100741300,101214244,101706623,102203724,102702809,103202590,103702558,104202402,104701454,105198644,105693670,106188552,106668209,107167293,107654509,108153408,108643571,109140447,109631936,110124934,110615086,111104170,111597125,112096906,112595804,113093027,113592810,114091415,114589181,115087418,115585176,116080738,116567954,117067923,117558086,118057868,118549357,119048442,119540443,120037757,120529913,121025141,121514909,122014753,122511630,123010235,123509319,124005151,124504320,124999107,125496723,125991518,126476099,126975151,127466640,127964407,128456408,128955576,129444361,129941988,130432181,130927549,131407671,131904861,132397859,132896096,133393411,133888198,134385824,134870790,135365761,135853348,136331235,136826261,137316413,137814171,138306327,138803944,139294137,139789107,140268613,140761869,141234813,141729694,142218778,142714340,143209568,143704363,144199732,144687319,145180575,145654892,146111020,146604548,147097503,147572450,148062752,148551572,149030788,149517152,149997970,150469564,150963092,151462177,151961958,152459539,152958802,153458398,153956288,154455080,154952535,155448113,155941068,156440849,156939747,157436970,157936753,158435358,158933124,159431361,159929119,160424682,160899629,161397209,161894432,162373648,162870479,163366196,163847035,164342307,164834856,165316242,165806545,166305808,166805590,167302421,167797533,168296439,168794206,169291181,169787227,170282583,170771403,171271000,171769605,172265321,172764227,173259685,173756939,174255102,174749455,175244787,175724003,176221893,176719660,177200499,177698265,178195519,178676905,179174053,179669962,180151503,180637867,181136659,181634896,182130169,182627144,183125306,183622454,184112942,184610046,185104838,185585656,186083111,186580869,187073418,187569465,188063818,188559726,189056830,189540680,190034891,190506485,191002063,191497625,191979011,192474367,192969700,193451241,193946033,194440244,194913068,195364912,195856244,196343460,196833763,197301748,197787046,198272822,198757874,199231331,199703659,200194991,200693866,201193835,201693098,202187653,202685142,203184616,203683416,204179170,204674180,205161396,205661365,206151528,206651310,207142799,207641884,208133885,208631199,209123355,209618583,210108886,210608149,211107931,211604762,212099874,212598780,213096547,213593522,214089568,214584925,215052910,215547464,216038953,216534065,217007522,217499718,217990901,218484410,218959905,219446727,219932026,220429515,220928599,221427505,221919701,222413231,222911993,223409872,223904583,224393574,224879350,225378825,225870826,226368592,226859775,227358537,227847477,228346119,228838089,229333643,229818695,230317495,230814810,231311785,231805293,232303172,232801814,233291625,233787283,234281777,234755234,235250988,235743144,236239191,236714686,237209396,237701366,238197024,238673284,239165608,239637936,240132946,240628174,241123530,241610353,242099344,242594898,243089392,243581716,244056062,244506978,244997504,245487272,245976092,246461391,246935264,247422490,247905096,248384206,248856048,249346574,249844862,250344706,250844303,251341792,251837737,252335686,252833862,253330940,253825890,254315658,254815502,255312379,255810984,256310068,256805900,257305069,257799856,258297472,258792267,259281087,259780684,260279289,260775005,261273911,261769369,262266623,262764786,263259139,263754471,264239770,264737259,265236343,265735249,266227445,266720975,267219737,267717616,268212327,268701319,269175192,269671136,270166968,270662426,271155956,271637394,272132980,272626656,273118401,273606213,274093440,274591389,275090557,275587811,276086573,276582159,277074753,277572087,278069041,278564272,279046878,279545055,280039842,280538004,281035883,281529559,282026893,282513143,283009664,283502662,283981772,284478850,284976467,285470820,285965530,286457275,286954229,287450750,287934208,288427696,288899538,289394488,289889283,290384616,290873607,291361419,291856650,292349648,292843136,293317366,293760570,294246950,294731531,295210747,295696523,296183750,296643953,297123515,297597601,298067211,298553591,299049703,299548755,300046645,300546120,301044069,301536490,302030219,302525029,303019805,303504386,304003438,304494927,304992694,305484695,305983863,306472648,306970275,307460468,307955836,308435052,308932942,309430709,309911548,310409314,310906568,311387954,311885102,312381011,312862552,313348328,313847803,314339804,314837570,315328753,315827515,316316455,316815097,317307067,317802621,318289848,318787797,319286965,319784219,320282981,320778567,321271161,321768495,322265449,322760681,323220884,323713304,324202089,324683475,325172415,325665009,326128508,326618472,327104745,327585453,328065016,328558745,329056371,329553519,330052161,330549495,331039459,331524705,332017239,332510934,332985020,333479831,333970024,334465932,334957902,335454856,335941129,336433663,336912903,337405293,337874903,338369679,338865048,339346589,339842143,340337374,340818082,341311777,341804167,342276694,342712214,343191700,343671822,344158186,344643238,345125844,345605407,346054428,346524158,346990790,347470276,347962054,348459244,348958036,349456836,349955013,350448742,350933391,351420652,351913242,352393364,352890554,353383552,353881789,354379104,354873891,355371517,355856483,356351454,356839041,357325405,357824197,358322434,358817707,359314682,359812844,360309992,360800480,361297584,361792376,362277428,362776228,363273543,363770518,364264026,364761905,365260547,365750358,366246016,366740510,367223116,367721293,368216080,368714242,369212121,369705797,370203131,370689381,371185902,371678900,372158463,372652192,373149818,373646966,374145608,374642942,375132906,375618152,376110686,376604382,377053403,377538051,378023017,378513505,379003316,379489566,379974812,380427196,380908305,381388631,381858362,382345623,382840593,383337697,383833355,384329876,384822410,385303519,385778157,386266143,386732775,387225366,387712953,388207745,388702239,389195237,389688932,390169258,390657244,391130374,391555306,392027446,392505333,392986151,393459608,393938718,394412804,394882535,395315956,395779264,396251404,396739374,397234400,397731855,398227609,398724687,399219498,399706759,400181934,400666605,401144492,401639518,402129670,402627428,403119584,403617201,404107394,404602364,405081870,405575126,406055944,406553399,407051157,407543706,408039753,408534106,409030014,409527118,410010968,410505179,410978636,411474390,411966546,412462593,412938088,413432798,413924768,414420426,414896686,415389010,415868120,416365198,416862815,417357168,417851878,418343623,418840577,419337098,419820556,420314044,420788130,421282941,421773134,422269042,422761012,423257966,423744239,424236773,424716013,425208403,425678134,426165395,426660365,427157469,427653127,428149648,428642182,429123291,429597929,430085916,430519337,430994511,431474017,431957867,432434127,432917585,433396825,433871463,434306086,434779290,435242599,435727270,436220526,436714737,437207061,437700549,438192939,438680925,439154129,439626259,440041767,440513489,440986433,441458027,441930355,442402197,442871807,443338439,443801748,444223539,444695261,445183473,445678354,446173932,446668942,447163892,447658668,448151259,448635930,449110052,449582996,450077877,450566961,451062523,451557751,452052546,452547915,453035502,453528758,454003075,454474669,454970247,455465809,455947195,456442551,456937884,457419425,457914217,458408428,458881252,459353580,459848590,460343818,460839174,461325997,461814988,462310542,462805036,463297360,463771706,464243548,464738498,465233293,465728626,466217617,466705429,467200660,467693658,468187146,468661376,469130986,469625762,470121131,470602672,471098226,471593457,472074165,472567860,473060250,473532777,473999409,474492000,474979587,475474379,475968873,476461871,476955566,477435892,477923878,478397008,478860317,479344988,479838244,480332455,480824779,481318267,481810657,482298643,482771847,483243978,483665769,484139891,484614208,485087032,485561378,486035608,486508135,486981265,487453395,487875963};
inline bool check(const int &x)
{
	int sumv=0,t=x;
	while(t)
	  {
	  	sumv+=t%10;
	  	t/=10;
	  }
	if(sumv&1) return 0;
	long long S=1; t=x;
	while(t)
	  {
	  	S|=(S<<t%10);
	  	t/=10;
	  }
	return S&(1ll<<(sumv>>1));
}
int calc(const int &x)
{
	int res=table[x/1000000];
	for(int i=x/1000000*1000000+1;i<=x;++i)
	  res+=check(i);
	return res;
}
int main()
{
	int x,y;
	scanf("%d%d",&x,&y);
	printf("%d\n",calc(y)-calc(x-1));
	return 0;
}
时间: 2024-11-08 22:34:33

【分块打表】bzoj3758 数数的相关文章

BZOJ 3758 数数 分块打表

题目大意:定义一个数是完美的,当且仅当这个数的每一位可以分成两个集合,使这两个集合之和相等,求[a,b]区间内有多少个数是完美的 数位DP?--不大好搞 分块打表大法好! 首先考虑验证一个数是不是完美的怎么搞 求出数字和 如果是奇数肯定不是 如果是偶数就跑一下背包 背包很慢?没关系,由于最大的和只能有9*8/2=36 所以我们直接状压 令f=1 然后对于每一位x有 f|=f<<x 最后返回f&(1<<sum/2)即可 时间复杂度O(lgx) 但是这数据范围是10^9诶!总不

BZOJ3758 数数

首先我们看对于一个数怎么判断. 方法是,暴力判断...←_←(不要骗我...) 首先看各位数字之和,如果是奇数就不是"优美"的. 否则用dp判断,我们可以状压一下,直接记一个long long大小的数叫f,则f |= (f << num),其中num表示一位数. 时间复杂度是log(x),其中x是要判断的数字. 很慢?貌似是的说...于是,分块打表大法好!!! 我们设一个块大小10^6,总共10^3个块(要考虑到可能会编译超时的问题),于是直接求出来每个块之前的ans,然后

oracle 统计指定条件下所有表的行数

今天 需要统计下指定用户下的所有表的行数,于是采用了oracle 内置视图: select table_name,num_rows  from dba_tables where owner = 'USERNAME';  或 select table_name,num_rows from user_all_tables ; 可是统计结果发现,有的表的统计数量和实际数量有差异,因此,直接自己写了个统计指定条件下表的记录的sql: --创建一个表用于存储计算结果 create table t_temp

7.04 求一个表的行数

问题:计算一个表的行数,或计算某个列中值的个数.例如,找到职员总数以及每个部门的职员数.解决方案:如果以整个表作为一个组或一个窗口计算行数,则只需使用COUNT函数及"*"字符:select count(*) from emp; 如果要创建多个数据组或窗口,则使用COUNT函数的同时,还要使用GROUP BY子句:select deptno,count(*) from emp group by deptno;

c语言:实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定

实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定, 输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表. 程序: #include<stdio.h> void mul(int n)//multiplication 乘法 { int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%d*%d=%-2d  ", i, j, i*j); //其中%2d中的2表示

【SQL】统计所有表的行数

原文:[SQL]统计所有表的行数 环境:mssql ent 2k8 r2 原理:遍历所有用户表,用sp_spaceused过程分别获取每张表的行数并写入临时表,最后返回临时表 IF OBJECT_ID('tempdb..#TableRowCount','U') IS NOT NULL DROP TABLE #TableRowCount GO CREATE TABLE #TableRowCount (Name sysname PRIMARY KEY, RowCnt DECIMAL(11,0), R

datastage 分析日志获取表记录条数

DataStage通过分析日志获取Job插入目标表的记录数 这只是一种不太好的方法,也许还有更好.更简便的方法.这种方法要求每次运行Job之前删除已有的日志信息,否则无法统计出正确的记录数.当然,在Job跑完之后,可以在shell备份本次Job运行的日志到服务器磁盘. 1       日志清理设置 登录Datastage Administrator,选择对应项目,项目属性->记录,勾选"自动清除作业日志",设置为自动清理上次及以前的日志. Figure 1 Administrat

sqlserver查询所有表的行数的sql语句

原文:sqlserver查询所有表的行数的sql语句 select a.name, b.rows  from sysobjects a inner join sysindexes b on a.id = b.id where a.type = 'u'   and b.indid in (0, 1)order by a.name

SQLSERVER中统计所有表的记录数

利用系统索引表sysindexes中索引ID indid<1的行中的rows列存有该表的行数这一特点.    最后一种方法是利用隐藏未公开的系统存储过程sp_MSforeachtable CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT) EXEC sp_MSforeachtable 'INSERT INTO #temp SELECT ''?'', COUNT(*) FROM ?' SELECT TableName, RowCnt F

查询所有表的记录数SQLServer

SELECT object_name (i.id) TableName,          rows as RowCnt   FROM sysindexes i   INNER JOIN sysObjects o       ON (o.id = i.id AND o.xType = 'U ')   WHERE indid < 2   ORDER BY RowCnt desc,TableName 查询所有表的记录数SQLServer