Cod sursa(job #1900478)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 3 martie 2017 13:27:11
Problema Oz Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <bits/stdc++.h>
using namespace std;
using namespace placeholders;
using ll = long long;

constexpr ll maxval = 2e9, maxn = 1e4 + 100, maxm = 1e5 + 100;

void lcm_eq(ll &x, const ll y){
    x = min(x * y / __gcd(x, y), maxval+1); }

ifstream f("oz.in");
ofstream g("oz.out");
ll v[maxn] = {};
int n, m, mucs[maxm][3] = {};

int main(){
    f >> n >> m;
    for(int i = 0 ; i < m; ++i) for(auto& x : mucs[i]) f >> x;
    fill(v, v+maxn, 1);
    for(int i = 0; i < m; ++i)
        for(int j = 0; j < 2; ++j)
            lcm_eq(v[mucs[i][j]], mucs[i][2]);
    if(any_of(v+1, v+n+1, bind(greater<ll>(), _1, maxval)) ||
       any_of(mucs, mucs+m, [](const int mm[3]){
        return __gcd(v[mm[0]], v[mm[1]]) != mm[2]; })){
        g << -1 << endl; }
    else{
        copy_n(v+1, n, ostream_iterator<int>(g, " "));
        g << endl; }
    return 0; }