Description
在n维空间,每次只能把一维加1,起点(a[1],…,a[n]),终点(b[1],…,b[n]),问方案数
Input
多组用例,每组用例输入一整数n表示空间维数,之后输入两个n维向量分别表示起点和终点(1<=n<=50,1<=a[i]<=b[i]<=500,sum(b[i]-a[i])<=500)
Output
输出从起点走到终点的方案数,结果模1e9+7
Sample Input
2
2 1
5 5
4
0 0 0 0
1 2 3 4
5
1 2 3 4 5
8 5 6 4 8
5
0 0 0 0 0
100 100 100 100 100
0
Sample Output
35
12600
19219200
257055440
Solution
简单排列,记c[i]=b[i]-a[i],d[i]=sum(c[j]),1<=j<=i,那么答案即为C(d[n],c[n])C(d[n-1],c[n-1])…*C(d[1],c[1])
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 555
const ll mod=1e9+7ll;
ll C[maxn][maxn];
void init()
{
memset(C,0,sizeof(C));
C[0][0]=1;
for(int i=1;i<=500;i++)
{
C[i][0]=C[i][i]=1;
for(int j=1;j<i;j++)C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
}
}
int n,a[maxn],b[maxn];
int main()
{
init();
while(~scanf("%d",&n),n)
{
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&b[i]),a[i]=b[i]-a[i];
b[0]=0;
for(int i=1;i<=n;i++)b[i]=b[i-1]+a[i];
ll ans=1;
for(int i=1;i<=n;i++)ans=1ll*ans*C[b[i]][a[i]]%mod;
printf("%lld\n",ans);
}
return 0;
}