Cod sursa(job #1601725)

Utilizator akaprosAna Kapros akapros Data 16 februarie 2016 10:46:26
Problema Sandokan Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <bits/stdc++.h>
#define mod 2000003LL
#define maxN 5002
#define ll long long
using namespace std;
ll n, k, x, f[maxN];
ll invm(ll x)
{
    ll p = mod - 2, ans = 1, aux = x;
    for (int i = 1; i <= p; i <<= 1)
    {
        if(i & p)
            ans = (1LL * ans * aux) % mod;
        aux = (1LL * aux * aux) % mod;
    }
    return ans;
}
ll C(ll n, ll k)
{
    if (k == 0)
        return 1LL;
    if (k == 1)
        return n;
    if (k == n)
        return 1LL;
    ll sol = f[n];
    sol = (1LL * sol * invm(f[k])) % mod;
    sol = (1LL * sol * invm(f[n - k])) % mod;
    return (1LL * sol) % mod;
}
void read()
{
    freopen("sandokan.in", "r", stdin);
    scanf("%lld %lld", &n, &k);
    f[0] = 1LL;
    for (int i = 1; i <= n; ++ i)
        f[i] = (1LL * f[i] * i) % mod;
}
void solve()
{
    x = n % (k - 1) - 1;
    if (x < 0)
        x = 0;
}
void write()
{
    freopen("sandokan.out", "w", stdout);
    printf("%lld", C(n - 1, x));
}
int main()
{
    read();
    solve();
    write();
    return 0;
}