2017"百度之星"程序设计大赛 - 资格赛 寻找母串

Posted 樱花落舞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017"百度之星"程序设计大赛 - 资格赛 寻找母串相关的知识,希望对你有一定的参考价值。

Problem Description

对于一个串S,当它同时满足如下条件时,它就是一个01偏串: 1、只由0和1两种符组成; 2、在S的每一个前缀中,0的个数不超过1的个数; 3、S中0的个数和1的个数相等。

现在给定01偏串S,请计算一下S在所有长度为n的01偏串中作为子串出现的次数的总和。 由于结果比较大,结果对1e9+7取余后输出。

样例解释: 在第二个样例中,长度为4的偏串共两个1010,1100。10在1010中出现了两次,在1100中出现了1次。所以答案是3。

Input

第一行给出一个整数T(1<=T<=40),表示测试数据的数目。 每一组测试包含一个整数n和字符串S,中间用空格分开。(1<=|S|<=100000,1<=n<=1000000000)

输入保证S是一个01偏串。

Output

对于每一组数据,输出一个整数占一行,表示答案。

Sample Input
2
2 10
4 10
Sample Output
1
3
解法:
1 我们求等于0的情况 n为奇数或者字符长度>n
2 我们暴力的算一算发现,这个和字符串长度有关,和具体内容无关
3 通过查询发现这是个ans=(n-len)/2
                                     

但数据太大,1s算不完,只能化简看看啦
4 x=(n-len)/2+1
                  

我们有个叫卡特兰数的东西带进去

5 我们只需要算f(x),他有一个

