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的更多相关文章

  1. LightOJ - 1226 - One Unit Machine(排列组合)

    链接: https://vjudge.net/problem/LightOJ-1226 题意: OUM is a one unit machine which processes jobs. Sinc ...

  2. lightoj 1226 - One Unit Machine(dp+大组合数去摸)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1226 题解:由于这些任务完成是有先后的所以最后一个完成的肯定是最后一个任务的子 ...

  3. LightOJ 1226 - One Unit Machine Lucas/组合数取模

    题意:按要求完成n个任务,每个任务必须进行a[i]次才算完成,且按要求,第i个任务必须在大于i任务完成之前完成,问有多少种完成顺序的组合.(n<=1000 a[i] <= 1e6 mod ...

  4. 和组合数有关的dp

    1. UVaLive 7143 Room Assignment 用dp[i][r]表示,前i个盒子已经放完了,手上还拿着r对同色球. 状态转移方程为:dp[i+1][r-a-b] = dp[i][r] ...

  5. [CoreOS 转载] CoreOS实践指南(四):集群的指挥所Fleet

    转载:http://www.csdn.net/article/2015-01-14/2823554/2 摘要:CoreOS是采用了高度精简的系统内核及外围定制的操作系统.ThoughtWorks的软件 ...

  6. fleet-运行一个全局的单元

    运行一个全局的单元 正如前面所提到的,全局单元是有用的,用于在您的集群中的所有机器上运行一个单元.它不会比一个普通的单元差太多,而是一个新的x-fleet参数称为Global=true.这是一个示例单 ...

  7. Fleet-运行一个高可用的服务

    运行一个高可用的服务 使用CoreOS最大的好处就是你可以以高可用的方式来运行你的服务.接下来我们将部署两个一样的Apache web server容器.然后,我们将通过让一台机器出现故障,fleet ...

  8. Fleet(集群管理器)

    工作原理 fleet 是通过systemd来控制你的集群的,控制的任务被称之为unit(单元),控制的命令是fleetctl unit运行方式 unit的运行方式有两种: standard globa ...

  9. [Java Basics3] XML, Unit testing

    What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...

随机推荐

  1. python基础实战

    字符串的互相转换 字典的排序 字典的排序可以直接把,key值或者,values拿出来排序 也可以用dict.items拿出所有的key,value的值再加key=lambda x:x[1] 来排序. ...

  2. Hadoop入门 完全分布式运行模式-集群配置

    目录 集群配置 集群部署规划 配置文件说明 配置集群 群起集群 1 配置workers 2 启动集群 总结 3 集群基本测试 上传文件到集群 查看数据真实存储路径 下载 执行wordcount程序 配 ...

  3. 零基础学习java------day8------javabean编写规范,继承,static关键字,代码块,单例设计模式

    0. 今日内容提要 1. javabean书写规范 javabean:一个普通的类,用来描述事物的类,里面不包含任何的业务逻辑,只是用来存储数据. 比如:Teacher,Student,Mobile. ...

  4. CPU 是如何认识和执行代码的

    CPU的介绍 CPU 也称为微处理器,是计算机的心脏和/或大脑. 深入研究计算机的核心,可以帮助我们有效地编写计算机程序. CPU 是计算机的心脏和大脑,它执行提供给他们的指令.它的主要工作是执行算术 ...

  5. windows Visual Studio 上安装 CUDA【转载】

    原文 : http://blog.csdn.net/augusdi/article/details/12527497  前提安装: Visual Studio 2012 Visual Assist X ...

  6. 分配器——allocators

    任何容器的构建都离不开分配器,分配器顾名思义就是分割配置内存资源的组件,分配器的效率直接影响力容器的效率. operator new()和malloc() C/C++底层都是通过malloc()调用系 ...

  7. 【Android】我有放入Icon到mipmap,但不显示,只显示安卓机器人Icon(Android 8.0 图标适配)

    首先,放上别人写的博客,而我自己的博客,只会写大概思路,给自己留给备忘 https://blog.csdn.net/guolin_blog/article/details/79417483 其实会发生 ...

  8. Linux学习 - 使用qq邮箱发送邮件

    1 打开qq邮箱,设置->账户->POP3/SMTP,开启服务 2 配置/etc/mail.rc文件 set from=73***32@qq.com #设置发送方邮件地址 set smtp ...

  9. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  10. d3 CSS

    CSS的inline.block与inline-block 块级元素(block):独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默认为浏览器的宽度,即就是100%宽. 行内元素(inline ...