hdu 赛码1010 GCD

来源:互联网 发布:c语言static 编辑:程序博客网 时间:2024/06/07 00:18

GCD

 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 131072/131072 K (Java/Others)
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
22 21 2 11 2 22 11 2 2
Sample Output
Stupid BrotherK!2 2


n个数 q个已知区间的最大公约数  求出这n个数的值 是的这个n个数的和最小

直接构造  注意那个无解的判断

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 1010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char ch;    int a = 0;    while((ch = getchar()) == ' ' | ch == '\n');    a += ch - '0';    while((ch = getchar()) != ' ' && ch != '\n')    {        a *= 10;        a += ch - '0';    }    return a;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}ll gcd(ll a,ll b){    if(b==0) return a;    else return gcd(b,a%b);}ll l[MAXN],r[MAXN],a[MAXN];ll b[MAXN];int main(){//    fread;    int tc;    scanf("%d",&tc);    while(tc--)    {        int n,q;        scanf("%d%d",&n,&q);        for(int i=0;i<q;i++)            scanf("%I64d%I64d%I64d",&l[i],&r[i],&a[i]);        int flag=0;        for(int i=0;i<q;i++)            for(int j=i+1;j<q;j++)        {            if((l[j]>=l[i]&&r[j]<=r[i]&&a[j]<a[i])||(l[j]<=l[i]&&r[j]>=r[i]&&a[j]>a[i]))            {                flag=1;            }        }        if(flag)        {            puts("Stupid BrotherK!");            continue;        }        for(int i=1;i<=n;i++)            b[i]=1;        for(int i=0;i<q;i++)        {            for(int j=l[i];j<=r[i];j++)            {                if(a[i]%b[j]==0)                {                    b[j]=a[i];                }                else                {                    ll tmp=(a[i]*b[j])/gcd(a[i],b[j]);                    b[j]=tmp;                }            }        }        for(int i=1;i<=n;i++)        {            if(i>1) printf(" ");            printf("%I64d",b[i]);        }        puts("");    }    return 0;}





0 0