但从1算到1e9还是大了,我们就分段计算,每1e5个数字保存一次,然后每一次计算找到它最近的起点就行

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 //计算n的k次方对M取模,二分法
 5 ll Pow(ll n, ll k, ll p){
 6     ll ans=1;
 7     while(k){
 8         if(k&1)
 9             ans=(ans*n)%p;
10         n = (n*n)%p;
11         k>>=1; //k=k>>1  k=k/2;
12     }
13     return ans;
14 }
15 ll C(ll n,ll m,ll p){
16     if(m==0) return 1;
17     if(m>n-m) m=n-m;
18     ll up=1,down=1;
19     for(int i=1;i<=m;i++){
20         up = (up*(n-i+1))%p;
21         down=(down*i)%p;
22     }
23     return up*Pow(down,p-2,p)%p;
24 }
25 ll lucas(ll n,ll m,ll p){
26     if(m==0) return 1;
27     return C(n%p,m%p,p)*lucas(n/p,m/p,p);
28 }
29 #define mod 1000000007
30 int ct[6000]={1,945729344,874271714,728937729,488957151,213941567,621948608,165410919,130152966,477849061,70646122,104331417,804334677,112228257,406385851,373610584,959189339,129232103,596011761,653302265,502350404,254254274,23933464,200023312,437339905,838455202,353529507,181826790,268707842,918554769,361346624,475114772,551764204,264122255,348587014,339775734,190610810,147039338,576212642,958452888,907080272,897468755,485757524,926001334,253159604,720502565,795701677,485379561,512490195,62869610,44647373,170729326,181183678,223602264,14266485,546070015,669748641,266401585,722432477,844276843,806809752,215407850,514089950,48258832,388402233,287386547,184275733,39087220,221157750,796058916,274451201,551481903,468840632,654694122,349643823,123450237,466554578,613371117,104229634,534520804,340376885,53768135,665006362,362834988,113383960,458629513,877935697,990187981,24010351,477599350,491321663,619368665,730486899,302800222,248364522,617385859,136706557,988949479,681384128,140672609,39443745,327846413,353172956,108275948,378351259,482275913,432213793,904945283,871063203,757590126,541675614,649654378,339103071,153511190,307061975,535998427,953278988,315396128,629843192,166102135,623766767,9922024,962639966,258645380,484156059,75913669,837794293,574075634,404396328,768092500,903633183,512519672,44285819,538902069,621789723,71341556,81438997,28847533,40513591,849551327,716939958,942552133,395636757,409774346,383255288,612104482,95851638,696370843,884779483,524922348,220269701,390881347,114227221,395767320,614338019,380172060,180822541,691300228,891829295,967678016,701386253,162940737,994175698,908221589,19098271,438672588,631720803,573850469,339056525,473356393,859071294,949727021,98529638,168890563,563975847,7483795,276476730,39489314,514728211,87529508,963753157,380031470,856696972,158703490,515908206,227044626,959511111,993078622,80360936,888779384,323285665,543535546,550763954,601069950,458756853,717452438,141044757,197501322,634124805,238583278,200414323,553793080,66504653,42069842,722598090,913608032,740683059,742050754,961341417,612741304,91785690,526087340,214299335,290988315,920349511,650032143,826861662,655676777,832872230,243503280,428075391,780176613,762279490,958553222,298583588,683539087,245173409,561149407,172900450,994863083,373712992,577710034,676813275,65061172,354158163,474587452,779048700,252731521,513247255,63716789,589280106,300449755,843219077,437936157,617131692,778975010,166446598,341293627,939851882,377019792,974135048,18159888,617899054,824539871,922256819,904951680,298436189,877366713,763013330,69488053,73161945,968841940,502298660,815060335,585478837,52225572,617841877,964952304,864682249,537138023,438032312,507344261,611203109,211965075,510592131,253042462,662211654,306668985,993054101,886017682,550897294,748379779,600811784,662510825,923914254,398819263,459843924,563578785,800110936,890507874,833696238,67588077,662523469,355339772,897895240,326276953,66185485,707119691,543399352,359252814,343020869,435649016,326479513,313711009,180736295,933688638,341979700,832484396,852821336,938966456,327938691,897898594,514258765,67631305,832209634,147167201,933864871,764372915,290610995,894446445,552066969,955550128,899538317,98587581,791520663,371485442,250166899,386277652,850700239,24883422,365069578,542759096,393648297,338311897,998392022,963038213,184331347,216025562,616142588,243671535,922469047,642896156,173840546,617065897,502916270,399859525,626708892,203438585,794335044,466847771,485435744,176762177,370414892,387922877,421040690,506803021,831791096,536761402,659624740,205742555,354512880,992080290,655638247,441557017,45234385,602453687,642559130,907907516,150309994,959349261,667245533,336984599,7514180,293021893,527065808,716061908,183191397,64976751,577967909,833760679,814950379,238991246,714290102,902231891,437427307,149073027,103173270,685925477,694198173,725256565,71168984,261760780,55757530,112359206,436686978,516723667,537954237,916255072,34043384,456908325,881291767,146092388,189805926,498850664,471576564,977024064,502710283,230937450,166248501,100683033,725464484,750575586,525293805,107750423,541103694,100780834,184498203,767490572,317419646,113029816,182757677,138614676,694365799,832641460,450632411,301670090,972578606,809851941,356139480,741575534,319109663,491688831,946837771,820876500,944731056,441691410,439779726,878100434,110259934,781810231,822737100,122949140,185157201,147977057,78103215,98159640,570747872,913214189,526507036,614910594,606219102,234699129,882201646,184318349,623751699,220799331,756153289,390599249,416019375,679472812,608212465,946818526,676784388,848462543,701625645,631586665,279738981,507735946,983863013,797150469,667058957,302361878,11123699,265956565,759232458,630434107,603234456,651468455,184824418,604726700,101186449,24954474,873710205,172883575,299713548,965606121,589841373,782619807,545740664,94087806,846145323,99057958,275906334,98324641,712417499,506415720,153372884,988856645,995466415,137196894,296743168,461577383,447394859,350928543,800328411,359672375,623997509,289025167,258422311,586168764,714133593,295537278,650382172,140783549,886171581,956390396,255289567,255216580,795608020,678371829,996937306,387496328,235100473,649738138,898466097,407604566,529571196,854241604,278302571,998505802,446819056,674284500,819876855,166920818,141076505,198714798,612486167,700718656,391542764,645522969,999548402,477521930,505594128,951413460,665706996,664760847,514065631,101412856,896853242,846607158,200777626,574295366,319187233,35082951,154276754,661540946,477502005,725521461,655895869,355739306,678518235,192498927,173855439,165487057,456827891,29739781,258791510,428391536,531278508,763339063,168419466,868466779,365442496,722700915,698024831,661694896,590895521,463918561,314021483,842517461,302478861,688035954,232366905,961113387,828397927,320767088,508824951,735431855,426624942,591154618,76109991,328101502,665849594,385026767,472223460,891042862,342446307,246096980,406346591,527916773,998217449,259406947,423766371,70821164,405777406,55383694,177073358,520289379,953144123,598695706,882910945,148088957,127593638,565277379,746539154,329767227,652704430,458041013,532152689,943689273,198275398,746095184,296234153,567962117,585755152,914398975,392347020,605551281,526603889,299409566,554048889,207398267,231395425,761907727,542580836,511879608,477430193,68337675,420520000,90537136,881957003,293703031,601859481,982488742,687784417,778652850,678832519,41785321,569973809,659662535,165782945,800601868,946694094,798296737,530957874,155643917,78799254,840622486,223970795,8485824,28964502,945323929,340741737,34732972,603272702,607664609,673843887,72927622,72008879,956047483,777700919,664146374,919751340,24878865,460990642,148315028,357424860,741503968,317099797,829104947,566163262,167156841,600465821,697110956,353280563,96899824,252662837,7893272,120921870,514991140,889565398,248085788,692098208,933756018,425884820,645876939,21896522,887680365,852291368,441176290,66262852,156844282,636816511,124116038,842190463,966404243,788403295,940665695,989549719,828820601,250027891,298842711,270122615,174406628,2135753,46964343,877722285,279439798,165618275,192058083,45442983,312364959,141906913,672563513,989170139,871761564,190705367,121151650,442674415,781847041,760092753,473161041,775211615,133246058,75660748,105103896,314024082,699785781,135093763,341755252,356922254,545665155,516050961,683432858,554252611,980150710,3809442,280205936,618138190,143338978,788688497,335020131,301878323,399780697,432488867,731503292,78552686,970031600,634518684,501384623,44760617,805930887,940017151,197445475,720089820,203297800,544488568,323817708,248584685,735890324,467669218,62087980,905110436,891634369,10533152,23990287,611609572,542849349,924481418,4214140,12444440,736717687,401040098,157325666,367265798,451303368,161946117,507226623,459380345,866752949,897212880,806566311,378612676,64109037,642588978,48895787,519942317,136434876,29861018,573535950,758513856,146511954,726260224,399742173,745185429,190644678,862659605,284303510,93481167,54655028,557236856,907886262,184073171,469064478,587825128,561305691,650561892,494974250,427901529,127482682,377916374,510109927,601476427,61867544,242080407,336520612,817284238,357107802,831433550,401060931,360517611,209089633,445727073,273046955,399724590,819481328,852463455,937507966,711938640,599907247,439538485,872938446,909790292,180096711,650114586,529418274,560890330,882019804,509410348,501169449,344529911,730231156,858736987,621110787,4459667,873347202,696188165,506145206,73642136,147943559,379421898,654783213,956981417,651743441,639780318,78577382,593374499,830162763,301755654,792435305,117970707,932913412,832255341,132217712,628450721,227843471,107653574,884405265,36919433,120185702,713929739,355335147,939617354,328252980,400671521,3750430,291712420,161768446,204525632,110812455,655316736,810170496,495246572,411585803,110977744,480012790,837775503,421678299,537576259,215096702,255249918,999498556,551932245,400608941,789747750,660573311,224129758,960267908,823407521,663185418,71834349,194653367,479377667,556838543,30135513,434301805,554957759,766164228,423757881,77024851,446918481,86585877,167204113,199048610,165982142,29904724,116697346,136404554,840462862,989156366,172584177,788447557,850533198,955155103,112270248,718115841,395066112,581133650,802794769,427918059,178030850,977926539,732960431,699231435,65702561,976789228,525323070,86516285,991883842,476155672,748102517,322281052,655855568,406512465,964314311,364509254,195333797,846194973,949738561,886254842,502143379,589271740,437138529,387943507,470570144,725480162,669584085,390652246,75608046,254775915,749807458,399204577,389518587,676947848,2017"百度之星"程序设计大赛 - 资格赛-度度熊与邪恶大魔王(dp+后缀最小值)

2017"百度之星"程序设计大赛 - 资格赛 寻找母串

2017"百度之星"程序设计大赛 - 初赛(B)小小粉丝度度熊

2017"百度之星"程序设计大赛 - 初赛(A)

2017"百度之星"程序设计大赛 - 资格赛 1003

2017"百度之星"程序设计大赛 - 初赛(A) 01,05,06