Cod sursa(job #273512)

Utilizator RobytzzaIonescu Robert Marius Robytzza Data 8 martie 2009 18:04:54
Problema Oz Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <stdio.h>

long n,m,sir[101000];
long a,b,d,r,ok;

long cmmdc(long a,long b)
{
     while(b)
     {
          r=a%b;
          a=b;
          b=r;
     }
     return a;
}

int main ()
{
     freopen ("oz.in","r",stdin);
     freopen ("oz.out","w",stdout);
     scanf ("%ld %ld",&n,&m);
     for (long i=0;i<=n;i++)
          sir[i]=1;
     long x,x1,x2;
     while (m)
     {
          m--;
          scanf ("%ld %ld %ld",&a,&b,&d);
          x=cmmdc(sir[a],sir[b]);
          x1=d/cmmdc(d,sir[a]);
          sir[a]*=x1;
          x2=d/cmmdc(d,sir[b]);
          sir[b]*=x2;
          if (cmmdc(sir[a],sir[b])!=d)
          {
               ok=1;
               break;
          }
          if (sir[a]>2000000000)
          {
               ok=1;
               break;
          }
          if (sir[b]>2000000000)
          {
               ok=1;
               break;
          }
     }
     if (ok==1)
          printf("-1");
     else
          for (long i=1;i<=n;i++)
               printf("%ld ",sir[i]);
     printf("\n");
     return 0;
}