SPOJ - UCV2013E 数学,逆元

Greedy Walking

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:

Every step is exactly one unit long.
He only moves in one dimension at a time.
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 different 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 different 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维空间,你要从起点走向终点且只能向正方向走,问,有多少种走法可以走到终点。

解题思路:因为只能向正方向走,所以总共走的步数必定为各个轴上起点和终点的差的总和,那么设要走的步数为sum,对于一个轴而言,如果他们起点和终点在这个轴上的差为a,那么,我们只要在这sum步中选择任意a步走向这个轴的正方向即可,所以他的走法为C(sum,a),那么根据这个思路,我们再在剩下的步数中按照这个方法依次递推即可。例如第二个样例答案即为C(10,4)*C(6,3)*C(3,2)*C(1,1)=10!/(4!*3!*2!*1!)=12600
因为所求的组合数过大,所以我们要用逆元的方法求答案。

#include <cstdio>
#define mod 1000000007
#define maxn 505
typedef long long ll;
ll fac[maxn],inv[maxn],n,a[maxn],b[maxn],sum;
ll quickpower(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans*=a;
            ans%=mod;
        }
        a=(a*a)%mod;
        b=b>>1;
    }
    return ans;
}
void init(){
    fac[0]=1;
    for(ll i=1;i<maxn;i++)  fac[i]=fac[i-1]*i%mod;
    inv[maxn-1]=quickpower(fac[maxn-1],mod-2);
    for(ll i=maxn-2;i>=0;i--)   inv[i]=inv[i + 1]*(i + 1)%mod;
}
int main(){
    ll n,ans;
    init();
    while(scanf("%lld",&n)&&n){
        sum=0;
        for(ll i=0;i<n;i++) scanf("%lld",&a[i]);
        for(ll i=0;i<n;i++){
            scanf("%lld",&b[i]);
            a[i]=b[i]-a[i];
            sum+=a[i];
        }
        ans=fac[sum];
        for(ll i=0;i<n;i++){
            ans=ans*inv[a[i]]%mod;
        }
        printf("%lld\n",ans);
    }

    return 0;
}

逆元的运用可以详见ACdreamers大佬的博客
http://blog.csdn.net/acdreamers/article/details/8220787

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值