zrq495
www.zrq495.com

2009=287*7=41*49=41*7*7。所以当n>=41时,n!%2009=0。

n<41时,用公式:(a*b)%c = ((a%c) * (b%c)) % c。

代码如下:

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     long long n, i ,s;
 8     while(cin >> n)
 9     {
10         if (n == 0)
11         {
12             cout << "1" << endl;
13             continue;
14         }
15         if (n >= 41)
16         {
17             cout << "0" << endl;
18             continue;
19         }
20         s=n%2009;
21         for (i=n-1; i>=1; i--)
22         {
23             s=((s)*(i%2009))%2009;
24         }
25         cout << s << endl;
26     }
27     return 0;
28 }
posted on 2012-08-04 21:33  zrq495  阅读(176)  评论(0编辑  收藏  举报