洛谷2822(NOIP2016)[组合数问题]--杨辉三角

【链接】
洛谷2822

【解题报告】
杨辉三角无脑搞。

#include
using namespace std;
const int maxn=2005;
int n,k,a[maxn][maxn],f[maxn][maxn];
inline char nc()
{
    static char buf[100000],*l,*r;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF; return *l++;
}
inline int Read()
{
    int res=0; char ch=nc();
    while (ch<'0'||ch>'9') ch=nc();
    while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=nc();
    return res;
}
int main()
{
    freopen("2822.in","r",stdin);
    freopen("2822.out","w",stdout);
    n=Read(); k=Read();
    for (int i=0; i<=2000; i++) a[i][i]=a[i][0]=1;
    for (int i=1; i<=2000; i++)
     for (int j=1; j<=i; j++)
      a[i][j]=(a[i-1][j]+a[i-1][j-1])%k;
    for (int i=1; i<=2000; i++)
     for (int j=1; j<=2000; j++)
      {
        f[i][j]=f[i][j-1]+f[i-1][j]-f[i-1][j-1];
        if (!a[i][j]&&j<=i) f[i][j]++;
      }
    for (int i=1,x,y; i<=n; i++) x=Read(),y=Read(),printf("%d\n",f[x][y]);
    return 0;
}

你可能感兴趣的:(洛谷题解,NOIP题解,杨辉三角,部分常见刷题网站的题解,By,Greninja)