HDU 5223 GCD

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5223


题面:

GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4    Accepted Submission(s): 0


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
 

Source
 

Recommend
hujie
 


解题:出现不可能的情况是,A区间的长度比B的大,而公约数却比B大,那么是不可能发生的,排除掉这一点就可以了。因为数据量不大,先把每个位置初始值赋值为1,只要枚举过去,找出每一个位置和该区间的公约数C的公约数D,如果该位置的数能整除C,不做操作。如果不能整除就将该点的值乘上C/D(现在想想也不用分类,直接乘上C/D也行,两种方法效率上差不多 )这样出来就可以保证是最小值了。代码中的排序多此一举了。


代码:

#include <cstdio>
#include <algorithm>
using namespace std;
long long int store[1005];
struct info
{
    long long int fm,to,val;
}storage[1005];
int gcd (long long int a,long long int b)
{
    if(a==0)return b;
    else return gcd(b%a,a);
}
bool cmp(info a,info b)
{
    return a.val>b.val;
}
int main()
{
    long long int n,q,l,r,tmp,cnt,x,t;
    bool flag;
    scanf("%I64d",&t);
    while(t--)
    {
        scanf("%I64d%I64d",&n,&q);
        cnt=0;
        flag=true;
        for(long long int i=1;i<=q;i++)
        {
            scanf("%I64d%I64d%I64d",&l,&r,&tmp);
            storage[cnt].fm=l;
            storage[cnt].to=r;
            storage[cnt].val=tmp;
            cnt++;
            if(flag)
            {
                for(long long int j=0;j<cnt;j++)
                {
                    if((l<=storage[j].fm&&r>=storage[j].to&&tmp>storage[j].val)||(storage[j].fm<=l&&storage[j].to>=r&&storage[j].val>tmp))
                    {
                        flag=false;
                        break;
                    }
                }
            }
        }
        if(flag)
        {
            //sort(storage+1,storage+n+1,cmp);
            for(long long int j=1;j<=n;j++)
            store[j]=1;
            for(long long int j=0;j<cnt;j++)
            {
                l=storage[j].fm;
                r=storage[j].to;
                tmp=storage[j].val;
                for(long long int k=l;k<=r;k++)
                {
                    if(store[k]%tmp==0)
                    continue;
                    else
                    {
                      x=gcd(store[k],tmp);
                      store[k]*=tmp/x;    
                    }
                }
            }
            printf("%I64d",store[1]);
            for(long long int i=2;i<=n;i++)
            printf(" %I64d",store[i]);
            printf("\n");
        }
        else
        {
            printf("Stupid BrotherK!\n");
        }
    }
    return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值