题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123
1 10 861017
593846
此题n过于庞大,暴力是不可能的,所以必然会有一定的技巧在其中!
思路:当n大于m时 ,n的阶乘中必定包含因数m,所以取余后必定为0。
代码如下:
//#pragma warning (disable:4786) #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <climits> #include <ctype.h> #include <queue> #include <stack> #include <vector> #include <utility> #include <deque> #include <set> #include <map> #include <iostream> #include <algorithm> using namespace std; const double eps = 1e-9; //const double pi = atan(1.0)*4; const double pi = 3.1415926535897932384626; #define INF 1e18 //typedef long long LL; typedef __int64 LL; int main() { int t; LL m; char str[1017]; scanf("%d",&t); while(t--) { scanf("%s%I64d",str,&m); LL len = strlen(str); LL tmp = 1, tt = 0; for(int i = 0; i < len; i++) { tt = tt * 10 + str[i]-'0'; if(tt >= m) break; } if(tt == 0) { printf("%I64d\n",1%m); continue; } if(tt == 1) { printf("%I64d\n",2%m); continue; } LL ans = 2; for(int i = 2; i <= m && i <= tt; i++) { tmp *= i; tmp %= m; ans += tmp; ans %= m; } printf("%I64d\n",ans); } return 0; }
hdu 3123 GCC(数学题),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012860063/article/details/38615459