Dispatch

Ah ah ah ah ah ah ah ah ah ah ah ah I write this question autistic friends ah ah ah ah ah ah ah ah ah ah ah Ah ah ah ah ah

Do mentality of solving the problem:

Each thought of an idea, there is a need to do out of the feeling. But then I think you will find that a method has some very small problems, but I can not solve. . .

So it pulls ideas. . .

Finally, I ask this question of the topic and people i_m_a_ after finally did come out ~ ~ (data-oriented programming ~~

Hu mouth about ideas:

First of all, we play with a wave of the hand persimmon.

\[\sum_{x=0}^{n}\prod_{y=0}^{x}\frac{x}{kx+y-x}=\]
\[=\prod_{x=0}^n\prod_{y=0}^{k}(\frac{x}{kx+y-x}+1)\]
\[=\prod_{x=0}^n\prod_{y=0}^{k}\frac{kx+y}{kx+y-x}\]
\[=\prod_{x=0}^{n}\frac{\prod_{i=1}^x(kx+k-i)}{\prod_{j=1}^x(kx-j)}\]
\[=\frac{\prod_{i=1}^{n-1}(kn-i)}{\prod_{j=1}^{k-1}(kj-j)}\]
\[=\frac{(nk-1)!}{(nk-n-1)!n!(k-1)^n}\]

Then we look at how this thing seek.

Probably written in the numerator and denominator

\[\frac{p=a*m^x}{q=b*m^y}\]

(M is the modulus) form.

then. . .

If \ (X> Y \) , then after reduced \ (P \ equiv 0 \) , so \ (a = 0 \) to output \ (0 \)

If \ (X <Y \) , then after reduced \ (Q \ equiv 0 \) , so either (A \) \ what values can not satisfy the condition, output \ (- 1 \)

If \ (X = Y \) , then the \ (P \ equiv 0, Q \ equiv 0 \) , P! = 0, Q! = 0, this value can be determined by inverse

The amount of, the source is i_m_a_'s blog

(Also, he's written a blog where p = 0, q = 0, instead of congruence (fog

disappointed ..

Code

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;

const ll P = 1145141;
ll T;
ll n,k;
ll f[P+1];

inline void readx(ll &x)
{
    x=0;
    int s=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
            s=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<1)+(x<<3)+ch-'0';
        ch=getchar();
    }
    x*=s;
}

inline void pre()
{
    f[0]=1;
    for(int i=1;i<P;++i)
        f[i]=f[i-1]*i%P;
}

inline ll qpow(ll a,ll b)
{
    ll x=1;
    while(b)
    {
        if(b&1)
            x=x*a%P;
        a=a*a%P;
        b>>=1;
    }
    return x;
}

inline ll inv(ll x)
{
    return qpow(x,P-2);
}

int main()
{
    pre();
    readx(T);
    while(T--)
    {
        readx(n);readx(k);
        ll a_mo=1,a_de=1,x_mo=0,x_de=0;

        ll r=n*k-1;
        while(r)
        {
            a_mo=a_mo*f[r%P]%P;
            r/=P;
            x_mo+=r;
        }
        a_mo=a_mo*qpow(f[P-1],x_mo)%P;
//处理分子 

        ll tmp=0;
        r=k-1;
        while(r%P==0)
        {
            ++tmp;
            r/=P;
        }
        tmp*=(n-1);
        a_de=qpow(r,n-1);
        x_de+=tmp;

        r=n*k-n;
        tmp=0;
        while(r)
        {   
            a_de=a_de*f[r%P]%P;
            r/=P;
            tmp+=r;
        }
        a_de=a_de*qpow(f[P-1],tmp)%P;
        x_de+=tmp;

        r=n-1;
        tmp=0;
        while(r)
        {
            a_de=a_de*f[r%P]%P;
            r/=P;
            tmp+=r;
        }
        a_de=a_de*qpow(f[P-1],tmp)%P;
        x_de+=tmp;

//处理分母 

        ll ans=a_mo*inv(a_de)%P;
        if(x_mo>x_de)
            printf("0\n");
        else if(x_mo<x_de)
            printf("-1\n");
        else
            printf("%lld\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/oierwyh/p/11366646.html