Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- using namespace std;
- ifstream f("arbori.in" );
- ofstream g("arbori.out");
- #define H_SIZE 100
- struct HUGE
- {
- short cifra[H_SIZE];
- short& operator[](int x)
- {
- return cifra[x];
- }
- void operator=(int x)
- {
- memset(cifra, 0, sizeof cifra);
- while ( x )
- {
- cifra[++cifra[0]] = x%10;
- x /= 10;
- }
- }
- friend HUGE operator*(HUGE H, int X)
- {
- unsigned long T=0;
- for ( int i=1; i<=H[0]; i++)
- {
- H[i]=H[i]*X+T;
- T=H[i]/10;
- H[i]=H[i]%10;
- }
- while (T) /* Cat timp exista transport */
- {
- H[++H[0]]=T%10;
- T/=10;
- }
- return H;
- }
- friend HUGE operator*(HUGE A, HUGE B)
- /* C <- A x B */
- {
- HUGE C; memset(C.cifra, 0, sizeof C.cifra);
- int i,j,T=0;
- C[0]=A[0]+B[0]-1;
- for (i=1; i<=A[0]+B[0];) C[i++]=0;
- for (i=1; i<=A[0]; i++)
- for (j=1; j<=B[0]; j++)
- C[i+j-1]+=A[i]*B[j];
- for (i=1; i<=C[0]; i++)
- {
- T=(C[i]+=T)/10;
- C[i]%=10;
- }
- if (T) C[++C[0]]=T;
- return C;
- }
- friend HUGE operator+(HUGE A, HUGE B)
- {
- HUGE result; memset(result.cifra, 0, sizeof result.cifra);
- short r = 0;
- for ( int i=1; i<=A[0] || i<=B[0]; i++ )
- {
- result[i] = A[i] + B[i] + r;
- r = result[i]/10;
- result[i] %= 10;
- }
- result[0] = max(A[0], B[0]);
- if ( r )
- result[++result[0]] = r;
- return result;
- }
- friend HUGE operator-(HUGE A, HUGE B)
- {
- int T=0;
- for ( int i=B[0]+1; i<=A[0]; ) B[i++]=0;
- for ( int i=1; i<=A[0]; i++)
- A[i]+= (T=(A[i]-=B[i]+T)<0) ? 10 : 0;
- while ( !A[A[0]] && A[0] ) A[0]--;
- return A;
- }
- friend ostream& operator<<(ostream& afs, HUGE A)
- {
- if ( !A[0] ) afs << 0;
- for ( int i=A[0]; i>=1; i-- )
- afs << A[i];
- return afs;
- }
- friend istream& operator>>(istream& in, HUGE& A)
- {
- char readbuff[H_SIZE];
- in >> readbuff;
- A[0] = strlen(readbuff);
- for ( int i=1; i<=A[0]; i++ )
- A[A[0]-i+1] = readbuff[i-1] - '0';
- return in;
- }
- friend int compare(HUGE H1, HUGE H2) {
- // Elimina zero-urile semnificative, daca exista.
- while (H1[0] && !H1[H1[0]]) H1[0]--;
- while (H2[0] && !H2[H2[0]]) H2[0]--;
- if (H1[0] < H2[0]) {
- return -1;
- } else if (H1[0] > H2[0]) {
- return +1;
- }
- for (int i = H1[0]; i > 0; --i) {
- if (H1[i] < H2[i]) {
- return -1;
- } else if (H1[i] > H2[i]) {
- return +1;
- }
- }
- return 0;
- }
- };
- HUGE A, B;
- int main()
- {
- f >> A >> B;
- g << A * 4 + A;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement