大數超級馬拉松+直式開跟法
//====================================================================||
// ||
// ||
// Author : GCA ||
// 6AE7EE02212D47DAD26C32C0FE829006 ||
//====================================================================||
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
#ifdef ONLINE_JUDGE
#define ll "%lld"
#else
#define ll "%I64d"
#endif
typedef unsigned int uint;
typedef long long int Int;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 55
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
class bignum{
public:
const static int maxn=50;
Int n[maxn];
bignum(){
Set(n,0);
//debug("c\n");
}
void print(){
int i;
for(i=maxn-1;n[i]==0&&i>=1;i--);
printf("%lld",n[i]);
i--;
for(;i>=0;i--){
printf("%04lld",n[i]);
}
Pln();
}
bignum add(int a){
bignum ans;
Int c=0;
for(int i=0;i<maxn;i++){
Int tmp=n[i]+c+a;
// printf("tt %lld\n",tmp);
a=0;
ans.n[i]=tmp%10000;
c=tmp/10000;
//if(c==0)break;
}
return ans;
}
bignum mul(int a){
bignum ans;
for(int i=0;i<maxn;i++){
ans.n[i]=n[i]*a;
}
Int c=0;
for(int i=0;i<maxn;i++){
Int tmp=ans.n[i]+c;
ans.n[i]=tmp%10000;
c=tmp/10000;
}
return ans;
}
bignum operator+(const bignum& d) const{
bignum ans;
Int c=0;
for(int i=0;i<maxn;i++){
Int tmp=n[i]+d.n[i]+c;
ans.n[i]=tmp%10000;
c=tmp/10000;
}
return ans;
}
bignum operator*(const bignum& d)const{
bignum ans;
Int c=0;
for(int i=0;i<maxn;i++){
if(n[i]==0)continue;
for(int j=0;j+i<maxn;j++){
ans.n[i+j]+=n[i]*d.n[j];
c=ans.n[i+j]/10000;
ans.n[i+j]%=10000;
for(int k=i+j+1;k<maxn;k++){
Int tmp=ans.n[k]+c;
ans.n[k]=tmp%10000;
c=tmp/10000;
}
}
}
return ans;
}
bignum operator-(const bignum& d)const{
bignum ans;
int b=0;
for(int i=0;i<maxn;i++){
ans.n[i]=n[i]-d.n[i]-b;
if(ans.n[i]<0){
ans.n[i]+=10000;
b=1;
}else b=0;
}
return ans;
}
bool operator>(const bignum&d)const{
for(int i=maxn-1;i>=0;i--){
if(n[i]>d.n[i])return true;
else if(n[i]<d.n[i])return false;
}
return false;
}
bool operator<(const bignum&d)const{
for(int i=maxn-1;i>=0;i--){
if(n[i]<d.n[i])return true;
else if(n[i]>d.n[i])return false;
}
return false;
}
};
int sp[10000];
int findnear(bignum x,bignum target){
int l=0,r=9;
bignum tmp;
bignum close;
// x.print();
// target.print();
while(r>=l){
//printf("%d~%d\n",l,r);
int mid=(l+r)>>1;
tmp=x.add(mid);
// printf(">>>>");tmp.print();
tmp=tmp.mul(mid);
// printf(">>>>");tmp.print();
if(tmp>target){
r=mid-1;
}else l=mid+1;
}
return r;
}
Int sqri[10000];
void solve(char str[]){
int k=0;
int len=strlen(str);
int j=0;
for(int i=len-1;i>=0;i--){
if(j&1){
sp[k]=sp[k]+(str[i]-'0')*10;
k++;
j=0;
}else{
sp[k]=str[i]-'0';
if(i==0)k++;
j=1;
}
}
// for(int i=0;i<k;i++)printf("<<%d\n",sp[i]);
for(int i=0;i<k/2;i++){
int tmp=sp[i];
sp[i]=sp[k-i-1];
sp[k-i-1]=tmp;
}
bignum a;
bignum b;
bignum tar;
bignum sqr;
char sqrc[150];
Set(sqrc,0);
for(int i=0;i<k;i++){
// a.print();
// b.print();
tar=tar.mul(100);
tar=tar.add(sp[i]);
// tar.print();
int index=findnear(a,tar);
// debug(">>%d\n",index);
sqrc[i]=index+'0';
a=a.add(index);
tar=tar-(a.mul(index));
a=a.add(index);
a=a.mul(10);
}
int s=0;
char stmp[100];
Set(stmp,0);
int t=0;
// sqr.print();
// puts(sqrc);
for(int i=k-1;i>=0;i--){
if(s<4){
Int tmp=sqrc[i]-'0';
if(s==1)tmp*=10;
if(s==2)tmp*=100;
if(s==3)tmp*=1000;
sqr.n[t]+=tmp;
s++;
}
if(s>=4){
t++;
s=0;
}
}
// sqr.print();
sqr=sqr*sqr;
sqr.print();
}
char inp[1005];
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%s",inp)){
// test();
if(!strcmp(inp,"0"))break;
solve(inp);
}
}
留言
張貼留言