Fishmaster @ 2021-08-15 20:29:14
四个WA,六个TLE,真就“万紫千红”呗! 真搞不懂我代码错哪了,求助大佬
#include<bits/stdc++.h>
using namespace std;
string num[10]={"0","1","2","3","4","5","6","7","8","9"};
string add(string a,string b){
int x[100000]={0},y[100000]={0},sum[200000]={0};
string s="";
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
for(int i=0;i<a.length();i++){
x[i]=a[i]-'0';
}
for(int i=0;i<b.length();i++){
y[i]=b[i]-'0';
}
int len=max(a.length(),b.length());
int flag=0;
for(int i=0;i<len;i++){
sum[i]=x[i]+y[i]+flag;
if(sum[i]>=10){
sum[i]-=10;
flag=1;
}else{
flag=0;
}
}
if(flag==1){
s+="1";
}
for(int i=len-1;i>=0;i--){
s+=num[sum[i]];
}
return s;
}
string sub(string aa){
string aaa=aa;
if(aa[aa.length()-1]>='1'){
aaa[aa.length()-1]--;
}else{
aaa[aa.length()-1]+=9;
aaa[aa.length()-2]--;
}
return aaa;
}
int main(){
int p;
cin>>p;
string n="1";
for(int i=1;i<=p;i++){
n=add(n,n);
}
cout<<sub(n).length()<<endl;
for(int i=500;i>sub(n).length();i--){
cout<<"0";
}
cout<<sub(n);
return 0;
}
by SadLava @ 2021-08-15 20:41:59
WA可能是因为0的数量不对,我也没数过,TLE的话要换成快速幂
by SadLava @ 2021-08-15 21:09:33
对哈还得换行
by lfy666_lifeiyang @ 2022-09-03 20:12:59
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
ll p,gs;
vec mult_short(vec &a,ll b){
vec res;
ll x=0;
for(ll i=0;i<a.size();i++){
x+=a[i]*b;
res.push_back(x%10);
x/=10;
}
while(x){
res.push_back(x%10);
x/=10;
}
//删除前导零
// while(res.back()==0&&res.size()>0)
// res.pop_back();
res.resize(500);
return res;
}
vec mult(vec &a,vec &b){
vec res;
ll x=0;
for(ll i=0;i<500;i++){
for(ll j=0;j<=i;j++)
x+=a[j]*b[i-j];
res.push_back(x%10);
x/=10;
}
res.resize(500);
return res;
}
vec q_pow(ll a,ll b){
if(!b){
vec tmp(500);
//只保留500位
tmp[0]=1;
return tmp;
//因为用vector做不能直接返回1,所以要麻烦一点
}
vec tmp=q_pow(a,b>>1);
tmp=mult(tmp,tmp);
if(b&1)
tmp=mult_short(tmp,a);
return tmp;
}
int main(){
cin>>p;
cout<<(ll)(log10(2)*p+1)<<endl;
//自带函数,输出第一个答案
vec sum=q_pow(2,p);
sum[0]--;
for(ll i=499;i>=0;i--){
cout<<sum[i];
gs++;
//50位换一行
if(gs==50){
gs=0;
cout<<endl;
}
}
return 0;
}```