码迷,mamicode.com
首页 > 系统相关 > 详细

1226 - One Unit Machine

时间:2016-04-27 18:52:07      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

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

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
思路:dp+费马小定理;
题意:有N个任务,每个任务有K个小任务,要求每个任务要做完,就是他的K个小任务要做完。要求所有的任务要做完,并且第i+1个任务要在i个任务做完前做完,问共有多少种方案
dp[i]表示做完第i个任务的方案种数,状态转移方程:dp[i]=dp[i-1]*(Cnm);
考虑加入第i种时,因为第i种要完成,所以最后一个必须是第i种,那么其他的可以在前面的找ans[i-1]个空放,这样也不会改变前i-1个完成的顺序,那么当排第i个时,前面的空共有
ans[i]-1个,所以要在其中选出(ans[i]-ans[i-1]-1)个位置放,那么这就是后面的组合数,然后对1e9+7取模时用费马小定理;
 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<string.h>
 4 #include<iostream>
 5 using namespace std;
 6 typedef long long LL;
 7 const LL N=1e9+7;
 8 LL ans[1005];
 9 LL qq[1000005];
10 LL dp[10005];
11 LL quick(LL n,LL m);
12 int main(void)
13 {
14         int i, j ,k;
15         int p,q;
16         qq[0]=1;
17         for(i=1; i<=1000000; i++)
18         {
19                 qq[i]=(qq[i-1]*i)%N;
20         }
21         scanf("%d",&k);
22         int cas=0;
23         while(k--)
24         {
25                 cas++;
26                 scanf("%d",&p);
27                 memset(dp,0,sizeof(dp));
28                 for(i=1; i<=p; i++)
29                         scanf("%lld",&ans[i]);
30                 dp[1]=1;
31                 LL sum=1;
32                 for(i=2; i<=p; i++)
33                 {
34                         LL cc=ans[i]-1;
35                         LL ck=ans[i]+ans[i-1]-1;
36                         LL bk=(qq[ans[i-1]])*(qq[cc])%N;
37                         LL uu=quick(bk,N-2);
38                         sum=(qq[ck]*uu)%N;
39                         ans[i]+=ans[i-1];
40                         dp[i]=dp[i-1]*(sum)%N;
41                 }
42                 printf("Case %d: ",cas);
43                 printf("%lld\n",dp[p]);
44         }
45         return 0;
46 }
47 LL quick(LL n,LL m)
48 {
49         LL ak=1;
50         while(m)
51         {
52                 if(m&1)
53                 {
54                         ak=ak*n%(N);
55                 }
56                 n=(n*n)%(N);
57                 m/=2;
58         }
59         return ak;
60 }

 

1226 - One Unit Machine

标签:

原文地址:http://www.cnblogs.com/zzuli2sjy/p/5439908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!