[NOIP analog test 7] visit solution to a problem (mathematical combination + CRT + Lucas theorem)

Barley

T because of restrictions, it is not difficult to get out of a $ O (T ^ 3) $ violent dp

But I did not try is said to have 30 minutes?

If positive solutions is clearly a combination of mathematical friends

First of all $ n, m $ may be negative, but it did not affect,

We can treat it all as mess that is positive to go to the right

You can think of are truly effective step to the right direction or the other two are to go up in counterproductive

U is a number set up walking, the d, l left, and right R & lt

They satisfy the relationship:

$r-l=m,u-d=n,T=u+d+l+r$

Since the effective number of steps for the $ m + n $, it must be an even number $ $ Tmn

Because the remaining steps to ensure that the upper and lower average, about average

Enumeration $ udlr $ one can get the final answer:

$ans=\sum \limits_{i=n,2|(i-n)}^{t-m} \binom{t}{i} \binom{i}{\frac{i-n}{2}} \binom{t-i}{\frac{t-i-m}{2}}$

(Copy from there over the Emperor)

Logically speaking this question should end

But the topic of frenzied people are still sick about you: modulus may not be a prime number

But it must be given a few different from each other in the number of product quality, save us from the clutches of exLucas

After the Chinese remainder theorem on the line

The decomposition of the prime factors of the modulus $ p [i] $, for each side of factors are considered answer, referred to as $ ans [i] $

Then get the final answer of course, is equivalent to solving

$\begin{cases} x\equiv ans_1(mod\ p_1)\\ x\equiv ans_2(mod\ p_2)\\ x\equiv ans_3(mod\ p_3)\\ ...\\ x\equiv ans_n(mod\ p_n)\\ \end{cases}$

This is not the CRT bare it? Board hit it again to get away.

Expansion of the EU do not have to fight, you can use fast power front combined with Fermat's little understanding CRT set in congruence equation.

//#define R
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#define re register
using namespace std;
const int N=100005;
typedef long long ll;
int T,mod,n,m;
ll fac[N],ans[1005];
vector<int> fact;
int abss(int x)
{
    return x<0?-x:x;
}
void divi(int x)
{
    int sq=sqrt(x)+1;
    for(int i=2;i<=sq;i++)
    {
        if(x%i==0)
        {
            fact.push_back(i);
            while(x%i==0)x/=i;
        }
        if(x==1)break;
    }
    if(x!=1)fact.push_back(x);
}
ll qpow(ll a,ll b,ll p)
{
    ll res=1;a=a%p;
    while(b)
    {
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res;
}
ll C(ll x,ll y,ll p)
{
    if(x<y)return 0;
    return fac[x]*qpow(fac[y],p-2,p)%p*qpow(fac[x-y],p-2,p)%p;
}
ll lucas(ll x,ll y,ll p)
{
    if(!y)return 1;
    return C(x%p,y%p,p)*lucas(x/p,y/p,p)%p;
}
void getfac(ll p)
{
    fac[0]=1;
    for(int i=1;i<=T;i++)
        fac[i]=1LL*i*fac[i-1]%p;
}
int main()
{
    scanf("%d%d%d%d",&T,&mod,&n,&m);
    n=abss(n),m=abss(m);
    divi(mod);
    int sz=fact.size();
    for(re int now=0;now<sz;now++)
    {
        int p=fact[now];getfac(p);
        for(re int i=n;i<=T-m;i++)
        {
            if((i-n)&1||(T-i-m)&1)continue;
            ll res=1;
            res=res*lucas(T,i,p)%p*lucas(i,(i-n)/2,p)%p*lucas(T-i,(T-i-m)/2,p)%p;
            ans[now]=(ans[now]+res)%p;
        }
    }
    ll anss=0;
    for(re int i=0;i<sz;i++)
    {
        ll times=mod/fact[i];
        ll ress=qpow(times,fact[i]-2,fact[i]);
        ress=(ress%fact[i]+fact[i])%fact[i];
        anss=(anss+times*ress*ans[i])%mod;
    }
    cout<<(anss+mod)%mod<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Rorschach-XR/p/11230688.html