简单
2025-10-30 18:43:59
发布于:上海
2阅读
0回复
0点赞
AI给出的*答案
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stdexcept>
#include<cmath>
#include<sstream>
class BI{
private:
std::vector<int>dg;
bool ng=false;
void rlz(){
while(dg.size()>1&&dg.back()==0){
dg.pop_back();
}
if(dg.empty()){
dg.push_back(0);
}
}
int ca(const BI&o)const{
if(dg.size()!=o.dg.size()){
return dg.size()>o.dg.size()?1:-1;
}
for(int i=dg.size()-1;i>=0;--i){
if(dg[i]!=o.dg[i]){
return dg[i]>o.dg[i]?1:-1;
}
}
return 0;
}
void sl(int p){
if(dg.size()==1&&dg[0]==0)return;
std::vector<int>nd(p,0);
nd.insert(nd.end(),dg.begin(),dg.end());
dg=nd;
}
public:
BI():dg({0}){}
BI(long long n){
if(n<0){
ng=true;
n=-n;
}
if(n==0){
dg.push_back(0);
}else{
while(n>0){
dg.push_back(n%10);
n/=10;
}
}
}
BI(const std::string&s){
if(s.empty()){
dg.push_back(0);
return;
}
int st=0;
if(s[0]=='-'){
ng=true;
st=1;
}else if(s[0]=='+'){
st=1;
}
while(st<s.size()&&s[st]=='0'){
st++;
}
if(st==s.size()){
dg.push_back(0);
ng=false;
return;
}
for(int i=s.size()-1;i>=st;--i){
if(s[i]<'0'||s[i]>'9'){
throw std::invalid_argument("inv");
}
dg.push_back(s[i]-'0');
}
rlz();
}
BI(const BI&o):dg(o.dg),ng(o.ng){}
BI&operator=(const BI&o){
if(this!=&o){
dg=o.dg;
ng=o.ng;
}
return*this;
}
bool operator==(const BI&o)const{
return dg==o.dg&&ng==o.ng;
}
bool operator!=(const BI&o)const{
return!(*this==o);
}
bool operator<(const BI&o)const{
if(ng!=o.ng){
return ng;
}
if(ng){
return ca(o)>0;
}
return ca(o)<0;
}
bool operator>(const BI&o)const{
return o<*this;
}
bool operator<=(const BI&o)const{
return!(o<*this);
}
bool operator>=(const BI&o)const{
return!(*this<o);
}
BI operator+(const BI&o)const{
if(ng!=o.ng){
if(ng){
BI t=o;
t.ng=false;
return t-(*this).abs();
}else{
BI t=o;
t.ng=false;
return(*this)-t.abs();
}
}
BI r;
r.dg.clear();
r.ng=ng;
int c=0;
int ms=std::max(dg.size(),o.dg.size());
for(int i=0;i<ms||c;++i){
int s=c;
if(i<dg.size())s+=dg[i];
if(i<o.dg.size())s+=o.dg[i];
r.dg.push_back(s%10);
c=s/10;
}
r.rlz();
return r;
}
BI operator-(const BI&o)const{
if(ng!=o.ng){
BI t=o;
t.ng=!o.ng;
return(*this)+t;
}
if(ng){
BI t1=(*this).abs();
BI t2=o.abs();
return t2-t1;
}
int cmp=ca(o);
if(cmp==0){
return BI(0);
}
BI r;
r.dg.clear();
if(cmp<0){
r=o-(*this);
r.ng=true;
return r;
}
int b=0;
for(int i=0;i<dg.size();++i){
int df=dg[i]-b;
if(i<o.dg.size()){
df-=o.dg[i];
}
if(df<0){
df+=10;
b=1;
}else{
b=0;
}
r.dg.push_back(df);
}
r.rlz();
return r;
}
BI operator*(const BI&o)const{
if(*this==BI(0)||o==BI(0)){
return BI(0);
}
BI r;
r.dg.resize(dg.size()+o.dg.size(),0);
for(int i=0;i<dg.size();++i){
int c=0;
for(int j=0;j<o.dg.size()||c;++j){
long long p=r.dg[i+j]+
(long long)dg[i]*(j<o.dg.size()?o.dg[j]:0)+
c;
r.dg[i+j]=p%10;
c=p/10;
}
}
r.rlz();
r.ng=(ng!=o.ng);
if(r.dg.size()==1&&r.dg[0]==0){
r.ng=false;
}
return r;
}
BI operator/(const BI&o)const{
if(o==BI(0)){
throw std::invalid_argument("div0");
}
BI dv=this->abs();
BI ds=o.abs();
if(dv<ds){
return BI(0);
}
BI r;
r.dg.clear();
BI cur;
for(int i=dv.dg.size()-1;i>=0;--i){
cur.dg.insert(cur.dg.begin(),dv.dg[i]);
cur.rlz();
int qd=0;
while(cur>=ds){
cur=cur-ds;
qd++;
}
r.dg.insert(r.dg.begin(),qd);
}
r.rlz();
r.ng=(ng!=o.ng);
if(r.dg.size()==1&&r.dg[0]==0){
r.ng=false;
}
return r;
}
BI operator%(const BI&o)const{
if(o==BI(0)){
throw std::invalid_argument("mod0");
}
BI q=*this/o;
return*this-q*o;
}
BI&operator+=(const BI&o){
*this=*this+o;
return*this;
}
BI&operator-=(const BI&o){
*this=*this-o;
return*this;
}
BI&operator*=(const BI&o){
*this=*this*o;
return*this;
}
BI&operator/=(const BI&o){
*this=*this/o;
return*this;
}
BI&operator%=(const BI&o){
*this=*this%o;
return*this;
}
BI operator++(){
*this=*this+BI(1);
return*this;
}
BI operator++(int){
BI t=*this;
*this=*this+BI(1);
return t;
}
BI operator--(){
*this=*this-BI(1);
return*this;
}
BI operator--(int){
BI t=*this;
*this=*this-BI(1);
return t;
}
BI abs()const{
BI r=*this;
r.ng=false;
return r;
}
BI pwr(int e)const{
if(e<0){
throw std::invalid_argument("nege");
}
if(e==0){
return BI(1);
}
BI r(1);
BI b=*this;
while(e>0){
if(e%2==1){
r=r*b;
}
b=b*b;
e/=2;
}
return r;
}
BI sqrt()const{
if(ng){
throw std::invalid_argument("sqrtneg");
}
if(*this==BI(0)||*this==BI(1)){
return*this;
}
BI l(1);
BI r=*this;
BI res(1);
while(l<=r){
BI m=(l+r)/BI(2);
BI sq=m*m;
if(sq==*this){
return m;
}else if(sq<*this){
l=m+BI(1);
res=m;
}else{
r=m-BI(1);
}
}
return res;
}
bool ev()const{
return dg[0]%2==0;
}
bool od()const{
return!ev();
}
bool pr()const{
if(*this<BI(2)){
return false;
}
if(*this==BI(2)){
return true;
}
if(ev()){
return false;
}
BI i(3);
BI lim=this->sqrt();
while(i<=lim){
if(*this%i==BI(0)){
return false;
}
i=i+BI(2);
}
return true;
}
std::string ts()const{
std::string r;
if(ng&&!(dg.size()==1&&dg[0]==0)){
r+='-';
}
for(int i=dg.size()-1;i>=0;--i){
r+=char(dg[i]+'0');
}
return r;
}
long long toll()const{
long long r=0;
long long m=1;
for(int i=0;i<dg.size();++i){
r+=dg[i]*m;
m*=10;
}
return ng?-r:r;
}
int toi()const{
return static_cast<int>(toll());
}
double tod()const{
double r=0.0;
double m=1.0;
for(int i=0;i<dg.size();++i){
r+=dg[i]*m;
m*=10.0;
}
return ng?-r:r;
}
friend std::istream&operator>>(std::istream&is,BI&n);
friend std::ostream&operator<<(std::ostream&os,const BI&n);
};
std::istream&operator>>(std::istream&is,BI&n){
std::string s;
is>>s;
n=BI(s);
return is;
}
std::ostream&operator<<(std::ostream&os,const BI&n){
os<<n.ts();
return os;
}
class MU{
public:
static BI gcd(BI a,BI b){
a=a.abs();
b=b.abs();
while(b!=BI(0)){
BI t=b;
b=a%b;
a=t;
}
return a;
}
static BI lcm(const BI&a,const BI&b){
if(a==BI(0)||b==BI(0)){
return BI(0);
}
return(a*b)/gcd(a,b);
}
static BI fac(int n){
if(n<0){
throw std::invalid_argument("facneg");
}
BI r(1);
for(int i=2;i<=n;++i){
r=r*BI(i);
}
return r;
}
static BI fib(int n){
if(n<0){
throw std::invalid_argument("fibneg");
}
if(n==0)return BI(0);
if(n==1)return BI(1);
BI a(0),b(1),c(0);
for(int i=2;i<=n;++i){
c=a+b;
a=b;
b=c;
}
return b;
}
static BI bc(int n,int k){
if(k<0||k>n){
return BI(0);
}
if(k==0||k==n){
return BI(1);
}
BI r(1);
for(int i=1;i<=k;++i){
r=r*BI(n-i+1);
r=r/BI(i);
}
return r;
}
static bool psq(const BI&n){
if(n<BI(0))return false;
BI r=n.sqrt();
return r*r==n;
}
static BI pw(const BI&b,int e){
return b.pwr(e);
}
};
class PU{
public:
static bool pr(const BI&n){
return n.pr();
}
static BI np(const BI&n){
BI c=n+BI(1);
while(!c.pr()){
c=c+BI(1);
}
return c;
}
static BI pp(const BI&n){
if(n<=BI(2)){
throw std::invalid_argument("nopr");
}
BI c=n-BI(1);
while(!c.pr()){
c=c-BI(1);
}
return c;
}
static std::vector<BI>pf(BI n){
std::vector<BI>f;
if(n<BI(2)){
return f;
}
while(n.ev()){
f.push_back(BI(2));
n=n/BI(2);
}
BI i(3);
while(i*i<=n){
while(n%i==BI(0)){
f.push_back(i);
n=n/i;
}
i=i+BI(2);
}
if(n>BI(1)){
f.push_back(n);
}
return f;
}
};
class SU{
public:
static BI pbi(const std::string&s){
return BI(s);
}
static std::string ts(const BI&n){
return n.ts();
}
static bool ins(const std::string&s){
if(s.empty())return false;
int st=0;
if(s[0]=='-'||s[0]=='+'){
st=1;
}
if(st==s.size())return false;
for(int i=st;i<s.size();++i){
if(s[i]<'0'||s[i]>'9'){
return false;
}
}
return true;
}
};
class TR{
private:
int tp=0;
int tf=0;
public:
void rt(const std::string&tn,bool r){
if(r){
std::cout<<"✓ "<<tn<<" - PASS"<<std::endl;
tp++;
}else{
std::cout<<"✗ "<<tn<<" - FAIL"<<std::endl;
tf++;
}
}
void ps()const{
std::cout<<"\n=== Test Summary ==="<<std::endl;
std::cout<<"Total: "<<(tp+tf)<<std::endl;
std::cout<<"Passed: "<<tp<<std::endl;
std::cout<<"Failed: "<<tf<<std::endl;
std::cout<<"Rate: "<<(tp*100.0/(tp+tf))<<"%"<<std::endl;
}
};
void rct(){
TR r;
std::cout<<"Running Comprehensive Tests..."<<std::endl;
r.rt("Add1",BI(123)+BI(456)==BI(579));
r.rt("Add2",BI("999999999")+BI(1)==BI("1000000000"));
r.rt("Sub1",BI(1000)-BI(1)==BI(999));
r.rt("Sub2",BI(5)-BI(10)==BI(-5));
r.rt("Mul1",BI(123)*BI(456)==BI(56088));
r.rt("Div1",BI(100)/BI(25)==BI(4));
r.rt("Mod1",BI(17)%BI(5)==BI(2));
r.rt("Eq1",BI(123)==BI(123));
r.rt("Ne1",BI(123)!=BI(456));
r.rt("Lt1",BI(5)<BI(10));
r.rt("Gt1",BI(10)>BI(5));
r.rt("GCD1",MU::gcd(BI(54),BI(24))==BI(6));
r.rt("LCM1",MU::lcm(BI(12),BI(18))==BI(36));
r.rt("Fac1",MU::fac(5)==BI(120));
r.rt("Fib1",MU::fib(10)==BI(55));
r.rt("BC1",MU::bc(5,2)==BI(10));
r.rt("Pr1",PU::pr(BI(17)));
r.rt("Pr2",!PU::pr(BI(15)));
r.rt("Np1",PU::np(BI(10))==BI(11));
r.ps();
}
void daf(){
std::cout<<"\n=== Advanced Features ==="<<std::endl;
BI b1("12345678901234567890");
BI b2("98765432109876543210");
std::cout<<"Large add: "<<b1<<" + "<<b2<<" = "<<(b1+b2)<<std::endl;
std::cout<<"Large mul: "<<b1<<" * "<<b2<<" = "<<(b1*b2)<<std::endl;
std::cout<<"GCD 56,98: "<<MU::gcd(BI(56),BI(98))<<std::endl;
std::cout<<"LCM 12,18: "<<MU::lcm(BI(12),BI(18))<<std::endl;
std::cout<<"Fib10: "<<MU::fib(10)<<std::endl;
std::cout<<"BC(8,3): "<<MU::bc(8,3)<<std::endl;
BI tp(100);
std::cout<<"Next prime after 100: "<<PU::np(tp)<<std::endl;
auto f=PU::pf(BI(360));
std::cout<<"PF of 360: ";
for(const auto&x:f){
std::cout<<x<<" ";
}
std::cout<<std::endl;
}
int main(){
std::cout<<"=== BI Library ==="<<std::endl;
rct();
daf();
std::cout<<"\n=== A+B Problem ==="<<std::endl;
BI a,b;
std::cout<<"Enter a b: ";
std::cin>>a>>b;
BI s=a+b;
std::cout<<"Result: "<<a<<" + "<<b<<" = "<<s<<std::endl;
return 0;
}
不会通过的
这里空空如也







有帮助,赞一个