Java [2016年NOIP提高组] 组合数问题

这里写图片描述
输入
第一行有两个整数t, k ,其中t代表该测试点总共有多少组测试数据,k的意义见【问题描述】。
输出
输出t行,每行一个整数代表所有的 0 ≤ i ≤ n, 0 ≤j ≤ min (i, m) 中有多少对 (i, j) 满足是 k 的倍数。
样例输入
1 2
3 3

样例输出
1

import java.math.BigInteger;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
public class Main {
    public static void main(String[] args) {
        int a[][]=new int[2010][2010];
        int b[][]=new int [2010][2010];
        int n,m,ans,t,k;
        Scanner scanner=new Scanner(System.in);
        t=scanner.nextInt();
        k=scanner.nextInt();
        a[1][1]=1%k;
         if (a[1][1]==0) 
             b[1][1]++;
         for (int j=2;j<=2001;j++)
         {
             for (int q=1;q<=Math.min(j,2001);q++)
             {
                  if (q==1) 
                      a[j][q]=(a[j-1][q]+1)%k;
                  else 
                       a[j][q]=(a[j-1][q]+a[j-1][q-1])%k;
                  if (a[j][q]==0) 
                       b[j][q]=b[j][q-1]+1; 
                  else 
                       b[j][q]=b[j][q-1];
             }
         }
         for (int i=1;i<=t;i++)
         {
             n=scanner.nextInt();
             m=scanner.nextInt();
             ans=0;
             for (int j=1;j<=n;j++)
             ans+=b[j][Math.min(m,j)];
             System.out.println(ans);

        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41611106/article/details/80294401