HDU 5223 GCD

Problem Description
In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.---Wikipedia 

BrotherK and Ery like playing mathematic games. Today, they are playing a game with GCD. 

BrotherK has an array  A  with  N  elements:  A1  ~  AN , each element is a integer in [1, 10^9]. Ery has  Q  questions, the i-th question is to calculate
GCD( ALi, ALi+1, ALi+2, ..., ARi ), and BrotherK will tell her the answer. 

BrotherK feels tired after he has answered  Q  questions, so Ery can only play with herself, but she don't know any elements in array  A . Fortunately, Ery remembered all her questions and BrotherK's answer, now she wants to recovery the array  A .
 

Input
The first line contains a single integer  T , indicating the number of test cases.

Each test case begins with two integers  N, Q , indicating the number of array  A , and the number of Ery's questions. Following  Q  lines, each line contains three integers  Li, Ri  and  Ansi , describing the question and BrotherK's answer.

T  is about 10

2  N Q  1000

1  Li < Ri  N

1  Ansi  109
 

Output
For each test, print one line.

If Ery can't find any array satisfy all her question and BrotherK's answer, print "Stupid BrotherK!" (without quotation marks). Otherwise, print  N  integer, i-th integer is  Ai .

If there are many solutions, you should print the one with minimal sum of elements. If there are still many solutions, print any of them.
 

Sample Input
  
  
2 2 2 1 2 1 1 2 2 2 1 1 2 2
 

Sample Output
  
  
Stupid BrotherK! 2 2

直接暴力

#include<cstdio>
#include<string>
#include<queue>
#include<cstring>
#include<iostream>
#include<map>
#include<stack>
#include<malloc.h>
#include<algorithm>
using namespace std;
const int maxn = 1005;
//const long long base = 4294967296;
int T, n, m, l[maxn], r[maxn], ans[maxn], x[maxn];

int gcd(int x, int y)
{
    if (x%y) return gcd(y, x%y);
    return y;
}
int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) x[i] = 1;
        int F = 1;
        for (int i = 1; i <= m; i++)
        {
            scanf("%d%d%d", &l[i], &r[i], &ans[i]);
            for (int j = l[i]; j <= r[i]; j++)
            {
                x[j] = x[j] / gcd(ans[i], x[j]);
                if ((double)x[j] * ans[i] > 1e9) F = 0;
                x[j] = x[j] * ans[i];
            }
        }
        if (F)
        for (int i = 1, u; i <= m; i++)
        {
            u = x[l[i]];
            for (int j = l[i]; j <= r[i]; j++) u = gcd(u, x[j]);
            if (u != ans[i]) { F = 0; break; }
        }
        if (F)
        {
            for (int i = 1; i < n; i++) printf("%d ", x[i]);
            printf("%d\n", x[n]);
        }
        else printf("Stupid BrotherK!\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值