Sort by

recency

|

30 Discussions

|

  • + 0 comments

    C solution

    int t;
    scanf("%d", &t);
    
    while(t--){
        int a_count;
        scanf("%d", &a_count);
        int a[a_count];
        for(int j = 0 ; j < a_count ; j++){
            scanf("%d", &a[j]);
        }
        
        int gcd[a_count+1];
        int result_count = 0;
        
        gcd[result_count++] = a[0];
        for(int i = 0 ; i < a_count-1 ; i++){
            int y = a[i];
            while(y % a[i+1] != 0 || (y % a[i] != 0 && a[i] % y != 0)){
                y++;
            }
            gcd[result_count++] = y;
        }
        gcd[result_count++] = a[a_count-1];
        
        for(int i = 0 ; i <= a_count ; i++){
            printf("%d ", gcd[i]);
        }
        printf("\n");
    }   
    
  • + 0 comments
    1. def gcd(a,b):

      if b==0 :

      return a
      
      • return gcd(b,a%b)![https://)

    def ppcm(a,b): return a*b//gcd(a,b)

    def solve(a): # Write your code here L=[a[0]] for i in range(len(a)-1): L.append(ppcm(a[i],a[i+1])) L.append(a[-1]) return L

  • + 0 comments

    include

    int gcd(int,int); int lcm(int,int); int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int lcm(int a, int b) { return (a*b)/gcd(a,b); } int main() { int t; scanf("%d",&t); while(t--) { int n,i; scanf("%d",&n); int a[n]; for(i=0 ; ia[i]) printf("%d ",lcm(a[i+1],a[i])); else printf("%d ",lcm(a[i],a[i+1])); } printf("%d\n",a[n-1]);
    } return 0; }

  • + 0 comments

    C# - the only problem, calculate min Multiple for 2 numbers

    private static int GCD(int a, int b)
    {
        return a == 0 ? b : GCD(b % a, a);
    }
    private static List<int> CalcNext(ref int prev, List<int> lst, int cur)
    {
        int gcd = GCD(prev,cur);
        lst.Add((prev/gcd)*(cur/gcd)*gcd);
        prev = cur;
        return lst;
    }
    public static List<int> solve(List<int> a)
    {
        int prev = a.ElementAt(0), prevB = prev;
        List<int> ret = a.Skip(1).Aggregate(new List<int>{prev}, (lst,cur)=>CalcNext(ref prev, lst, cur)).ToList();
        ret.Add(a.Last());
        return ret;
    }
    

    ...not the shortest code though

  • + 0 comments

    Some hint:

    Suppose the input is [A,B,C,,...]

    The first value of the list should be one number X such that : A = gcd(X,??) In other words: X should be multiple of A.

    Because the sum have to be minimal, lets try the lowest multiple of A (X = A).

    From here, whats the second number Y of the list? We have two restrictions of Y: A = gcd(X,Y) B = gcd(Y, ??) In other words Y should be a multiple of A and a multiple of B. Again, because the sum have to be minimal, get the lowest multiple of A and B ( Least Common Multiple)

    Iterate from there...