当前位置:网站首页>Factorial implementation of large integer classes
Factorial implementation of large integer classes
2022-07-07 10:16:00 【Last night was peaceful and Chang'an_】
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 10000;
struct BigInt
{
BigInt(int x = 0)
{
memset(arr, 0, sizeof(arr));// Initialize all to 0
for (len = 1; x; len++)
{
arr[len] = x % 10;// Yes x Reverse order preservation
x /= 10;
}
len--;
}
int& operator[](int i)
{
return arr[i];// use x[i] Instead of x.arr[i]
}
void flatten(int L)// Handle bits that are not single digits into single digits , So it is called flattening ,L Not less than the effective length
{
len = L;
for (int i = 1; i <= len; ++i)
{
arr[i + 1] += arr[i] / 10;
arr[i] %= 10;
}
while (!arr[len])// Eliminate pre 0
{
len--;
}
}
void print()// Reverse output
{
for (int i = max(1, len); i >= 1; --i)
{
cout << arr[i];
}
cout << endl;
}
int len;
int arr[maxn];
};
BigInt operator+(BigInt &a,BigInt &b)
{
BigInt c;
int len = max(a.len, b.len);
for (int i = 1; i <= len; ++i)
{
c[i] += a[i] + b[i];
}
c.flatten(len + 1);// The maximum length shall not exceed len+1
return c;
}
BigInt operator*(BigInt& a, BigInt& b)
{
BigInt c;
int len = a.len + b.len;
for (int i = 1; i <= a.len; ++i)
{
for (int j = 1; j <= b.len; ++j)
{
c[i + j - 1] += a[i] * b[j];//a[i] * b[j] The result of is produced in i+j-1 On a
}
}
c.flatten(len );// The maximum length shall not exceed len+1
return c;
}
BigInt operator*(BigInt& a, int& b)
{
BigInt c;
int len = a.len;
for (int i = 1; i <= len; ++i)
{
c[i] += a[i] *b;
}
c.flatten(len + 11);//int Type longest 10 position , So you can flatten it like this
return c;
}
int main()
{
int n;
cin >> n;
BigInt fac(1);
BigInt ans(0);
for (int i = 1; i <= n; ++i)
{
fac = fac * i;
ans = ans + fac;
}
ans.print();
return 0;
}
边栏推荐
- Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
- request对象对请求体,请求头参数的解析
- Inno setup packaging and signing Guide
- 对存储过程进行加密和解密(SQL 2008/SQL 2012)
- 【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
- STM32 ADC和DMA
- Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
- ES6中的函数进阶学习
- Introduction to energy Router: Architecture and functions for energy Internet
- 反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
猜你喜欢
A wave of open source notebooks is coming
The story of Plato and his three disciples: how to find happiness? How to find the ideal partner?
arcgis操作:dwg数据转为shp数据
STM32中AHB总线_APB2总线_APB1总线这些是什么
虚数j的物理意义
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
Word自动生成目录的方法
高数_第1章空间解析几何与向量代数_向量的数量积
Memory ==c language 1
随机推荐
Apprentissage avancé des fonctions en es6
Parameter sniffing (1/2)
Analyze Android event distribution mechanism according to popular interview questions (I)
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Web3.0 series distributed storage IPFs
运用tensorflow中的keras搭建卷积神经网络
2022.7.4DAY596
Delete a record in the table in pl/sql by mistake, and the recovery method
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
Or in SQL, what scenarios will lead to full table scanning
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Pit encountered by vs2015 under win7 (successful)
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
ES6中的函数进阶学习
【ORM框架】
【acwing】789. 数的范围(二分基础)
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
STM32 Basics - memory mapping
EasyExcel读取写入简单使用
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat