[BZOJ]2142 礼物 扩展Lucas
生活随笔
收集整理的這篇文章主要介紹了
[BZOJ]2142 礼物 扩展Lucas
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2142: 禮物
Time Limit:?10 Sec?? Memory Limit:?259 MBSubmit:?1788?? Solved:?748
[ Submit][ Status][ Discuss]
Description
一年一度的圣誕節(jié)快要來到了。每年的圣誕節(jié)小E都會收到許多禮物,當(dāng)然他也會送出許多禮物。不同的人物在小E 心目中的重要性不同,在小E心中分量越重的人,收到的禮物會越多。小E從商店中購買了n件禮物,打算送給m個人 ,其中送給第i個人禮物數(shù)量為wi。請你幫忙計算出送禮物的方案數(shù)(兩個方案被認(rèn)為是不同的,當(dāng)且僅當(dāng)存在某 個人在這兩種方案中收到的禮物不同)。由于方案數(shù)可能會很大,你只需要輸出模P后的結(jié)果。Input
輸入的第一行包含一個正整數(shù)P,表示模; 第二行包含兩個整整數(shù)n和m,分別表示小E從商店購買的禮物數(shù)和接受禮物的人數(shù); 以下m行每行僅包含一個正整數(shù)wi,表示小E要送給第i個人的禮物數(shù)量。Output
若不存在可行方案,則輸出“Impossible”,否則輸出一個整數(shù),表示模P后的方案數(shù)。
Sample Input
1004 2
1
2
Sample Output
12【樣例說明】
下面是對樣例1的說明。
以“/”分割,“/”前后分別表示送給第一個人和第二個人的禮物編號。12種方案詳情如下:
1/23 1/24 1/34
2/13 2/14 2/34
3/12 3/14 3/24
4/12 4/13 4/23
【數(shù)據(jù)規(guī)模和約定】
設(shè)P=p1^c1 * p2^c2 * p3^c3 * … *pt ^ ct,pi為質(zhì)數(shù)。
對于100%的數(shù)據(jù),1≤n≤109,1≤m≤5,1≤pi^ci≤10^5。
HINT
Source
[ Submit][ Status][ Discuss] HOME?Back
? ??擴(kuò)展Lucs講解: Click Here.
? ??這道題就是求, C(n, w[1]) * C(n - w[1], w[2])......用lucas即可. 因為模數(shù)不一定為質(zhì)數(shù)所以要用擴(kuò)展Lucas.
#include<stdio.h> typedef long long dnt; dnt Mod, n, m , w[8], sum, ans; inline dnt mpow(dnt a, dnt b, dnt mod){dnt rt = 1;while(b){if(b & 1) rt = rt * a % mod;a = a * a % mod, b >>= 1;}return rt; } inline dnt exgcd(dnt a, dnt b, dnt &x, dnt &y) {if(!b) {x = 1; y = 0;return a;}dnt d = exgcd(b, a%b, y, x);y -= a / b * x;return d; } inline dnt inv(dnt a, dnt b){ dnt x, y; exgcd(a, b, x, y); return (x % b + b) % b; } dnt calc(dnt n, dnt p, dnt pr){if(!n) return 1;dnt rt = 1;for(dnt i = 2; i <= pr; ++i)if(i % p) rt = rt * i % pr;rt = mpow(rt, n / pr, pr);dnt res = n % pr;for(dnt i = 2; i <= res; ++i)if(i % p) rt = rt * i % pr;return rt * calc(n / p, p, pr) % pr; } inline dnt C(dnt n, dnt m, dnt p, dnt pr){if(n < m) return 0;dnt x = calc(n, p, pr), y = calc(m, p, pr), z = calc(n - m, p, pr);dnt c = 0;for(dnt i = n; i; i /= p) c += i / p;for(dnt i = m; i; i /= p) c -= i / p;for(dnt i = n - m; i; i /= p) c -= i / p;dnt rt = x * inv(y, pr) % pr * inv(z, pr) % pr * mpow(p, c, pr) % pr;return rt * (Mod / pr) % Mod * inv(Mod / pr, pr) % Mod; } inline dnt Ex_Lucas(dnt n, dnt m){dnt x = Mod, rt = 0;for(dnt i = 2; i <= x; ++i)if(!(x % i)){dnt pr = 1;while(!(x % i)) x /= i, pr *= i;rt = (rt + C(n, m, i, pr)) % Mod;}return rt; } int main(){scanf("%lld%lld%lld", &Mod, &n, &m);for(int i = 1; i <= m; ++i) scanf("%lld", &w[i]), sum += w[i];if(sum > n) {puts("Impossible"); return 0;}ans = 1;for(int i = 1; i <= m; ++i)ans = ans * Ex_Lucas(n, w[i]) % Mod, n -= w[i];printf("%lld\n", ans); }
總結(jié)
以上是生活随笔為你收集整理的[BZOJ]2142 礼物 扩展Lucas的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据分析师怎么样?我们为什么要学数据分析
- 下一篇: Airbnb开源框架,真响应式架构——M