为了解决经营不善的危机,小 D 决定大量购入御守。 御守有不同种类,每种御守的购入数目因为灵性不同分别有不同的限制,小 D 需要统计总共(各种御守购入量之和)购买指定数量的御守的方案数(同种御守之间是两两全等的,不同种类的御守是不同的)。因为方案可能实在太多了,因此小 D 想求出答案 模 P 的值。
一星御守最多购买一个,也可以不买。
二星御守最多购买一个,也可以不买。
三星御守最多购买一个,也可以不买。
四星御守最多购买一个,也可以不买。
五星御守只能购买九的倍数个(含 0 个)。
六星御守一直到 M 星御守(M>5)可以买任意多个,也可以不买。
一月亮御守最多购买六个,也可以不买。
二月亮御守最多购买八个,也可以不买。
学业御守只能购买七的倍数个,也可以不买。
一太阳御守只能购买偶数个,也可以不买。
二太阳御守只能购买偶数个,也可以不买。
三太阳御守只能购买偶数个,也可以不买。
四太阳御守只能购买偶数个,也可以不买。
只能购买以上提到的御守。
现在, 小 D 要求出共购买 N-M个(N>M) 御守的方案数,计算出结果后要模如题目背景所述的常数 P.
(N,M leq 10^9, P = 1004535809)
Solution
根据题意写出生成函数
[egin{aligned}
f(x) = &(1+x)^4 (1+x^9+x^{18}+cdots)(1+x+x^2+cdots)^{M-5}\ &(1+x+cdots+x^6) (1+x+cdots+x^8)(1+x^7+x^{14}+cdots)(1+x^2+x^4+cdots)^4
end{aligned}
]
即
[f(x)=(1+x)^4 frac {1}{1-x^9}frac{1}{(1-x)^{M-5}}frac{1-x^7}{1-x} frac{1-x^9}{1-x}frac{1}{1-x^7}frac{1}{(1-x^2)^4}
]
化简得
[frac{1}{(1-x)^{M+1}}
]
于是所求为
[[x^{N-M}] frac{1}{(1-x)^{M+1}}
]
考虑到
[frac{1}{(1-x)^n} = sum_{i=0}^infty C_{n+i-1}^i x^i
]
答案即为
[C_N^M = frac{n!}{m!(n-m)!}
]
我们需要用到 (10^9) 范围内的阶乘,考虑分块打表,预装 ((iB)!) 的值,这样每次阶乘计算的复杂度即为 (mathcal{O}(B)),考虑到实际情况,取 (B=10^6)
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int fr[1001] = {1,101902910,356957657,777799867,459488968,660944677,811033589,98027646,817753166,653120991,162919107,30731223,982121070,753885206,535567262,867307661,701552070,984844395,666305879,245417729,236127804,913186282,137933631,270904009,692720582,30023949,838808013,708514329,102193809,521782056,927887736,215185037,742905498,323419051,815398905,63139231,134608199,841528295,791100567,516629667,191354878,164231167,894905125,247642849,786929808,242303249,314252241,292014554,679931589,219153337,843272563,854346331,70116717,716422363,976426320,702096974,786081193,153954247,651032529,708322339,154020745,921882787,657943992,904010041,626618038,632110578,432897000,328753915,327856528,347515080,566632606,880436061,526470932,144033955,123714451,442776007,502176335,870291968,279988124,688376089,4675137,735278299,625953447,616631441,77206328,618616193,777294015,140292011,45997493,693666613,846342199,168157637,763980877,579204830,361329367,854340415,297752672,657029319,48598835,29688062,585625036,586750163,1000867721,261836860,232033375,443569907,918263266,984044443,245790030,237085185,559215312,724010589,500597161,706136126,482791428,246462147,19996965,363334140,537868986,724972954,640367408,448931437,531762462,518061439,526165934,921038105,642863818,41890256,151131921,747448140,441668916,60229985,676305793,963836384,867751683,416882359,528764100,808343121,124066477,545115363,748329585,775234832,387560774,517206328,623438504,954466763,492608161,178369742,944161928,168664101,562905290,442297037,977041201,756767732,308011024,993623477,553427141,670351489,591470816,661470802,22335312,879449944,912788779,554732523,769527117,627934691,938676291,933654378,410868285,422841119,137534166,319314474,93149170,611545759,20584657,129337704,663563807,890359518,668588382,499733499,747260214,210229334,418389764,299363687,585913413,317505840,996548064,908306233,248612534,666169794,878798738,733006657,278852777,690369506,342002504,816008474,289293824,110601826,580231856,96365872,977341000,197304650,132431350,173159350,663168201,917929117,44199622,411318024,152705418,437010004,428608656,796961530,390165266,1004050327,795322935,861770883,889948816,254049728,522085799,4861505,11069564,590174262,387669516,505170408,884163521,220865455,768660291,79798213,382394019,767485644,976456588,352012887,793806942,918916680,549162906,221740807,871325174,844383286,976701203,278794670,189497845,618315725,544032275,369914028,45917448,235691047,291982711,844204267,462789001,103769548,289810713,252163860,213265968,14772738,547201533,361839080,328688345,804524795,22665591,879991257,495636528,183239508,187166055,700674708,604143748,360837614,784794984,358043924,79500428,170638764,632139535,305198015,543849964,611829285,399578299,804410653,556894966,141918142,8679571,275325700,547711273,925257501,861534087,767711443,971431351,519979577,23257014,426081068,93640476,27889573,803010149,363800271,499395844,250222239,76869833,102790200,680864157,481128395,392952910,916756929,984383309,404856289,909988069,144239970,22348936,126585762,152383683,824592132,925687334,276831070,473105142,735210143,809485745,382323053,986117058,668801511,184033115,529175059,84892694,535506090,143131388,964605784,958666429,495682144,48053818,803123590,208620606,762425368,75250108,318950007,851880334,620319242,279784955,705446569,899973485,780524907,269129252,352294565,868411041,893615953,624006337,612592368,940369253,886353042,51552945,60430514,430911309,283235921,616251536,750954605,438634958,743691048,118180904,825640083,480257594,335849230,307156146,183095229,850862520,881844719,245181136,212483134,302377430,336465768,372773554,993509915,7207869,500386381,11550239,296079096,218543669,394304591,388877051,15834491,341723777,182778067,985334519,854582928,814767757,388872157,125865062,987654302,677287678,153427935,796332066,87069400,914158337,697309039,914313752,171431160,775459971,1000989778,870707268,77219622,197152304,219500823,875149452,690133536,601162062,163643844,268348313,353533044,423775434,596067175,455447039,717999470,156041986,538750213,37964653,230337009,509045191,485493948,789440168,132729702,987404610,993418561,235237400,224421881,233189590,332407184,36511120,558162519,84428749,627746902,503524275,559080578,602586644,135531446,502795953,367828410,634348676,252989692,719359488,889181402,780741192,460797514,764355930,164781633,572105557,376683545,944985261,228625851,465387939,959471411,318365775,25395316,13061618,164766137,105266003,177384452,793740139,788463870,501698256,689756935,17630739,765138329,8301101,382702994,796124692,410956838,688126329,990133287,794353772,436433354,428927214,64210561,805801218,487180404,233831016,141350816,22954182,735711587,683815914,348475493,679154621,529483046,142702033,125250717,855458798,303542573,446736746,366845103,390200212,765623264,403046684,663280409,180873483,715709583,86250882,284744594,451966542,413760128,923471003,982019624,52630936,184266463,708245291,552932641,905910466,317718255,278302600,823433559,585192638,586698258,189750,454920986,510951261,733157625,245027631,656175915,108496945,810474733,505591663,1004533225,115057272,114437151,968871496,403063750,208264108,310411947,599938817,159269355,928411224,518611714,415716866,588200406,632790364,527154772,252340561,300732415,533145263,327575305,456748476,105047329,711038328,231391229,640372549,349016853,750788735,605839587,168305918,753886446,168015193,647033699,325145493,634552670,806545509,447682678,226863129,923293579,781279768,281441466,684079989,619490600,590539136,216761269,610936931,864246074,339505145,427719888,815964885,609504111,429646881,205885297,34139064,620529034,266954519,117197391,538651109,622957993,354978679,670362323,162424676,67510001,385327299,302763220,632709019,853112987,989536267,774607709,680476673,879054449,966487087,296915794,107956361,728636642,925476350,433739610,79173956,114506427,238701766,7503426,217645599,897973278,121333100,306044357,367082459,842214519,536432263,618447737,909403380,482600156,141651006,364575132,540533924,909934851,709685661,604490721,557071535,659112223,78392571,626080285,882951887,296531738,604290768,885117258,722386762,396965960,231430072,995253616,212022020,658179936,774366496,158757141,823653560,922996929,161971523,910900086,218159936,234136120,570650772,679730529,883117681,508240098,645479292,667885756,809719152,716465677,871062539,864801010,486831413,104663243,739698541,733142188,377613495,794667776,564244191,890152995,482917570,26960807,11614863,149196935,735447417,822792131,166361633,780136397,500488265,779837766,426936478,574111507,531369161,839053401,306345653,630179564,624654308,954265323,4351672,616983798,272645028,7089833,746586886,372483972,11469737,230874743,564148719,469279477,137381067,341589990,928974068,779153555,841216621,448543278,40025546,361259681,672676214,487845091,58633163,624184609,313468067,753258446,553636948,219304039,668057005,516803884,2789680,97543346,680961993,26300140,264081902,724405055,926539760,260497956,953456526,395834890,921250524,303873541,458391006,486054214,132588404,647265977,707703566,615850596,856456565,292761750,835403267,237128566,833998670,331823394,991641111,483573937,938263045,732388688,25335117,217228312,540643006,448723670,468791157,157740282,124704791,786536469,454712522,568078466,43466512,32964666,433818984,874945027,815549496,13912679,850455766,416296863,289722830,518106643,373385286,757361285,534645480,86297931,625979363,946483138,1001118350,641836458,277528550,524130414,238433985,730881709,523043572,174813872,193672600,632664975,532329695,395615698,163265352,378292265,630331022,529188109,158719119,701308974,734781188,959368935,146019006,277575501,146942724,319384534,696042270,269223143,959941041,740831702,469490587,332259229,357921213,14304603,454773515,432297368,630241271,399453687,659979733,110283210,379721491,415476528,361401290,868190210,417120914,820447489,489945466,934277183,521072654,677221797,368275726,921924071,927655117,913749911,553303210,668368897,395194966,197532096,939730394,159980901,878597200,436961223,9604030,731065715,88730680,201457810,443767149,434370094,473684070,886795154,109106097,621492305,99685970,973882448,314994194,690981646,33744853,818235243,868275831,617474757,149160186,963175112,733710134,514623052,149300251,64135843,831412165,586401229,988039381,228478733,485348309,983791236,509233959,126391665,556986836,392233145,922309422,841991656,959723886,569324055,683742023,248508746,675159404,668395943,673123745,341392122,378357146,505975037,807922024,268868858,466889756,86249088,140056819,808965728,712874765,402571974,841226977,717664915,509691235,614157685,484804399,297583494,36130043,599237109,799621477,476871090,398712491,652345688,755684294,420335346,461652412,24018132,732819276,797346752,264608954,713940585,86313083,153746880,332546238,347644710,673557230,531576601,191635908,230307160,771574540,829517335,913523451,67724728,852823549,146607359,778235013,345114944,825213087,568005880,637761951,385712728,979025571,612515001,896793909,625997653,238625238,174196415,165677783,777965776,630369946,54816293,750751654,337441625,175647840,255730664,941178527,335088527,935028315,165745879,3516760,93398549,351435851,938699563,459916151,388928770,797596659,637262373,273580510,568687059,352305021,906436762,63720049,828681824,828649421,511841065,769622726,340880360,503402170,312175581,213054345,835446716,970882441,810146930,84041764,571808141,405942878,649498935,308534744,294654781,434518553,535980037,403704387,377456588,539010050,193122764,847905993,991902484,475602348,305947080,842229725,564804840,722090621,978573205,867494095,60478436,985031795,341946875,356263084,364500856,528909202,230192737,564828880,334098436,959858542,960490546,573928022,243866184,890335756,898995748,876927036,234323775,680590368,226403496,736326412,773014443,340262148,969825020,466259401,821747109,327198548,111906965,259890389,223343321,494682407,802757698,53244208,731384680,177722027,849476585,9608478};
const int mod = 1004535809;
int qpow(int p,int q) {return (q&1?p:1)*(q?qpow(p*p%mod,q/2):1)%mod;}
int inv(int p) {return qpow(p,mod-2);}
int frac(int p) {
int k=p/1000000;
int res=fr[k];
for(int i=k*1000000+1;i<=p;i++) (res*=i)%=mod;
return res;
}
signed main() {
int n,m,p;
cin>>n>>m>>p;
cout<<frac(n)*inv(frac(m))%mod*inv(frac(n-m))%mod;
}