codechef World War 2(杜教BM/矩阵快速幂)

对前若干项暴力打表,然后杜教BM即可
或者使用矩阵快速幂,构造两个矩阵A,B。答案=单位矩阵*B*A*B*A…,矩阵可以满足结合律,设C=B*A,则有答案=单位矩阵*C*C=单位矩阵*C^(n-1>>1),如果n-1是奇数,则再乘以一个矩阵B就好了

#include
using namespace std;
typedef long long ll;
const int N=35,mod=1e9+7;
int n,m;
struct node
{
    int a[N][N];
    node(){memset(a,0,sizeof(a));}
    node operator*(const node&o)const
    {
        node ans;
        for(int i=1;i<=m;i++)
            for(int j=1;j<=m;j++)
            for(int k=1;k<=m;k++)
            ans.a[i][j]=(ans.a[i][j]+(ll)a[i][k]*o.a[k][j]%mod)%mod;
        return ans;
    }
}A,B,C;
node qpow(node a,int n)
{
    node ans;
    for(int i=1;i<=m;i++) ans.a[i][i]=1;
    while(n)
    {
        if(n&1) ans=ans*a;
        a=a*a;
        n>>=1;
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        A=node();B=node();
        for(int i=1;i<=m;i++)
        {
            A.a[i][i]=A.a[i][i-1]=A.a[i][i+1]=1;
            B.a[i][i-1]=B.a[i][i+1]=1;
            if(i-1<1) B.a[i][i-1]=A.a[i][i-1]=0;
            if(i+1>m) B.a[i][i+1]=A.a[i][i+1]=0;
        }
        C=B*A;
        node res=qpow(C,(n-1)/2);
        if((n-1)&1) res=res*B;
        int ans=0;
        for(int i=1;i<=m;i++)
            for(int j=1;j<=m;j++)
            ans=(ans+res.a[i][j])%mod;
        printf("%d\n",ans);
    }
}
/*
#include 
using namespace std;
#define rep(i,a,n) for (long long i=a;i=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((long long)(x).size())
typedef vector VI;
typedef long long ll;
typedef pair PII;
const ll mod=1e9+7;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head
long long value[35][105]={{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{2,2,4,4,8,8,16,16,32,32,64,64,128,128,256,256,512,512,1024,1024,2048,2048,4096,4096,8192,8192,16384,16384,32768,32768,65536,65536,131072,131072,262144,262144,524288,524288,1048576,1048576,2097152,2097152,4194304,4194304,8388608,8388608,16777216,16777216,33554432,33554432,67108864,67108864,134217728,134217728,268435456,268435456,536870912,536870912,73741817,73741817,147483634,147483634,294967268,294967268,589934536,589934536,179869065,179869065,359738130,359738130,719476260,719476260,438952513,438952513,877905026,877905026,755810045,755810045,511620083,511620083,23240159,23240159,46480318,46480318,92960636,92960636,185921272,185921272,371842544,371842544,743685088,743685088,487370169,487370169,974740338,974740338,949480669,949480669,898961331,898961331},
{3,4,10,14,34,48,116,164,396,560,1352,1912,4616,6528,15760,22288,53808,76096,183712,259808,627232,887040,2141504,3028544,7311552,10340096,24963200,35303296,85229696,120532992,290992384,411525376,993510144,405035513,392055787,797091300,581202867,378294160,540699887,918994047,393807,919387854,920175468,839563315,679914237,519477545,879306012,398783550,157395560,556179110,870970230,427149333,169089779,596239112,934418670,530657775,399495101,930152876,729143071,659295940,117582068,776878008,12042137,788920145,813004419,601924557,227933381,829857938,285724693,115582624,687032010,802614634,176678640,979293274,332650547,311943814,977244908,289188715,243678517,532867232,20224259,553091491,593540009,146631493,333711504,480342997,147765998,628108995,923640991,551749979,399031947,950781926,748845813,699627732,197319344,896947076,291585757,188532826,771704340,960237166},
{4,6,16,26,68,110,288,466,1220,1974,5168,8362,21892,35422,92736,150050,392836,635622,1664080,2692538,7049156,11405774,29860704,48315634,126491972,204668310,535828592,866988874,269806326,672623785,615053889,557483993,730021868,902559743,535141340,167722937,870587214,573451484,17490168,461528859,940547886,419566906,779681691,139796469,59274622,978752782,16780172,54807569,126395310,197983051,522361412,846739773,215840944,584942122,385725181,186508240,758741661,330975075,420691804,510408533,441508863,372609193,186727242,845291,188417824,375990357,940398538,504806712,950011955,395217191,740446330,85675462,911797254,737919039,387635318,37351597,462338512,887325427,236989352,586653284,410295913,233938542,878172997,522407445,922987880,323568308,570124489,816680670,203485815,590290967,384067742,177844517,739756776,301669028,343094825,384520622,112136062,839751509,791639073,743526637},
{5,8,22,38,104,180,492,852,2328,4032,11016,19080,52128,90288,246672,427248,1167264,2021760,5523552,9567072,26137728,45271872,123685056,214228800,585283968,13741561,769593458,797076580,105856933,700010086,17580878,417601043,470343677,305545756,716576780,327668285,477398611,132735174,564931000,830401348,525194334,185997016,761580011,133574036,418314055,685462127,940404285,311328525,132541359,755198409,152822479,663219290,121686720,448125293,813185453,709436032,148992370,567864427,14841530,150570377,195094967,496235721,81520615,73992050,318553895,466537995,422199673,355275656,621874668,332425973,198049963,862901909,457051791,182855595,554010961,919722151,581755020,421199308,166464354,8862963,508256025,525981951,50750012,102713907,254963943,460391757,225283579,146067086,821917823,114051988,579805443,807909419,547325734,163144558,805121753,131410862,546776107,809597831,449926138,69121786},
{6,10,28,50,140,252,706,1272,3564,6422,17994,32424,90850,163706,458694,826538,2315908,4173122,11692828,21069748,59036122,106379416,298068500,537100878,504923211,711777922,598232881,691542551,362849724,127466567,690857157,18864583,929127189,167397214,483327059,37526929,945183580,407040094,183593346,422003119,858969263,534345173,181032393,503095530,974941417,768850434,803455765,631970113,127059898,450664038,720022001,312984097,488288253,256554505,456679418,425070583,18657237,580635063,316834599,615011961,264400813,211967027,20889120,777377434,120165261,219441402,880946779,641728290,705743461,530540143,949892139,194040810,51582287,153272442,265776516,479970745,286639786,307503056,442538423,598437060,487808117,533077811,796366,513784622,8276048,15755730,533482575,58689095,160311555,787140542,302732517,445153479,548319895,793907273,936568333,324816764,180543019,424517712,948736372,716929718},
{7,12,34,62,176,324,922,1702,4846,8952,25492,47100,134128,247832,705764,1304076,3713692,6862000,19541320,36107640,102825856,189997392,541066792,999761048,847078570,260715197,981249390,681739315,830924062,660554473,806164414,461850430,698682566,101276793,300721173,68521878,338011771,943658701,94344721,186834708,149703955,146867149,993652604,871895193,984738820,7282170,365790027,656540971,419127873,404708806,365598271,535062700,114538049,72052791,151335101,566171546,141113917,466446868,355050654,319377964,22339452,765351781,929486725,876684825,201095564,255767566,625793927,780753546,269395033,697929404,610195429,890888702,979785413,717802638,481904551,633144210,599023347,103279882,140388080,220130218,353538343,609322546,338093300,445024335,369284253,778635487,653864128,423535312,734188048,708954511,42138967,14203620,559584540,715910303,983685418,18731132,721618536,226822260,594935896,270569583},
{8,14,40,74,212,396,1138,2134,6140,11530,33190,62358,179534,337376,971398,1825556,5256404,9878658,28444312,53457520,153924494,289282822,832956088,565444171,507515634,471355954,392294902,842514245,998254141,75561128,305099902,454443524,443492949,657414356,747305543,505496118,848057887,606093508,402544514,462265115,123001455,449027616,680744451,699703979,309772078,385730169,687106211,759950012,499993655,690222660,603705395,450589924,384813539,822589911,897108105,792883724,423176005,637616088,60264999,221802261,389647527,819775231,123301424,5015765,289375520,127741206,534250419,489173815,329172719,661901696,313105943,606343917,817259894,623577404,555854412,181316587,411812081,452394641,669191831,229983142,590145260,207683285,854053865,836325529,149835323,630059914,676984627,993979135,618906960,97928564,253770641,696692185,431154537,334624453,91180707,704213981,768794754,293663078,445961345,852601586},
{9,16,46,86,248,468,1354,2566,7436,14120,40950,77830,225800,429340,1245810,2369270,6875420,13076800,37949150,72180950,209474200,398436900,156301043,199398136,382881858,140918916,234267505,19447281,497879642,956664917,655721739,213807456,734444018,316951089,437839738,357998140,527330807,702347860,892827705,723029762,571324876,315050610,559143186,740058340,599862611,128218548,899938278,916804882,921175622,947989355,413770301,122011956,501269635,643492637,735164485,630223748,120786720,917870380,122170251,966710634,146687166,820244216,749543326,586115779,155992168,665798395,863385751,848283834,706956245,991138773,100038456,506245658,913308872,447816456,23693474,783538742,304041231,632569587,228681767,722799049,309840677,517982797,240723285,555250414,452259409,534143709,452706980,609413335,631782723,137420487,100783640,339864048,374267114,887707277,409108611,756699272,727668331,444593485,727520165,676854718},
{10,18,52,98,284,540,1570,2998,8732,16712,48724,93366,272358,522244,1523890,2923092,8530852,16366860,47769756,91658222,267534304,513360482,498447381,875396348,393106462,105941408,12608079,215590596,336805249,336774027,67203225,624598688,542115314,676084425,461460064,314119805,550313410,923804269,135260135,820108781,236464928,610907269,273445039,802580905,146070580,726685113,486329068,880556033,618714874,143384055,678570265,46193497,289541225,335061851,79688652,227255246,21973881,399888064,492872844,503060200,736508223,63850258,92509647,30130826,742568871,599236277,655021962,805756574,626066135,738373453,815209035,843458087,51505197,337825391,818008650,360799755,533441127,876636376,828097252,480057905,291671377,541183859,732390567,985844012,149534887,174846930,530074081,371779448,570881760,189724181,586198411,988095236,377269860,163052425,305231558,428681825,523730526,319726673,487859564,687794604},
{11,20,58,110,320,612,1786,3430,10028,19304,56500,108916,318996,615456,1803300,3480988,10201896,19699264,57742192,111517648,326908376,631430256,851106785,575694179,482882330,250111956,368540083,686904946,238900384,548969281,360421600,891617918,893462713,574684100,503488601,272983623,445407906,397666319,417061099,644440752,320455071,451206075,972694135,805116582,853848452,577755384,408836782,604553448,168588408,351253141,106468382,868748027,677157809,49985657,547762062,424383075,495847770,276524638,285262636,677614982,486101087,416925648,244903911,144364233,168980107,804586009,426073585,649601179,181680567,741989019,426209243,518710217,622675853,853829601,906314857,33237549,424576380,891811680,718674442,809962708,698692421,545758862,795353911,127447598,284763663,849339438,781733151,666778447,512262866,564440605,735835325,204770549,487821408,550038628,814362561,174058698,904421981,940264508,59936939,798042784},
{12,22,64,122,356,684,2002,3862,11324,21896,64276,124468,365650,708750,2083116,4040314,11878868,23049236,67781012,131555022,386918048,751096330,209258504,289175352,616836230,496885656,61684358,922164727,614904747,258751909,252146475,673045897,401838763,527806185,784458515,843646175,680018430,207360538,165828596,864023453,156339306,183738243,947356938,996583860,750382519,302257222,704114633,38398482,728700544,754644771,808801278,528882148,342627350,49532629,542746851,668480379,536386009,225028175,205189527,549747436,39886116,728745084,258380170,422667885,934026488,596758655,184115872,980601830,782839156,139592503,5816900,601311218,356256011,680877378,582172055,793917665,974224368,204242241,559728152,437181113,463260497,974689485,780927137,394790096,653684892,855944965,853815771,161174544,877275670,647833487,682502705,138830139,73926690,738911954,839282164,711807038,708934161,33852217,543792385,711330202},
{13,24,70,134,392,756,2218,4294,12620,24488,72052,140020,412306,802060,2363032,4600064,13557796,26405680,77845950,151667894,447210050,871512208,570080282,9350269,773834320,799087324,941022250,591641467,422829762,229407192,744486786,146943498,62355045,138951814,36012238,643812135,574082301,635737134,25421186,407948070,778081987,708302663,58858858,487587910,832049561,628461102,979826729,985189735,246583913,352404973,205075167,682323495,110855018,656428394,494196157,683310017,469704703,667836007,123069692,3758942,993411149,163295048,72003677,769434072,907636681,42427614,747697836,160756859,447190634,601865005,698083938,484879243,170738076,636752328,104999832,196936014,43000978,430470642,943242170,427305707,716658479,152872580,251029243,709453644,891625083,485743706,160551045,325414698,60844424,297471910,724563273,312211518,100588025,124627419,663299283,232102279,868160578,945338731,382376978,56835295},
{14,26,76,146,428,828,2434,4726,13916,27080,79828,155572,458962,895372,2642966,5159916,15237268,29764202,87920002,171809650,507616256,992252034,932103480,732692269,942082090,129449871,917355007,494831917,17493332,41370731,326983808,553430248,171215588,847672722,648001737,464986230,983086768,897660060,14020286,538887808,37906419,312346804,987374766,905459767,395388326,437208872,887396637,369891022,536947840,162996022,351805953,382538040,21276522,647961827,467413332,592550724,721767673,916457224,881170874,197322931,132392753,181316688,245566483,587351469,917131821,536267843,663416064,202328316,972935858,889310253,432961823,273489944,277446057,503638374,375309057,67136183,114054802,723156192,888809727,688250172,486298792,722419139,680531213,824275739,802923084,885856541,848129887,234363004,407430510,885146171,664136382,512485453,799466953,286157815,898359633,938750285,943682653,944979713,462089718,655922787},
{15,28,82,158,464,900,2650,5158,15212,29672,87604,171124,505618,988684,2922902,5719786,16916862,33123288,97996820,191961228,568064080,113119521,294623695,457423122,115446547,473218413,941207574,516828329,21532184,841251254,241145871,781985560,219855237,141608591,226415114,284792399,494041580,671627450,274921830,276688445,561574276,821400361,837891488,707346536,139428042,756071872,366819439,485688814,781136217,325536026,903421439,448405247,614853277,395614459,341277188,4486155,996100556,631297084,344409672,257923855,556342905,362493442,737763876,683930092,338067656,917837269,316045225,516811860,99695255,231326619,909892406,497887297,226920640,861165138,373319690,478221264,183020880,946090220,505612361,355637642,783211451,685234782,435845159,631979432,314561326,282705706,341171908,181403178,668635574,40287537,751540144,277868201,467812425,942342344,406678151,58836109,74737040,16723088,245527878,869375491},
{16,30,88,170,500,972,2866,5590,16508,32264,95380,186676,552274,1081996,3202838,6279658,18596476,36482498,108074346,212115718,628525368,234032423,657331357,182714733,290967319,822939891,986871229,595680319,226749132,143569289,887204527,195585863,404949644,717466144,434806321,157258495,871461176,943855844,240097887,673846056,991888367,703072721,78789454,587115720,508572585,578794735,435372956,665764346,297019401,167287491,618241353,348864226,419607939,115508269,677978053,862784538,438183365,794806103,989704601,822005556,513804283,134701714,54499292,50331960,998206906,109418179,116106016,79141623,463616568,78634010,140097212,178532739,853121404,543878741,732763452,956623805,881676857,109372836,22330598,370449614,538521141,296169869,817230047,668894535,180083084,401206937,947533238,376366109,731266892,207239604,429908453,640160627,554367094,574342741,753985946,362724727,727087876,92525921,28692232,712992079},
{17,32,94,182,536,1044,3082,6022,17804,34856,103156,202228,598930,1175308,3482774,6839530,20276092,39841728,118152018,232270938,688990446,354959689,20102746,908213041,467324333,175113526,41870645,700048865,525028322,687429387,386642769,738524691,932167954,56067080,728882086,842533014,633483009,866206755,393705362,37299564,669630264,687805632,86967115,628937043,732129746,62694948,240028257,852860815,863658147,87536061,99438072,12448274,371861715,960668047,308828090,556401485,618182371,38373849,119715026,465811091,315762825,349955623,650856658,162333559,584422279,592905911,781905459,76907669,15886589,265403260,907322945,970684701,678397847,469586087,536580609,96760381,836400555,974905919,970000001,99717855,507076435,989972668,640474751,747578970,358481169,831261086,999559960,167090765,276928932,742983287,870294598,770402374,312268246,740939000,496026503,695686126,511240743,200027396,937444422,585174266},
{18,34,100,194,572,1116,3298,6454,19100,37448,110932,217780,645586,1268620,3762710,7399402,21955708,43200960,128229712,252426306,749456424,475890917,382893392,633780136,643977203,528217836,100575536,815107519,863671472,340689496,283522941,309203863,85674485,435684394,205765839,46817876,178441912,825393385,792945206,958926009,247853674,926171372,353054241,633141367,490586411,395961003,292168803,889083733,605887100,10572430,892489654,200099061,17802028,66363110,995487292,940673289,256675699,278232804,683645791,440029035,59087103,900062608,235437393,337772870,679835334,535823175,176340707,718538695,705769748,225207817,468308889,82570586,884914407,254241420,459197142,39867130,629040544,531064109,272167256,406448336,393470221,557643731,118301631,350906717,335973853,997691492,473671195,481163484,371018978,213883434,131025028,488219217,671908692,304172157,266223676,610835995,133412346,176945348,777662965,797044301},
{19,36,106,206,608,1188,3514,6886,20396,40040,118708,233332,692242,1361932,4042646,7959274,23635324,46560192,138307408,272581696,809922574,596823069,745689098,359367603,820724499,881644782,160634767,934323469,218652391,40449461,354763871,349092401,938073496,194349897,115974945,702217445,375607201,343099857,560996919,252264894,809701369,975079543,381291478,480534817,266383072,141927462,53645996,470125891,957921302,501861314,583538927,800948281,399854418,85129508,774946746,453631002,983457948,610319980,366347194,731179480,325739973,125252822,399290212,172161615,923878240,969225124,180224072,474978493,548991925,964249981,548738222,379010854,702888263,391556531,284559712,189580460,130512846,678707453,158701189,243978405,540167602,831726613,184124194,808683062,733281011,546943321,398469108,73278345,617098489,217016493,49101731,705149874,360342689,841119024,566558072,951148315,930384051,860140161,428610826,235763327},
{20,38,112,218,644,1260,3730,7318,21692,42632,126484,248884,738898,1455244,4322582,8519146,25314940,49919424,148385104,292737088,870388748,717755395,108485919,84960330,997498575,235172757,221145826,55029784,579769213,758670317,497772237,590923695,543232461,966276193,294490754,39698296,348354772,634258510,455096505,599152015,280424707,716281681,964138227,412438016,296933732,10169139,522634842,851891921,820917477,854771884,325198,843258483,452907895,342900865,928846015,128564205,895114512,271557223,402859308,838605341,832166669,354145153,396093841,725745198,659063374,355039901,243116473,327617414,420974571,791422686,614395687,208888854,799477145,448557639,865789729,307907011,453480700,758998960,949684443,914714320,892009750,531848869,93272936,726316315,681316450,629743680,927432635,670045981,838313846,986840391,306028506,324637199,762535602,956054525,991932843,95161387,209384783,495039976,304547527,698781385},
{21,40,118,230,680,1332,3946,7750,22988,45224,134260,264436,785554,1548556,4602518,9079018,26994556,53278656,158462800,312892480,930854924,838687745,471282947,810554212,174279254,588728895,281792917,176224334,943009143,483697862,668362601,914362835,462759746,614200203,721762506,19565447,442348136,719492971,33931908,429649391,459779743,509023500,867849333,663981415,391299478,874870347,199778982,5200697,131086861,649419103,380483222,920796961,464388304,852305787,515751509,386911478,484422041,303260667,244036620,234820561,536981766,314549406,49655517,895890700,713579235,811910982,971888949,595756560,370263946,172119031,486502779,615673216,345322131,221984461,376277384,924667120,358074139,471921825,423753856,393162380,301008177,865541193,604225673,742811622,541039083,792749873,381088656,694037604,931533314,674534055,321550438,552539686,120918562,998294082,492531500,698149372,437804621,192663664,764715099,544346321},
{22,42,124,242,716,1404,4162,8182,24284,47816,142036,279988,832210,1641868,4882454,9638890,28674172,56637888,168540496,333047872,991321100,959620097,834080001,536148289,351061316,942291873,342476408,297563392,306919866,211039825,848793375,268619686,505524791,621621649,523113848,796210809,554835026,921935267,970329705,431619930,781798226,878790695,536546619,574906631,225502505,819419599,886473306,792944954,137052315,222519721,842569625,192308414,28377810,280368117,30006166,996639589,504637205,784399089,915621492,715529874,318758009,24239480,656274194,295164479,139105434,195523065,859897430,371602416,183730376,233794928,292281197,442299469,269867597,734555472,752216611,869216222,601772605,917361715,839723606,256772803,524036072,445437583,220042534,432194921,990079459,119518630,682740645,233247928,984987186,240795901,133901246,796705526,360711442,583638119,313570798,131256895,560842055,982164168,662633583,28892060},
{23,44,130,254,752,1476,4378,8614,25580,50408,149812,295540,878866,1735180,5162390,10198762,30353788,59997120,178618192,353203264,51787269,80552442,196877050,261742392,527843608,295856248,403168375,418940514,671021906,939101091,32459862,633688740,593418952,767627535,872487458,152185186,663283958,570656300,393464834,921170498,972210385,384978967,543760731,246641091,75686699,696870267,997134084,858272887,402895519,332843030,830115911,860418889,895395712,160673404,488250951,93230199,920258957,589378085,563200439,970106643,113296990,932717950,905565758,43214746,254229393,315593660,829632409,388890005,930197645,898110593,395047867,407153285,446214322,746817700,526550403,516445609,208586289,819840929,507134945,923928630,167062029,813921617,693414860,673647086,708726608,551435900,975502555,515589213,133471287,464668368,725697515,174845867,924388131,823629743,699392237,737514349,256907623,529716049,619693003,704207560},
{24,46,136,266,788,1548,4594,9046,26876,53000,157588,311092,925522,1828492,5442326,10758634,32033404,63356352,188695888,373358656,112253445,201484794,559674106,987336504,704625928,649420862,463862006,540326374,35172435,667364356,217098142,2256989,696659793,963573813,427110493,128014379,199518101,142628100,946617612,620061550,156380174,516237612,546797905,514165609,118934342,985531175,375768435,271051497,507323124,637458299,39685154,807846140,335105770,907728237,369275937,26826473,119083736,313379933,258155623,582389309,864497855,734825697,500644771,337085660,522811713,535115451,803237406,718055352,357930790,623521745,476022104,443388218,906347096,284888668,279353783,78495163,589580405,245638281,230578197,922543272,638016007,422453135,472340973,411889011,824900579,779763828,177783515,135240015,150517232,807568324,159686605,846957844,483406504,262020817,612323156,784564587,312704049,57086973,615035515,595158969},
{25,48,142,278,824,1620,4810,9478,28172,55592,165364,326644,972178,1921804,5722262,11318506,33713020,66715584,198773584,393514048,172719621,322417146,922471162,712930609,881408250,2985497,524555899,661713928,399333667,395678105,401999904,371860527,804711910,176244684,53566076,332444952,663040976,475088038,222373528,615789726,97226660,881827869,807108311,618154274,557411056,198053950,677886276,897372611,546095987,432097417,999645920,632599683,219429527,186301773,1594896,975199660,824974047,911644444,62354693,110461995,116068023,348803238,382316744,525164006,783962289,874617615,752387827,947245736,120240748,378372623,6307662,31131890,471588178,329155917,295625862,53440704,125569914,211281585,593716276,249834274,193524789,260391709,736107179,161741295,873832441,9598080,574140900,634030560,272127165,620099380,107284902,447629,449570922,779984152,616535604,693790168,690046285,563475223,346518821,483058125},
{26,50,148,290,860,1692,5026,9910,29468,58184,173140,342196,1018834,2015116,6002198,11878378,35392636,70074816,208851280,413669440,233185797,443349498,285268211,438524714,58190565,356550141,585249822,783101746,763496887,124002846,586965162,741740857,914141947,394083118,703373614,615708061,459266764,846412223,664948933,862016984,278647121,645289373,908059802,685117805,726686195,998988108,925573032,897045750,236307502,292803843,614831069,744752002,58383869,793673850,74094040,829871938,422537589,402982584,704788352,510399117,973951142,38245035,42818938,565613916,851569228,363975153,619663110,907695590,836954797,548018432,472112641,828269159,641581474,49085265,275699933,986824217,746877116,943844780,130866103,461438020,241844758,593483354,382077411,709371753,620484661,987858532,319976095,435099904,20292614,366393025,859520646,906615142,787276749,784710618,969827677,745635650,700207232,772739969,121133968,589463002},
{27,52,154,302,896,1764,5242,10342,30764,60776,180916,357748,1065490,2108428,6282134,12438250,37072252,73434048,218928976,433824832,293651973,564281850,648065267,164118819,234972887,710114785,645943747,904489594,127660396,852329614,771943730,111687026,23928257,613381344,360181133,924240174,367068074,585203505,635420268,801482011,101262139,617189103,714712927,683440748,89143632,233217924,623128330,961263019,58241065,252643448,767022631,646800538,781433105,980237709,296550385,625184914,214138297,993524148,548782869,736567542,21256246,635733760,964937024,284570775,442108087,742475840,450963380,260389571,521145204,802940231,954059929,328942199,892947870,313352235,547727828,661457349,112073074,19566034,872990856,164017269,42650407,739965485,386233208,392866699,597875460,717504577,764438809,240632503,356508065,221596788,422009133,271031123,752252424,651194608,891368510,794280094,766399516,743977600,588280571,744539464},
{28,54,160,314,932,1836,5458,10774,32060,63368,188692,373300,1112146,2201740,6562070,12998122,38751868,76793280,229006672,453980224,354118149,685214202,10862316,889712931,411755209,63679422,706637672,25877437,491823944,580656673,956924648,481646844,133796446,833052210,18905708,240246262,309633568,445559373,132309890,438404744,891741415,688480994,612084379,980611718,154098316,895609255,439658843,719306174,327869155,584288979,53886011,355648912,129042617,408758176,19365195,766811732,207232660,756561625,139826909,203722066,69822732,923198370,911662251,952329311,511566684,272221047,135924887,486079290,617529596,527549224,980818737,662774182,514533433,873537268,981487644,590722549,849135965,251944155,115700464,271517161,418916369,136638029,493940736,337272849,778416203,258566100,671265864,719813653,636546698,733654342,234841112,357850512,5436103,46834767,489056535,224447473,405538680,622657163,661145666,714759924},
{29,56,166,326,968,1908,5674,11206,33356,65960,196468,388852,1158802,2295052,6842006,13557994,40431484,80152512,239084368,474135616,414584325,806146554,373659372,615307036,588537531,417244066,767331597,147265287,855987494,308983764,141905891,851609046,243680995,52807693,678104284,558273555,262188126,343301845,798761295,652496253,140418281,542660825,102602217,750072489,454032203,815518621,500146700,166673833,837581123,579667230,610542830,92361514,834513862,516488660,938535677,822966361,178670471,994039097,763117823,560031695,318620505,40643224,39359838,726306211,204551077,797648737,14761369,432869329,589401769,582319569,780862368,99059701,138897447,594987261,915969930,606035234,761140479,794833896,490053275,17296656,629350907,248528036,239213287,613834936,992301168,321962747,711366948,376729694,103641365,344598755,986994405,764854783,462178548,133548142,570943916,177632003,700571230,348763642,685214815,175535191},
{30,58,172,338,1004,1980,5890,11638,34652,68552,204244,404404,1205458,2388364,7121942,14117866,42111100,83511744,249162064,494291008,475050501,927078906,736456428,340901141,765319853,770808710,828025522,268653137,220151037,37310857,326887175,221571575,353568296,272579913,337406995,876794732,217365104,251654903,515963175,50158877,204118843,107706941,974691543,983374635,308986885,293705781,130983846,470974980,563331284,809300267,60392045,352271776,594634827,899408184,271875489,596622725,423053582,616815117,528384513,985378951,387645634,764657900,876771022,44629056,130134187,250639397,610817259,24917985,67320513,629258263,968520872,720422141,245462021,468242044,366506314,82684993,595371952,657716536,332862592,906751329,115418951,984679035,653702758,457164329,908328675,792172200,918188758,126646994,323837333,439171393,544755186,455588945,387528747,994507923,312954505,584909370,237650831,870870115,961567581,221913995}};
long long _,n;
namespace linear_seq
{
    const long long N=10010;
    ll res[N],base[N],_c[N],_md[N];

    vector Md;
    void mul(ll *a,ll *b,long long k)
    {
        rep(i,0,k+k) _c[i]=0;
        rep(i,0,k) if (a[i]) rep(j,0,k)
            _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
        for (long long i=k+k-1;i>=k;i--) if (_c[i])
            rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
        rep(i,0,k) a[i]=_c[i];
    }
    long long solve(ll n,VI a,VI b)
    { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
//        printf("%d\n",SZ(b));
        ll ans=0,pnt=0;
        long long k=SZ(a);
        assert(SZ(a)==SZ(b));
        rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
        Md.clear();
        rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
        rep(i,0,k) res[i]=base[i]=0;
        res[0]=1;
        while ((1ll<=0;p--)
        {
            mul(res,res,k);
            if ((n>>p)&1)
            {
                for (long long i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
                rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
            }
        }
        rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
        if (ans<0) ans+=mod;
        return ans;
    }
    VI BM(VI s)
    {
        VI C(1,1),B(1,1);
        long long L=0,m=1,b=1;
        rep(n,0,SZ(s))
        {
            ll d=0;
            rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
            if (d==0) ++m;
            else if (2*L<=n)
            {
                VI T=C;
                ll c=mod-d*powmod(b,mod-2)%mod;
                while (SZ(C)

你可能感兴趣的:(codechef World War 2(杜教BM/矩阵快速幂))