小Q的歌单 | 卢卡斯定理

【小Q的歌单 | 卢卡斯定理】小Q的歌单 | 卢卡斯定理
文章图片
好久不见数论的题,碰到组合数+取模 马上想到卢卡斯定理 (其实组合数的递推式也可以做
Ac代码

#include #includeusing namespace std; typedef long long ll; ll f[101]; ll mod = 1000000007; void init() { f[0] = 1; for (ll i = 1; i <= 100; i++) f[i] = (i * f[i - 1]) % mod; }long long pow1(long long n, long long m) { long long ans = 1; while (m > 0) { if (m & 1)ans = (ans * n) % mod; m = m >> 1; n = (n * n) % mod; } return ans; }long long lucas(ll n, ll m) { ll ans = 1; while (n && m) { ll x, y; x = n % mod; y = m % mod; if (x < y) return 0; // printf("%lld %lld %lld\n",f[x],f[y],f[x-y]); ans = ans * f[x] * pow1(f[y] * f[x - y] % mod, mod - 2) % mod; n /= mod; m /= mod; } return ans % mod; }ll quick_multi(ll a, ll b) { ll ans = 0; while (b) { if (b & 1) ans = (ans + a)%mod; a = (a + a)%mod; b >>=1; } return ans; }int main() {ll K, X, A, Y, B; init(); while (cin >> K) { cin >> A >> X >> B >> Y; ll ans = 0; for (int i = 0; i <= X; i++) for (int j = 0; j <= Y; j++) { if ((i * A + j * B) == K) { ans += quick_multi(lucas(X, i) % mod , lucas(Y, j) % mod) % mod; } } cout << ans%mod << endl; } return 0; }


    推荐阅读