JZOJ5622. 【NOI2018模拟4.2】table

题目

这里写图片描述

题目大意

就是给出一个非常类似杨辉三角形的数表的某一行,
然后求出对应位置的值。

题解

最暴力的做法就是将整个数表都算出来。
考虑p=1的部分,
可以发现一个位置斜向下走就是乘b,向下走就是乘a,
求一个位置的值,就可以转化成第一行每一个数乘上一个系数的和,
这个系数就是a和b的几次方再乘一个组合数(方案数)。

同理对于p=n的部分,
向右走就是乘-b/a,向下走就是除a。

这样,p在中间的情况也就可以解决了。

组合数,次方,逆元都要线性求。

code

#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <time.h>
#define ll long long
#define N 100003
#define M 103
#define db double
#define P putchar
#define G getchar
#define mo 998244353
#define pi 3.1415926535897932384626433832795
using namespace std;
char ch;
void read(ll &n)
{
    n=0;
    ch=G();
    while((ch<'0' || ch>'9') && ch!='-')ch=G();
    ll w=1;
    if(ch=='-')w=-1,ch=G();
    while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
    n*=w;
}

int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
ll abs(ll x){return x<0?-x:x;}
ll sqr(ll x){return x*x;}
void write(ll x){if(x>9) write(x/10);P(x%10+'0');}

ll w[N],a,b,n,m,p,q,x,y,nya,nyb,ans,ny[N];

ll ksm(ll x,int y)
{
    ll s=1;
    for(;y;y>>=1,x=x*x%mo)
        if(y&1)s=s*x%mo;
    return s;
}

void work1()
{
    ll C=1,A=ksm(a,x-p),B=1;
    for(int i=0;i<=x-p && i<=y;i++)
        ans=(ans+w[y-i]*A%mo*B%mo*C%mo)%mo,
        C=C*(x-p-i)%mo*ksm(i+1,mo-2)%mo,
        A=A*nya%mo,B=B*b%mo;
}

void work2()
{
    ll C=1,A=ksm(nya,p-x),B=1;
    for(int i=y;i;i--)
        ans=(ans+w[i]*B%mo*A%mo*C%mo*((y-i)&1?-1:1)+mo)%mo,
        C=C*ksm(y-i+1,mo-2)%mo*(p-x+y-i)%mo,
        A=A*nya%mo,B=B*b%mo;
}

int main()
{
    freopen("table.in","r",stdin);
    freopen("table.out","w",stdout);

    read(m);read(n);read(a);read(b);read(p);read(q);
    nya=ksm(a,mo-2);nyb=ksm(b,mo-2);

    for(int i=1;i<=n;i++)
        read(w[i]);

    for(int i=1;i<=q;i++)
    {
        read(x);read(y);ans=0;
        if(x<p)work2();else
        if(x>p)work1();else ans=w[y];
        write(ans),P('\n');
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/lijf2001/article/details/79796622