SPOJ UCV2013E 数学

Greedy Walking

no tags 

Reginald is an N-dimensional traveler who wants to return to Filipistonia's Kingdom. He has an Obsessive-compulsive disorder in the way he travels so he can only do it following particular rules:

  1. Every step is exactly one unit long.
  2. He only moves in one dimension at a time.
  3. He only travels along the positive direction for each dimension.

For example, when traveling on a two-dimensional place. He can travel along either the X or the Y axis at any given time, but never on both at the same time. Moreover, since he only travels along the positive direction and every step is one unit long, his only possible moves are (+1, 0) and (0, +1).

Greedy Walking example

As you can see, he is a Greedy Walker: once he makes a decision he assumes it is the correct and he never goes back.


Given a starting position in an N-dimensional space (x1i, x2i, ... ,xni) your task is to count the number of diff erent travels he can make to position (x1f, x2f, ... , xnf) modulo 1000000007.

Input

The input contains several test cases, each one corresponding to a single travel. Each test case consists of a single line with one integer (1 <= N <= 50) followed by two lines each one with N integers, fi rst line will be initial position and second line will be target position.


You can assume that 0 <= xki <= x1f <= 500 for all k, 1 <= k <= N and Sum(xki-xkf) <= 500.

The end of input is indicated by a test case with N = 0.

Output

For each travel output a single line with one integer, the number of diff erent travels that exist from the initial position to the final position modulo 1000000007.

Example

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 Output: 35
12600
19219200
257055440

题意:n维空间,从一个点走到另一个点有多少种走法。


题解:有一个公式


求下逆元得到答案



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
ll fac[505],a[55],b[55];
ll quick(ll a,ll n){
	ll ans=1;
	while(n){
		if(n&1)ans=ans*a%mod;
		a=a*a%mod;
		n/=2;
	}
	return ans;
}
int main(){
	ll n;
	fac[0]=1;
	for(n=1;n<=500;n++)fac[n]=fac[n-1]*n%mod;
	while(scanf("%lld",&n)&&n){
		ll i,sum=0,mul=1;
		for(i=1;i<=n;i++)scanf("%lld",&a[i]);
		for(i=1;i<=n;i++){
			scanf("%lld",&b[i]);
			sum+=b[i]-a[i];
			mul=mul*fac[b[i]-a[i]]%mod;
		}
		sum=fac[sum];
		sum=sum*quick(mul,mod-2)%mod;
		printf("%lld\n",sum);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值