Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <vector>
- #include <stdio.h>
- #include <cstdlib>
- #include <sstream>
- using namespace std;
- class BigNum {
- public:
- BigNum() {
- buf_.push_back(0);
- }
- explicit BigNum(const string& s) {
- size_t i = s.size();
- while (i > 9) {
- buf_.push_back(stoul(s.substr(i - 9, 9)));
- i -= 9;
- }
- if (i > 0)
- buf_.push_back(stoul(s.substr(0, i)));
- if (!buf_.size())
- buf_.push_back(0);
- }
- BigNum(const BigNum& other) {
- buf_ = other.buf_;
- }
- BigNum& operator=(const BigNum& other) {
- buf_ = other.buf_;
- return *this;
- }
- friend BigNum operator*(const BigNum& a, const BigNum& b) {
- BigNum c("0");
- c.buf_.resize(0);
- c.buf_.resize(a.buf_.size() + b.buf_.size());
- for (size_t i = 0; i < a.buf_.size(); ++i)
- for (size_t j = 0, carry = 0;
- j < b.buf_.size() || carry;
- ++j) {
- carry += c.buf_[i + j];
- carry += a.buf_[i] * ((j < b.buf_.size()) ? b.buf_[j] : 0);
- c.buf_[i + j] = uint32_t(carry % a.BASE);
- carry /= a.BASE;
- }
- while (c.buf_.size() > 1 && c.buf_.back() == 0)
- c.buf_.pop_back();
- return c;
- }
- friend ostream& operator<<(ostream& os, const BigNum& a) {
- if (!a.buf_.size()) return os;
- os << a.buf_.back();
- for (auto it = ++a.buf_.rbegin();
- it != a.buf_.rend();
- ++it) {
- os << setfill('0') << setw(9) << *it;
- }
- return os;
- }
- private:
- const uint32_t BASE = 1000000000;
- vector<uint32_t> buf_;
- };
- BigNum longFactorial(int N) {
- BigNum result("1");
- for (int i = 2; i <= N; i++) {
- result = result * BigNum(to_string(i));
- }
- return result;
- }
- int main() { //факториал 100 (100!) на длинной арифметике
- BigNum fact = longFactorial(10);
- cout << fact << endl;
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement