A+B Problem _ C++(嘿嘿)
阿新 • • 發佈:2018-12-13
題目背景
作為所有程式語言的最“難”(一點也不難是假的)的一道題,曾近難倒了許多“大佬”,本次提供幾種“簡單”演算法(嘿嘿);
題目描述
a+b problem;
輸入輸出格式
輸入格式:
分兩行輸入a,b<=10^500;
輸出格式:
輸出只有一行,代表A+B的值
First.
最“難”的方法,輸入a,b,再相加輸出(雖計算的範圍不大,但"思維難度大")
#include <iostream> #include <cstdio> using namespace std; int main() { int a,b; cin >> a >> b; cout << a + b; return 0; }
Second.
來個Link-Cut Tree的a+b,這個相比上一個要簡單的多(嘿嘿)
作者Treeloveswater的方法:
#include<iostream> #include<cstring> #include<cstdio> #include<cstring> using namespace std; struct node { int data,rev,sum; node *son[2],*pre; bool judge(); bool isroot(); void pushdown(); void update(); void setson(node *child,int lr); }lct[233]; int top,a,b; node *getnew(int x) { node *now=lct+ ++top; now->data=x; now->pre=now->son[1]=now->son[0]=lct; now->sum=0; now->rev=0; return now; } bool node::judge(){return pre->son[1]==this;} bool node::isroot() { if(pre==lct)return true; return !(pre->son[1]==this||pre->son[0]==this); } void node::pushdown() { if(this==lct||!rev)return; swap(son[0],son[1]); son[0]->rev^=1; son[1]->rev^=1; rev=0; } void node::update(){sum=son[1]->sum+son[0]->sum+data;} void node::setson(node *child,int lr) { this->pushdown(); child->pre=this; son[lr]=child; this->update(); } void rotate(node *now) { node *father=now->pre,*grandfa=father->pre; if(!father->isroot()) grandfa->pushdown(); father->pushdown();now->pushdown(); int lr=now->judge(); father->setson(now->son[lr^1],lr); if(father->isroot()) now->pre=grandfa; else grandfa->setson(now,father->judge()); now->setson(father,lr^1); father->update();now->update(); if(grandfa!=lct) grandfa->update(); } void splay(node *now) { if(now->isroot())return; for(;!now->isroot();rotate(now)) if(!now->pre->isroot()) now->judge()==now->pre->judge()?rotate(now->pre):rotate(now); } node *access(node *now) { node *last=lct; for(;now!=lct;last=now,now=now->pre) { splay(now); now->setson(last,1); } return last; } void changeroot(node *now) { access(now)->rev^=1; splay(now); } void connect(node *x,node *y) { changeroot(x); x->pre=y; access(x); } void cut(node *x,node *y) { changeroot(x); access(y); splay(x); x->pushdown(); x->son[1]=y->pre=lct; x->update(); } int query(node *x,node *y) { changeroot(x); node *now=access(y); return now->sum; } int main() { scanf("%d%d",&a,&b); node *A=getnew(a); node *B=getnew(b); //連邊 Link connect(A,B); //斷邊 Cut cut(A,B); //再連邊orz Link again connect(A,B); printf("%d\n",query(A,B)); return 0; } //作者-洛谷-Treeloveswater
Third.
自己編了個高精的,這個沒有那些大佬的"簡單",,,
#include <iostream> #include <cstring> #define _MAX 100001 using namespace std; typedef long long ll; ll l1, l2; string s1, s2; ll a1[_MAX], a2[_MAX], h[_MAX]; int main() { cin >> s1 >> s2; if(s1.size() < s2.size()) swap(s1, s2); l1 = s1.size(); l2 = s2.size(); for(int i = 0; i < l1; i++) { a1[i + 1] = s1[i] - '0'; //cout << a1[i + 1]; } //cout << endl; for(int i = l1 - l2; i < l1; i++) { a2[i + 1] = s2[i - l1 + l2] - '0'; } /* for(int i = 1; i <= l1; i++) { cout << a2[i]; }*/ for(int i = l1; i >= 0; i--) { if(a1[i] + a2[i] >= 10) { h[i] = a1[i] + a2[i] - 10; a1[i - 1] += 1; if(i - 1 == 0) a1[0] = 1; } else { h[i] = a1[i] + a2[i]; } } if(h[0] != 0) for(int i = 0; i <= l1; i++) { cout << h[i]; } else for(int i = 1; i <= l1; i++) { cout << h[i]; } return 0; }
Next.
看到doby編的SPFA,Floyd了,這個還不錯,有點東西(嘿嘿)
//SPFA by doby
#include<cstdio>
using namespace std;
int n,m,a,b,op,head[200009],next[200009],dis[200009],len[200009],v[200009],l,r,team[200009],pd[100009],u,v1,e;
int lt(int x,int y,int z)
{
op++,v[op]=y;
next[op]=head[x],head[x]=op,len[op]=z;
}
int SPFA(int s,int f)//SPFA……
{
for(int i=1;i<=200009;i++){dis[i]=999999999;}
l=0,r=1,team[1]=s,pd[s]=1,dis[s]=0;
while(l!=r)
{
l=(l+1)%90000,u=team[l],pd[u]=0,e=head[u];
while(e!=0)
{
v1=v[e];
if(dis[v1]>dis[u]+len[e])
{
dis[v1]=dis[u]+len[e];
if(!pd[v1])
{
r=(r+1)%90000,
team[r]=v1,
pd[v1]=1;
}
}
e=next[e];
}
}
return dis[f];
}
int main()
{
scanf("%d%d",&a,&b);
lt(1,2,a);lt(2,3,b);//1到2為a,2到3為b,1到3即為a+b……
printf("%d",SPFA(1,3));
return 0;
}
// Floyd, by doby
#include<iostream>
#include<cstring>
using namespace std;
long long n=3,a,b,dis[4][4];
int main()
{
cin>>a>>b;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dis[i][j]=2147483647;
}
}
dis[1][2]=a,dis[2][3]=b;
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);//Floyd……
}
}
}
cout<<dis[1][3];
}
哎,學不來這些“簡單”的(╮(╯▽╰)╭)
好的,OK