解题报告:洛谷 P5239 回忆京都

版权声明:转载请附带原文链接,请勿随意删除原文内容,允许少量格式和/或内容修改,谢谢! https://blog.csdn.net/weixin_37661548/article/details/88091165

题目链接

算法

  • 递推(杨辉三角)
  • 二维前缀和

参考代码

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1055,P = 19260817;

int q,n,m;
int C[MAXN][MAXN],sum[MAXN][MAXN];

void init()
{
    scanf("%d",&q);
    for (int i = 0;i <= 1000;++i)
        for (int j = 0;j <= i;++j)
        {
            if (i == 0 || j == 0 || i == j) C[i][j] = 1;
            else C[i][j] = (C[i - 1][j] % P + C[i - 1][j - 1] % P) % P;
        }
    for (int i = 1;i <= 1000;++i)
        for (int j = 1;j <= 1000;++j)
            sum[i][j] = ((sum[i - 1][j] % P + 
            			  sum[i][j - 1] % P) % P + 
            			 (C[i][j] % P - 
            			  sum[i - 1][j - 1] % P + P) % P) % P;
}

void work()
{
    for (int i = 1;i <= q;++i)
    {
        qread(n);
        qread(m);
        qwrite(sum[m][n] % P);
        putchar('\n');
    }
}

int main()
{
    init();
    work();
    return 0;
}

分析

几乎就是板子题了 q w q qwq ,注意读入时 n n m m 是反的。

还有就是C[][]数组的初始化。

减法取模:(a - b) % P = (a % P - b %P + P) % P。

猜你喜欢

转载自blog.csdn.net/weixin_37661548/article/details/88091165