Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #define ULL unsigned long long int
- #define NMax 40
- using namespace std;
- ofstream g("bomboane4.out");
- typedef int Huge[NMax+3];
- void AtribValue(Huge H,ULL X) /// H <- X
- {
- H[0]=0;
- while (X)
- ++H[0],H[H[0]]=X%10,X/=10;
- }
- void MultHuge(Huge A, Huge B, Huge C)
- /* C <- A x B */
- { 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;
- }
- ULL Divide(Huge A,ULL X) /// A <- A/X si intoarce A%X
- {
- ULL R=0;
- for(int i=A[0];i;--i)
- A[i]=(R=10*R+A[i])/X,R%=X;
- while(!A[A[0]]&&A[0]>1)
- --A[0];
- return R;
- }
- void Subtract(Huge A,Huge B) /// A <- A-B
- {
- ULL 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];
- }
- void Afisare(Huge H)
- {
- for(int i=H[0];i>0;--i)
- g<<H[i];
- g<<'\n';
- }
- void cmmmcM(ULL a,ULL b)
- {
- ULL r;
- Huge A,B,P;
- AtribValue(A,a); /// A <- a
- AtribValue(B,b); /// B <- a
- MultHuge(A,B,P); /// P <- A*B (adica P=a*b)
- while(b!=0)
- r=a%b,a=b,b=r;
- Divide(P,a);
- Huge UNU;
- AtribValue(UNU,1); /// UNU <- 1
- Subtract(P,UNU);
- Afisare(P);
- }
- int main()
- {
- ULL p,q;
- ifstream f("bomboane4.in");
- f>>p>>q;
- f.close();
- cmmmcM(p,q);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement