Light oj 1226 - One Unit Machine(递推)

本文介绍了一种名为OneUnitMachine的任务调度算法,该算法针对轻量级任务进行处理,通过将任务划分为单位并在每个时间槽中处理一个单位来避免机器过载。文章详细解释了如何在不崩溃的情况下完成所有任务的不同方式,并提供了一个具体的示例以及输入输出样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1226 - One Unit Machine
Time Limit: 2 second(s)Memory Limit: 32 MB

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 Jas 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

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with an integer n (1 ≤ n ≤ 1000). The next line contains n space separated positive integers k1, k2, k3 ... kn. Where, ki denotes the number of units for the ith job. You can assume that total number of units for all the jobs in any case is not greater than 106.

Output

For each case, print the case number and the result modulo 1000,000,007.

Sample Input

Output for Sample Input

2

2

2 2

3

2 2 3

Case 1: 3

Case 2: 45

 


PROBLEM SETTER: KAZI RAKIBUL HOSSAIN
SPECIAL THANKS: JANE ALAM JAN

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;
using namespace std;

#define mod 1000000007
#define INF 1000000000
#define N 1000005

ll dp[N];
ll inv[N],f[N];
int a[N];
int n;

ll fdd(ll s,int n)
{
   ll ans=1;
   while(n)
   {
       if(n&1) ans=ans*s%mod;
       s=s*s%mod;
       n>>=1;
   }
   return ans;
}

void inint()
{
    inv[0]=1;
    for(ll i=1;i<N;i++)
        inv[i]=inv[i-1]*fdd(i,mod-2)%mod;

    f[0]=1;
    for(int i=1;i<N;i++)
        f[i]=f[i-1]*i%mod;
}

ll C(int s,int n)
{
     ll ans=f[s]*inv[n]%mod*inv[s-n]%mod;
     return ans;
}

int main()
{
    inint();
    int i,j,t,ca=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);

        int sum=0;
        dp[1]=1;
         sum=a[1];
        for(int i=2;i<=n;i++)
        {
          sum+=a[i];
          dp[i]=dp[i-1]*C(sum-1,a[i]-1)%mod;
        }
        printf("Case %d: %lld\n",++ca,dp[n]);
    }
   return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值