[HDU] [2674] N! Again

Problem Description

Loading...

Problem Analysis

An application of the congruence theorem. Just get the congruence theorem straight and you’re fine. The key one is:

(a+b)%c=(a%c+b%c)%c

Then the code is easy to write:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<stdio.h>
int main() {
	int N,s,p;
	int i;
	while(scanf("%d",&N)!=EOF) {
		if(N>=49)
			printf("0\n");
		else {
			for(i=s=1; i<=N; i++) {
				s*=i;
				s%=2009;
			}
			printf("%d\n",s);
		}
	}
	return 0;
}

Problem link: 【杭电】[2674]N!Again

Loading...