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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章