LG P4351 [CERC2015]Frightful Formula

Description

A frightful matrix is a square matrix of order n where the first row and the first column are explicitly specified, while the other elements are calculated using a frightful formula which is, actually, a simple recursive rule.

Given two integer sequences l and t,both of size n,as well as integer parameters a,b and c,the frightful matrix F is defined as follows:

* The first column of the matrix is the sequence l:

$$F[k, 1] = lk$$

* The first row of the matrix is the sequence t:

$$F[1, k] = tk$$

* Other elements are calculated using a recursive formula:

$$F[i,j]=a*F[i,j-1]+b*F[i-1,j]+c$$

Given a frightful matrix, ?nd the value of the element $F[n,n]$ modulo $10^6 +3$.

Solution

如果$c=0$可以直接计算每一个格子对答案的贡献

$l_i$对答案的贡献为$a^{n-1}b^{n-i} \binom{2n-i-2}{n-2}$,其他的同理

考虑$c$

使用待定系数法,设$F_{i,j}+k=a(F_{i,j-1}+k)+b(F_{i-1,j}+k)$

解得$k=\frac 1{a+b-1}$,当$a+b \neq 1$

在上式中计算贡献,最后把$k$减掉即可

数据水,没有$a+b=1$的情况

#include<iostream>
#include<cstdio>
using namespace std;
long long n,a,b,c,l[200005],t[200005],fac[400005]={1},inv[400005],k,ans,A[200005]={1},B[200005]={1};
const long long mod=1e6+3;
inline int read()
{
    int f=1,w=0;
    char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=getchar();
    return f*w;
}
long long ksm(long long a,long long p)
{
    long long ret=1;
    while(p)
    {
        if(p&1) (ret*=a)%=mod;
        (a*=a)%=mod,p>>=1;
    }
    return ret;
}
long long C(int x,int y)
{
    return fac[x]*inv[y]%mod*inv[x-y]%mod;
}
int main()
{
    n=read(),a=read(),b=read(),c=read(),k=c*ksm(a+b-1,mod-2)%mod;
    for(int i=1;i<=400000;i++) fac[i]=fac[i-1]*i%mod;
    for(int i=1;i<=200000;i++)A[i]=A[i-1]*a%mod,B[i]=B[i-1]*b%mod;
    inv[400000]=ksm(fac[400000],mod-2);
    for(int i=399999;~i;i--) inv[i]=inv[i+1]*(i+1)%mod;
    for(int i=1;i<=n;i++) l[i]=read();
    for(int i=1;i<=n;i++) t[i]=read();
    for(int i=2;i<=n;i++) (ans+=((l[i]+k)*A[n-1]%mod*B[n-i]%mod*C(n+n-i-2,n-2)%mod+(t[i]+k)*A[n-i]%mod*B[n-1]%mod*C(n+n-i-2,n-2)%mod)%mod)%=mod;
    printf("%lld\n",(ans-k+mod)%mod);
    return 0;
}
[CERC2015]Frightful Formula

 

posted @ 2020-12-07 09:02  QDK_Storm  阅读(116)  评论(0编辑  收藏  举报