Light oj 1226 - One Unit Machine(递推)

来源:互联网 发布:java微服务消息组件 编辑:程序博客网 时间:2024/05/23 00:03

1226 - One Unit Machine
PDF (English)StatisticsForum
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-12typedef long long ll;using namespace std;#define mod 1000000007#define INF 1000000000#define N 1000005ll 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;}




0 0
原创粉丝点击