Category Archives: GCD and/or LCM
11388 – GCD LCM
#include <iostream> #include <stdio.h> using namespace std; int main() { long long G,L,a,b; int nCases; scanf("%d",&nCases); while(nCases-->0) { a=b=1; scanf("%lld %lld",&G,&L); int factor; factor=1; bool check; check=true; while(check&&a<=L) { a=G*factor; factor++; b=(G*L)/a; if(b%G==0&&L%b==0) check=false; } if(!check) printf("%lld %lld\n",a,b); else printf("-1\n"); } return 0; }
that’s my solution
another solution was discussed in the board:
long G,L; scanf("%ld %ld",&G,&L); if(L%G !=0) printf(“-1″); else printf(G L)
so Simple
10193 – All You Need Is Love
#include <iostream> #include <stdio.h> #include <cstring> #include <math.h> using namespace std; int GCD(int a,int b) { if(b==0) return a; else return GCD(b,a%b); } int main() { char s1[50]; char s2[50]; int nCases; scanf("%d",&nCases); getchar(); int test; test=1; while(nCases-->0) { gets(s1); int j,len1,len2; len1=strlen(s1); gets(s2); len2=strlen(s2); int n1,n2; n1=n2=0; for(j=0;j<len1;j++) if(s1[j]=='1') { n1=n1+(int)pow(2,len1-j-1); } for(j=0;j<len2;j++) if(s2[j]=='1') { n2=n2+(int)pow(2,len2-j-1); } if(GCD(n1,n2)>1) printf("Pair #%d: All you need is love!\n",test++); else printf("Pair #%d: Love is not all you need!\n",test++); } return 0; }