HDU 5223 GCD(模拟)

HDU 5223 GCD

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

题意:

给定一些区间的gcd要你还原原数列

分析:

模拟

直接求最小公倍数,然后最后检验

code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans[1010],l[1010],r[1010],a[1010];
int n;

void printAns(int flag){
    if(flag){
        puts("Stupid BrotherK!");
    }
    else{
        for(int i = 1; i <= n; i++){
            if(i != 1) printf(" ");
            printf("%lld",ans[i]);
        }
        puts("");
    }
}

ll gcd(ll a,ll b){
    return b ? gcd(b,a%b) : a;
}

ll lcm(ll a,ll b){
    return a / gcd(a,b) * b;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int Q,flag = 0;
        scanf("%d%d",&n,&Q);
        for(int i = 1; i <= n; i++) ans[i] = 1;
        for(int k = 0; k < Q; k++){
            scanf("%lld%lld%lld",&l[k],&r[k],&a[k]);
            for(int i = l[k]; i <= r[k]; i++){
                ans[i] = lcm(ans[i],a[k]);
                if(ans[i] >= 1e9) flag = 1;
            }
        }
        for(int k = 0; k < Q; k++){
            ll point = ans[l[k]];
            for(int i = l[k]+1; i <= r[k]; i++){
                point = gcd(ans[i],point);
            }
            if(point != a[k]) flag = 1;
        }
        printAns(flag);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/81660250