大数加法(模拟竖式运算)
hdu1002
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <iomanip> #include <stack> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const int MAXN = 1005; const int MOD = 1e9 + 7; #define MemI(x) memset(x, -1, sizeof(x)) #define Mem0(x) memset(x, 0, sizeof(x)) #define MemM(x) memset(x, 0x3f, sizeof(x)) #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 void Add(char a[MAXN], char b[MAXN], int alen, int blen) { int len = max(alen, blen); int aa[MAXN] = {0}, bb[MAXN] = {0}; for(int i = 0;i < alen;++i) aa[i] = a[alen - i - 1] - '0'; for(int i = 0;i < blen;++i) bb[i] = b[blen - i - 1] - '0'; int ans[MAXN]; Mem0(ans); for(int i = 0;i < len;++i) ans[i] = aa[i] + bb[i]; for(int i = 0;i < len;++i) { if(ans[i] > 9) { ans[i] -= 10; ans[i + 1]++; } } if(ans[len]) len++; bool flag = false; for(int i = len - 1;i >= 0;--i) { if(ans[i] != 0) flag = true; if(flag) cout << ans[i]; } if(!flag) cout << 0; cout << endl; } int main() { int T; while(cin >> T) { for(int cas = 1;cas <= T;++cas) { char a[MAXN], b[MAXN]; cin >> a >> b; if(cas != 1) cout << endl; cout << "Case " << cas << ": " << a << " + " << b << " = "; Add(a, b, strlen(a), strlen(b)); } } return 0; }
大数乘法
51nod 1027
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <iomanip> #include <stack> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const int MAXN = 1005; const int MOD = 1e9 + 7; #define MemI(x) memset(x, -1, sizeof(x)) #define Mem0(x) memset(x, 0, sizeof(x)) #define MemM(x) memset(x, 0x3f, sizeof(x)) #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 char a[MAXN], b[MAXN]; int ans[MAXN * MAXN] = {0}; int main() { cin >> a >> b; int alen = strlen(a), blen = strlen(b); for(int i = 0;i < alen >> 1;++i) swap(a[i], a[alen - i - 1]); for(int i = 0;i < blen >> 1;++i) swap(b[i], b[blen - i - 1]); for(int i = 0;i < alen;++i) { for(int j = 0;j < blen;++j) { ans[i + j] += (a[i] - '0') * (b[j] - '0'); ans[i + j + 1] += ans[i + j] / 10; ans[i + j] %= 10; } } bool flag = false; for(int i = alen + blen - 1;i >= 0;--i) { if(!flag && ans[i] != 0) flag = true; if(flag) cout << ans[i]; } if(!flag) cout << 0; cout << endl; return 0; }
hdu1042
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #include <queue> #include <iomanip> #include <stack> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const int MAXN = 100005; const int MOD = 1e9 + 7; #define MemI(x) memset(x, -1, sizeof(x)) #define Mem0(x) memset(x, 0, sizeof(x)) #define MemM(x) memset(x, 0x3f, sizeof(x)) #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 int main() { int n, a[MAXN]; while(cin >> n) { Mem0(a); int len = 0, t = 0; a[0] = 1; //万进制 for(int i = 1;i <= n;++i) { for(int j = 0;j <= len;++j) { a[j] = a[j] * i + t; t = a[j] / 10000; a[j] %= 10000; } if(t) { a[++len] = t; t = 0; } } printf("%d", a[len]); for(int i = len - 1;i >= 0;--i) printf("%04d", a[i]); printf(" "); } return 0; }
N!(分段乘)
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <queue> #include <iomanip> #include <stack> #include <algorithm> using namespace std; typedef long long LL; #define Mem0(x) memset(x, 0, sizeof(x)) #define MemI(x) memset(x, -1, sizeof(x)) #define MemM(x) memset(x, 0x3f, sizeof(x)) const int MAXN = 10005; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int ans[MAXN]; int main() { int n; cin >> n; int t = 0, num = 0; ans[0] = 1; for(int i = 1;i <= n;++i) { t = 0; for(int j = 0;j <= num;++j) { ans[j] = ans[j] * i + t; t = ans[j] / 10000; ans[j] %= 10000; } if(t) ans[++num] += t; } printf("%d", ans[num]); for(int i = num - 1;i >= 0;--i) printf("%04d", ans[i]); return 0; }
N!的长度(斯特林公式)
这里取10的对数后一定是小数,所以最后要加1
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <queue> #include <iomanip> #include <stack> #include <algorithm> using namespace std; typedef long long LL; #define Mem0(x) memset(x, 0, sizeof(x)) #define MemI(x) memset(x, -1, sizeof(x)) #define MemM(x) memset(x, 0x3f, sizeof(x)) const double PI = 3.1415926; const double E = 2.1718281828; const int MAXN = 10005; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int main() { int n; cin >> n; cout << (int)((0.5 * log(2 * PI * n) + n * log(n) - n) / log(10)) + 1 << endl; return 0; }