[HDU] [1799] How Many Loops?

Problem Description

Loading...

Problem Analysis

At first I didn’t derive it correctly
The editorial said it was a binomial coefficient

Struggled for a while and couldn’t see how to derive it
Then through Pascal’s triangle and this manually calculated relation
I found it satisfies f(n,m)=f(n-1,m)+f(n-1,m-1)
So it is just a binomial coefficient

After that, modulo for big numbers can also be written easily

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
int C[2020][2020];
int main() {
	C[0][0]=1;
	C[0][1]=0;
	for(int i=1; i<2010; i++) {
		for(int j=0; j<=i; j++) {
			if(j==0)
				C[i][j]=1;
			else
				C[i][j]=(C[i-1][j]+C[i-1][j-1])%1007;
		}
	}
	int T;
	scanf("%d",&T);
	while(T--) {
		int m,n;
		scanf("%d %d",&m,&n);
		printf("%d\n",C[n][m]);
	}
	return 0;
}

Problem link:【HDU】[1799] How Many Times Does It Loop?

Loading...