hdu5223 GCD

题目链接: hdu5223 ( GCD )

对一个区间 \([L,R]\)\(ans\) 应是该区间的所有数共有的因子,不断求 \(lcm\) 最后检验即可。

/**
 * hdu5223 GCD
 *
 */

#include <cstdio>
#include <algorithm>
using namespace std;

typedef long long LL;
LL gcd(LL a, LL b)
{
    LL t;
    while (b) {
        t = a%b;
        a = b;
        b = t;
    }
    return a;
}

const int N = 1003;
LL a[N], L[N], R[N], ans[N];
int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        int n,q;
        scanf("%d%d", &n, &q);
        fill(a, a+1+n, 1);
        for (int i = 1; i <= q; ++i) scanf("%lld%lld%lld", &L[i], &R[i], &ans[i]);
        for (int i = 1; i <= q; ++i) {
            for (int j = L[i]; j <= R[i]; ++j) {
                a[j] = a[j]/gcd(a[j],ans[i])*ans[i];
                if (a[j] > 1e9) {
                    puts("Stupid BrotherK!");
                    goto over;
                }
            }
        }
        for (int i = 1; i <= q; ++i) {
            int d = 0;
            for (int j = L[i]; j <= R[i]; ++j) d = gcd(d, a[j]);
            if (ans[i] == d) continue;
            puts("Stupid BrotherK!");
            goto over;
        }
        printf("%lld", a[1]);
        for (int i = 2; i <= n; ++i) {
            printf(" %lld", a[i]);
        }
        puts("");
        over:;
    }
    return 0;
}

要记得将每一个测试样例的输入都处理完,否则会影响后面的输入,产生意想不到的错误。

posted @ 2021-02-17 12:24  Zewbie  阅读(62)  评论(0编辑  收藏  举报