LightOJ

链接:

https://vjudge.net/problem/LightOJ-1226

题意:

OUM is a one unit machine which processes jobs. Since it can't handle heavyweight jobs; jobs needs to be partitioned into units. Initially, all the job information and unit partitions are given as input. Then the machine allocates necessary time slots. And in each time slot it asks the user for the name of the job to be processed. After getting the name; the machine determines the next unprocessed unit of that job and processes that unit in that slot. If there is no such unit, the machine crashes. A job is said to be complete if all the units of that job are complete.

For example, let J1 and J2 be two jobs each having 2 units. So, OUM will create 4 time slots. Now the user can give J1 J2 J2 J1 as input. That means it completes the 1st unit of J1 in time slot 1 and then completes the 1st unit of J2 in time slot 2. After that it completes the 2nd unit of J2 and 2nd unit of J1 in time slots 3 and 4 respectively. But if the user gives J1 J1 J2 J1 as input, the machine crashes in time slot 4 since it tries to process 3rd unit of J1 which is not available.

Now, Sam is the owner of a software firm named ACM and he has n jobs to complete using OUM. He wants to complete Jobi before Jobi+1 where 1 ≤ i < n. Now he wants to know the total number of ways he can complete these jobs without crashing the OUM. He assigned you for this task. Two ways are different if at tth slot one processed a unit of Jobi and another processed a unit of Jobj where i ≠ j. For the example above, there are three ways:

J1 J1 J2 J2

J1 J2 J1 J2

J2 J1 J1 J2

思路:

考虑对前面放好了i个,下一个只要把一个放在最后,其他的放在前面任意组合即可。
得到第i个能放的方法(C_{a[i]-1+sum}^{sum})sum为已经放了的个数

代码:

// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;

LL Fac[MAXN];
int a[MAXN], n;

LL PowMod(LL a, LL b, LL p)
{
    LL res = 1;
    while(b)
    {
        if (b&1)
            res = res*a%p;
        a = a*a%p;
        b >>= 1;
    }
    return res;
}

LL ExGcd(LL a, LL b, LL &x, LL &y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    LL d = ExGcd(b, a%b, x, y);
    LL tmp = x;
    x = y;
    y = tmp-(a/b)*y;
    return d;
}

LL GetInv(LL a, LL p)
{
    LL x, y;
    LL d = ExGcd(a, p, x, y);
    if (d == 1)
        return (x%p+p)%p;
    else
        return -1;
    // return PowMod(a, p-2, p);
}

LL C(LL a, LL b)
{
    if (a < b)
        return 0;
    if (a == b)
        return 1;
    return (Fac[a]*GetInv(Fac[a-b]*Fac[b]%MOD, MOD))%MOD;
}

LL Lucas(LL a, LL b)
{
    if (b == 0)
        return 1;
    return C(a%MOD, b%MOD)*Lucas(a/MOD, b/MOD)%MOD;
}

void Init()
{
    Fac[0] = 1;
    Fac[1] = 1;
    Fac[2] = 2;
    for (int i = 3;i < MAXN;i++)
        Fac[i] = Fac[i-1]*i%MOD;
}

int main()
{
    // freopen("test.in", "r", stdin);
    Init();
    int t, cas = 0;
    scanf("%d", &t);
    while(t--)
    {
        printf("Case %d:", ++cas);
        scanf("%d", &n);
        for (int i = 1;i <= n;i++)
            scanf("%d", &a[i]);
        LL ans = 1, sum = 0;
        for (int i = 1;i <= n;i++)
        {
            ans = ans*C(a[i]-1+sum, sum)%MOD;
            sum += a[i];
        }
        printf(" %lld
", ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/12019296.html