Light OJ 1226 One Unit Machine(DP)
来源:互联网 发布:history linux 编辑:程序博客网 时间:2024/05/22 17:56
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
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<set>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int INF=0x3f3f3f3f;typedef long long LL;const int mod=1e9+7;int t,n;int A[1100];LL dp[1100];LL inv[1000000+10];LL Pow(LL a,LL b){ LL ans=1; while(b) { if(b&1) ans=(ans*a)%mod; b>>=1; a=(a*a)%mod; } return ans;}LL C(LL a,LL b){ LL ans=1; for(int i=1;i<=b;i++) ans=(ans*(a-i+1)%mod*inv[i])%mod; return ans;}int main(){ int cas=1; for(int i=1;i<=1000000;i++) inv[i]=Pow(i,mod-2); scanf("%d",&t); while(t--) { scanf("%d",&n); REPF(i,1,n) scanf("%d",&A[i]); dp[1]=1; LL 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",cas++,dp[n]); } return 0;}
- Light OJ 1226 One Unit Machine(DP)
- Light OJ 1226 One Unit Machine (大组合数计算+DP)
- Light oj 1226 - One Unit Machine(递推)
- 1226 - One Unit Machine DP+组合数
- Light OJ 1298 One Theorem, One Year (DP)
- light oj 1032 数位DP
- light OJ 1205 数位DP
- light OJ 1068 数位DP
- light oj 1140 数位dp
- light oj 1422,区间dp
- Light OJ 1231(背包dp)
- light oj 1068(数位dp)
- light oj 1422(区间dp)
- light oj 1025 区间dp
- light oj 1032(数位dp)
- Light OJ 1038(概率DP)
- Light OJ 1031 区间dp
- light oj 1004 水DP
- iOS开发-自动处理键盘事件的第三方库 IQKeyboardManager
- 第二周项目3—体验复杂度
- canvas.save() canvas.restore() 作用
- 学习笔记--进程的三种基本状态及其转换
- FileZilla FTP服务软件
- Light OJ 1226 One Unit Machine(DP)
- openvpn 多用户访问
- Java获取Webapp下的css、js、image资源
- 技术类博客网站推荐
- MySQL删除表中数据(笔记)
- LeetCode House Robber II
- OS静态库
- Jquery数据复制
- android 为TextView的部分文字设置超链接样式并监听点击事件