Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Tuesday, December 2, 2014

RIVALS-Rivals

Rivals

Given below code is for RIVAL spoj.

Hint :- simple math and inverse modulo problem




#include <bits/stdc++.h>
using namespace std ;
#define LL long long
#define MOD 1000000007
LL arr[2000009];
void pre()
{
    arr[0] = 1;
    arr[1] = 1;
    for(LL i = 2 ; i<=2000000 ; i++)
        arr[i] = (arr[i-1] * i)%MOD;
}
LL inv_mod(LL x)
{
    LL a = 1 , p = x , n = 1000000005;
    while(n) 
    {
        if(n&1)
            a = (a*p)%MOD;
        p = (p * p )%MOD;
        n>>=1;
    }
    return a;
}
int main()
{
    int t;
    pre() ;
    scanf("%d",&t);
    while(t--)
    {
        int x , y;
        scanf("%d%d",&x , &y);
        LL temp = (inv_mod(arr[x]) * inv_mod(arr[y]))%MOD;
        LL res = (arr[x+y] * temp)%MOD;
        printf("%lld\n",res);
    }
    return 0;
}

No comments:

Post a Comment

Your comment is valuable to us