SDNU 1272.SL的秘密
Description
据说今天是男神SL的生日,但是他很低调,轻轻地送了一道大水题...
就是传说中的 " N !"
Input
每一行包含一个整数N(0 <= N<=10^9).
Output
对于每个数,输出 N! mod 2009
Sample Input
4 5
Sample Output
24 120
Source
思路:这道题有个玄学的地方,那就是2009 = 41*7*7。所以当累乘到了41!时,比它大的数取膜2009后,全都是0
#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <map> using namespace std; #define ll long long const int maxn = 5100; ll n, sum; ll qsm(ll a) { ll ans = 1; for(int i = 1; i <= a; i++) { ans = ((ans%2009)*(i%2009))%2009; } return ans; } int main() { while(~scanf("%lld", &n)) { if(n<41) { sum = qsm(n); printf("%lld\n", sum); } else printf("0\n"); } return 0; }