_TeaClaudz_ @ 2023-08-15 22:11:42
#include<bits/stdc++.h>
using namespace std;
int a[100], s[100], la, ls;
void add() {
int x = 0;
for(int i = 0; i < la; ++i) {
s[i] += a[i] + x;
x = s[i] / 10;
s[i] %= 10;
}
if(la > ls) ls = la;
if(x) {
s[la++] += x;
}
while(s[ls - 1] == 0 && ls > 1) ls--;
}
void mul(int n) {
int x = 0;
for(int i = 0; i < la; ++i) {
a[i] = a[i] * n + x;
x = a[i] / 10;
a[i] %= 10;
}
if(x) a[la++] = x;
while(a[la - 1] == 0 && la > 1) la--;
return;
}
int main() {
int n, n2;
ls = 1;
s[0] = 0;
cin >> n;
n2 = n;
while(n2) {
a[la++] = n2 % 10;
n2 /= 10;
}
for(int i = 1; i <= n; ++i) {
memset(a, 0, sizeof(a));
la = 1;
a[0] = 1;
for(int j = i; j >= 1; --j) {
mul(j);
}
add();
}
for(int i = ls - 1; i >= 0; --i) {
cout << s[i];
}
return 0;
}
by forest1145141919810 @ 2023-08-22 20:41:01
@luoyuwei 虽然我不知道你错哪里了,但是我可以提供一个更好的板子
by forest1145141919810 @ 2023-08-22 20:42:21
#include<bits/stdc++.h>
using namespace std;
struct Num{
#define LEN 105
int num[LEN],cnt,f;
inline int getnum(int x[],int cnt,int f){
int num=0;
for(int i=cnt;i>=1;i--)
num=num*10+x[i];
return num;
}
inline void copy(int x1[], int cnt1, int f1, int x2[], int& cnt2, int& f2) {//把高精度数x1完全复制给高精度数ans
for (int i = 1; i <= cnt1; i++) x2[i] = x1[i];
cnt2 = cnt1; f2 = f1;
}template<typename T>
inline void copynum(int x[],int &cnt,int &f,T num){
if(num==0) {x[1]=0,cnt=1,f=1;return;}
if(num<0) f=-1,num=-num;else f=1;cnt=0;
while(num>0) x[++cnt]=num%10,num/=10;
while(x[cnt]==0&&cnt>0) cnt--;
if(cnt==0) x[++cnt]=0;
}
inline bool ismax(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2) {//判断数x1是不是更大
if (f1 > f2) return true;
else if (cnt1 > cnt2) return true;
else if (f1 < f2) return false;
else if (cnt1 < cnt2) return false;
for (int i = cnt1; i >= 1; i--){
if (x1[i] < x2[i]) return false;
else if(x1[i] > x2[i]) return true;
}
return true;
}
inline bool ismin(int x1[], int cnt1, int f1, int x2[], int cnt2, int f2) {//判断数x1是不是更小
if (f1 > f2) return false;
else if (cnt1 > cnt2) return false;
else if (f1 < f2) return true;
else if (cnt1 < cnt2) return true;
for (int i = cnt1; i >= 1; i--){
if (x1[i] > x2[i]) return false;
else if(x1[i] < x2[i]) return true;
}
return true;
}
inline void add(int x1[],int cnt1,int x2[],int cnt2,int ans[],int &cnt){
int anscnt=cnt;int anst[LEN];int x=0;
for(int i=1;i<=cnt;i++) anst[i]=ans[i];
int len=max(cnt1,cnt2);anscnt=0;
if(len==cnt1) for(int i=cnt2+1;i<=len;i++) x2[i]=0;
else for(int i=cnt1+1;i<=len;i++) x1[i]=0;
for(int i=1;i<=len;i++){
anst[++anscnt]=(x1[i]+x2[i]+x)%10;
x=(x1[i]+x2[i]+x)/10;
}
if(x>0) anst[++anscnt]=x;
while(!anst[anscnt]&&anscnt>0) anscnt--;
if(anscnt==0) anscnt=1;cnt=anscnt;
for(int i=1;i<=anscnt;i++) ans[i]=anst[i];
}
inline void minus(int x1[],int cnt1,int x2[],int cnt2,int anst[],int &cnt,int &ansf){
int cntans=cnt;int ans[LEN];int f=ansf;
for(int i=1;i<=cnt;i++) ans[i]=anst[i];
if (ismin(x1, cnt1, 1, x2, cnt2, 1)) {
copy(x2, cnt2, 1, ans, cntans, f); f = -f;
for (int i = cnt1; i >= 1; i--) {
if (ans[i] < x1[i]) {
int cnt = i + 1; ans[i] = ans[i] + 10 - x1[i];
while (ans[cnt] == 0 && cnt <= cntans) ans[cnt] = 9, cnt++;
ans[cnt]--; while (ans[cntans] == 0) cntans--;
}
else ans[i] -= x1[i];
}while (cntans > 0 && ans[cntans] == 0) cntans--; if (cntans == 0) cntans++;
}
else {
copy(x1, cnt1, 1, ans, cntans, f);
for (int i = cnt2; i >= 1; i--) {
if (ans[i] < x2[i]) {
int cnt = i + 1; ans[i] = ans[i] + 10 - x2[i];
while (ans[cnt] == 0 && cnt <= cntans) ans[cnt] = 9, cnt++;
ans[cnt]--; while (ans[cntans] == 0) cntans--;
}
else ans[i] -= x2[i];
}while (ans[cntans] == 0) cntans--; if (cntans == 0) cntans++;
}
cnt=cntans;ansf=f;if(cnt==1&&ans[1]==0) ansf=1;
for(int i=1;i<=cntans;i++) anst[i]=ans[i];
}
inline void multiply(int x1[],int cnt1,int f1,int x2[],int cnt2,int f2,int ans[],int &cnt,int &f){
f=f1*f2;cnt=cnt1+cnt2+1;int anst[LEN],ansf=f,cntans=cnt;
for(int i=1;i<=cntans;i++) anst[i]=0;
for(int i=1;i<=cnt1;i++){
int temp=0;
for(int j=1;j<=cnt2;j++){
anst[i+j-1]=(x1[i]*x2[j]+temp+anst[i+j-1]);
temp=anst[i+j-1]/10;
anst[i+j-1]%=10;
}
anst[i+cnt2]=temp;
}
while(anst[cntans]==0&&cntans>0) cntans--;
if(cntans==0) cntans=1,ansf=1;
for(int i=1;i<=cntans;i++) ans[i]=anst[i];
cnt=cntans;f=ansf;return;
}
inline void operator += (Num b){
if (f < 0 && b.f>0) minus(b.num, b.cnt, num, cnt, num, cnt, f);
else if (f > 0 && b.f < 0) minus(num, cnt, b.num, b.cnt, num, cnt, f);
else {add(num, cnt, b.num, b.cnt, num, cnt); }
}
inline void operator -= (Num b){
if (b.f < 0 && f>0) { f = 1; add(num, cnt, b.num, b.cnt, num, cnt); }
else if (f < 0 && b.f>0) { f = -1; add(num, cnt, b.num, b.cnt, num, cnt); }
else if (f > 0 && b.f > 0) minus(num, cnt, b.num, b.cnt, num, cnt, f);
else if (f < 0 && b.f < 0) minus(b.num, b.cnt, num, cnt, num, cnt, f);
}
inline void operator *= (Num b){multiply(num,cnt,f,b.num,b.cnt,b.f,num,cnt,f);}
inline Num operator + (Num b){
Num c;if (f < 0 && b.f>0) minus(b.num, b.cnt, num, cnt, c.num, c.cnt, c.f);
else if (f > 0 && b.f < 0) minus(num, cnt, b.num, b.cnt, c.num, c.cnt, c.f);
else {add(num, cnt, b.num, b.cnt, c.num, c.cnt); }return c;
}
inline Num operator - (Num b){
Num c;if (b.f < 0 && f>0) { f = 1; add(num, cnt, b.num, b.cnt, c.num, c.cnt); }
else if (f < 0 && b.f>0) { f = -1; add(num, cnt, b.num, b.cnt, c.num, c.cnt); }
else if (f > 0 && b.f > 0) minus(num, cnt, b.num, b.cnt, c.num, c.cnt, c.f);
else if (f < 0 && b.f < 0) minus(b.num, b.cnt, num, cnt, c.num, c.cnt, c.f);
return c;
}
inline Num operator * (Num b){
Num c;
multiply(num,cnt,f,b.num,b.cnt,b.f,c.num,c.cnt,c.f);
return c;
}
inline void operator = (Num b){copy(b.num,b.cnt,b.f,num,cnt,f);}
template<typename T>inline void operator = (T b){copynum(num,cnt,f,b);}
inline void operator ++ (){
Num c;c=1;
if (f < 0 && c.f>0) minus(c.num, c.cnt, num, cnt, num, cnt, f);
else if (f > 0 && c.f < 0) minus(num, cnt, c.num, c.cnt, num, cnt, f);
else {add(num, cnt, c.num, c.cnt, num, cnt); }
}
inline void operator -- (){
Num c;c=-1;
if (f < 0 && c.f>0) minus(c.num, c.cnt, num, cnt, num, cnt, f);
else if (f > 0 && c.f < 0) minus(num, cnt, c.num, c.cnt, num, cnt, f);
else {add(num, cnt, c.num, c.cnt, num, cnt); }
}
inline bool operator >= (Num b){return ismax(num,cnt,f,b.num,b.cnt,b.f);}
template<typename T>inline bool operator >= (T b){
Num c;c=b;return ismax(num,cnt,f,c.num,c.cnt,c.f);
}
inline bool operator <= (Num b){return ismin(num,cnt,f,b.num,b.cnt,b.f);}
template<typename T>inline bool operator <= (T b){
Num c;c=b;return ismin(num,cnt,f,c.num,c.cnt,c.f);
}
inline bool operator == (Num b){
return ismax(num,cnt,f,b.num,b.cnt,b.f)&&ismin(num,cnt,f,b.num,b.cnt,b.f);
}
template<typename T>inline bool operator == (T b){
Num c;c=b;
return ismin(num,cnt,f,c.num,c.cnt,c.f)&&ismax(num,cnt,f,c.num,c.cnt,c.f);
}
inline bool operator > (Num b){return !ismin(num,cnt,f,b.num,b.cnt,b.f);}
inline bool operator < (Num b){return !ismax(num,cnt,f,b.num,b.cnt,b.f);}
template<typename T>inline bool operator > (T b){
Num c;c=b;return !ismin(num,cnt,f,c.num,c.cnt,c.f);
}
template<typename T>inline bool operator < (T b){
Num c;c=b;return !ismax(num,cnt,f,c.num,c.cnt,c.f);
}
inline bool operator ! (){
Num c;copy(num,cnt,f,c.num,c.cnt,c.f);
if(c==0) return true;
return false;
}
};
inline int GetNum(Num a){return a.getnum(a.num,a.cnt,a.f);}
inline void init(Num &a){a.cnt=1;a.f=1;}
inline void read(Num &a){
a.cnt=0;a.f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') a.f=-a.f;ch=getchar();}
while(ch>='0'&&ch<='9'){a.num[++a.cnt]=ch-'0';ch=getchar();}
for(int i=1;i<=a.cnt/2;i++) swap(a.num[i],a.num[a.cnt-i+1]);
}
inline void write(Num x){
if(x.f<0) putchar('-');
for(int i=x.cnt;i>=1;i--)
putchar(x.num[i]+'0');
putchar('\n');return;
}
#define int Num
#define long long Num
#define __int128 Num
#define ui Num
#define ull Num
int s,n;
signed main(){
read(n);s=0;int i;
for(i=1;i<=n;++i){
int jie,j;jie=1;
for(j=1;j<=i;++j) jie*=j;
s+=jie;
}write(s);
return 0;
}
最上面结构体里的是封装好了的高精,在主函数里用int算就可以了(我把int也重定义了),很简单的
by whoishe @ 2023-08-22 20:52:58
你可以参考我的代码
#include<bits/stdc++.h>
using namespace std;
int s[100],sum[100];
void cheng(int a[],int b){
int a1[100] = {};
for(int i=0;i<100;i++){
a1[i]+=a[i]*b;
a1[i+1]+=a1[i]/10%10;
a1[i+2]+=a1[i]/100;
a1[i]=a1[i]%10;
}
for(int i=0;i<100;i++){
a[i]=a1[i];
}
return;
}
int jia(int s[],int s1[]){
int s2[100]={};
for(int i=0;i<99;i++){
s2[i]+=s[i]+s1[i];
s2[i+1]+=s2[i]/10;
s2[i]=s2[i]%10;
}
for(int i=0;i<100;i++){
s[i]=s2[i];
}
}
int main(){
int n;
s[0]=1;
cin>>n;
for(int j=n;j>0;j--){
for(int k=0;k<100;k++){
s[k]=0;
}
s[0]=1;
for(int i=1;i<=j;i++){
cheng(s,i);
}
jia(sum,s);
}
int len=100;
while(sum[len-1]==0&&len>0){
len--;
}
for(int i=len-1;i>=0;i--){
cout<<sum[i];
}
return 0;
}
by _TeaClaudz_ @ 2023-08-23 21:11:01
谢谢,找到原因了,忘了 mul() 里面的 x 最后可以超过 10,应该用 while 而不是用 if
by _TeaClaudz_ @ 2023-08-23 21:11:24
此贴结