高精度:
重载运算符版:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int NR=1e4; char s[NR]; struct Bigint { int num[NR]; Bigint() { memset(num,0,sizeof(num)); num[0]=1; } void read() { scanf("%s",s); num[0]=strlen(s); for(int i=1;i<=num[0];i++) num[i]=s[num[0]-i]-'0'; } void print() { for(int i=num[0];i>=1;i--) printf("%d",num[i]); puts(""); } Bigint operator + (const Bigint &B) const { Bigint c; c.num[0]=max(num[0],B.num[0]); for(int i=1;i<=c.num[0];i++) { c.num[i]+=num[i]+B.num[i]; c.num[i+1]+=c.num[i]/10; c.num[i]%=10; } if(c.num[c.num[0]+1]>0) c.num[0]++; return c; } }; int main(void) { Bigint a,b,c; a.read(); b.read(); c=a+b; c.print(); return 0; }
普通高精度:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int NR=1e3; char s[NR+5]; int a[NR+5],b[NR+5],c[NR+5]; void read(int a[]) { scanf("%s",s); a[0]=strlen(s); for(int i=1;i<=a[0];i++) a[i]=s[a[0]-i]-'0'; } void print(int a[]) { for(int i=a[0];i>=1;i--) printf("%d",a[i]); puts(""); } void add(int a[],int b[],int c[]) { c[0]=max(a[0],b[0]); for(int i=1;i<=c[0];i++) { c[i]+=a[i]+b[i]; c[i+1]=c[i]/10; c[i]%=10; } if(c[c[0]+1]>0) printf("%d",c[c[0]]+1); } int main() { read(a); read(b); add(a,b,c); print(c); return 0; }
快速幂:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<algorithm> using namespace std; long long po(long long x,long long p,long long k) { if(p==0) return 1; if(p==1) return x; int a=po(x,p/2,k); a%=k; if(p%2==0) return (a*a)%k; return (a*a*x)%k; } int main(void) { long long x,p,k; scanf("%lld%lld%lld",&x,&p,&k); printf("%lld^%lld mod %lld=%lld\n",x,p,k,po(x,p,k)%k); return 0; }