hdu 赛码1010 GCD

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
2
2 2
1 2 1
1 2 2
2 1
1 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值