Cod sursa(job #169503)

Utilizator stefynr8Space Monkey stefynr8 Data 1 aprilie 2008 19:03:37
Problema Oz Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream.h>   
#include <math.h>   
#include <iostream.h>   
int main()   
  
{   
 fstream f1("oz.in",ios::in);   
 fstream f2("oz.out",ios::out);   
  
 long long m,n,i,j,d,a,b,h,v[10000],p,nr;

 f1>>n;
 f1>>m;

 for(h=1;h<=n;h++)
    {
     v[h]=1;
     }

 for(h=1;h<=m;h++)
    {
     f1>>i;
     f1>>j;
     f1>>d;

     //descompunem pe d in factori primi
     a=2;
     do{
    p=0;
    while(d%a==0)
	 {
	  p=p+1;
	  d=d/a;
          }   
  
    if(p>0){   
        //inmultim pe v[i] cu a k a la putearea p sa fie divizor a lu v[i]   
        nr=0;   
        b=v[i];   
        while(b%a==0)   
             {   
               nr++;   
               b=b/a;   
               }   
        if(p-nr>0){   
               v[i]=v[i]*pow(a,p-nr);   
               }   
  
        //la fel si pt v[j]   
        nr=0;   
        b=v[j];   
        while(b%a==0)   
             {   
               nr++;   
               b=b/a;   
               }   
        if(p-nr>0){   
               v[j]=v[j]*pow(a,p-nr);   
               }   
  
        }   
    a=a+1;   
  
    }while(d!=1);   
     }   
  
 for(h=1;h<=n;h++)   
    {   
     f2<<v[h]<<" ";   
     }   
  
 f1.close();   
 f2.close();   
  
 return 0;   
}