AtCoder Grand Contest 025 B - RGB Coloring

B - RGB Coloring

求ax + by = k (0<=x<=n && 0<=y<=n)的方案数,最后乘上C(n, x)*C(n,y)

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<int,pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 3e5 + 5;
const int MOD = 998244353;
int inv[N], fac[N];
LL q_pow(LL n, LL k) {
    LL ans = 1;
    while(k) {
        if(k&1) ans = (ans * n) % MOD;
        n = (n * n) % MOD;
        k >>= 1;
    }
    return ans;
}
void init() {
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = (1LL * fac[i-1] * i) % MOD;
    inv[N-1] = q_pow(fac[N-1], MOD - 2);
    for (int i = N-2; i >= 0; i--) inv[i] = (1LL * inv[i+1] * (i+1)) % MOD;
}
LL C(int n, int k) {
    return 1LL* fac[n] * inv[n-k] % MOD * inv[k] % MOD;
}
int main() {
    int n, a, b;
    LL k;
    init();
    scanf("%d%d%d%lld", &n, &a, &b, &k);
    LL ans = 0;
    for (int x = 0; x <= n; x++) {
        LL t = k - 1LL*x*a;
        if(t < 0) break;
        if(t%b) continue;
        LL y = t/b;
        if(y > n) continue;
        ans += (C(n, x)*C(n, y)) % MOD;
        ans %= MOD;
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/widsom/p/9145110.html
今日推荐