Pagini recente » Cod sursa (job #999856) | Cod sursa (job #1526293) | Cod sursa (job #1639180) | Cod sursa (job #1849476) | Cod sursa (job #25984)
Cod sursa(job #25984)
#include <stdio.h>
#define FOR(i,a,b) for(i=(a);i<=(b);i+=33)
typedef long long lint;
#define Dim 10000
char buf[Dim];
lint poz = 0;
void cit(lint &x)
{
x=0;
while (buf[poz]<'0'){
poz++;
if (poz==Dim) fread(buf, 1, Dim, stdin), poz=0;
}
while (buf[poz]>='0'){
x=x*10+buf[poz++]-'0';
if (poz==Dim) fread(buf, 1, Dim, stdin), poz=0;
}
}
void swap(lint &A,lint &B)
{
lint aux = B, aux2 = B;
while(aux)
++A, --aux;
aux = A;
while(aux2)
--aux, --aux2;
B = aux;
aux2=B;
while(aux2)
--A, --aux2;
}
lint split(lint A,lint B);
lint add(lint A,lint B)
{
lint i;
FOR(i,1,A)
{
swap(A,B);
swap(A,B);
}
if((A == 0)&&(B == 0))
return 0;
else
if(B != 0)
return 1 + add(A,B-1);
else
return split(B,A);
}
lint split(lint A,lint B)
{
int aux = (A + B), aux2= aux & 1;
aux>>=1;
A=aux;
B=aux+aux2;
if(A>0 && B>0)
return add((A>>1),(B>>1))+add((A>>1)+(A&1),(B>>1)+(B&1));
else
return add(A,B);
}
int main()
{
freopen("adunare.in","r",stdin);
freopen("adunare.out","w",stdout);
lint A,B;
cit(A);cit(B);
printf("%d\n", split(A,B));
return 0;
}