GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 591 Accepted Submission(s): 262
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 .
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
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.
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
赛码"BestCoder"杯中国大学生程序设计冠军赛
题目分析:
首先将所有的数初始化为1,为了 满足条件且保证最小,我们把每个查询区间的数更新为当前数和给定数的最小公倍数
然后再跑一遍查询,判断当前修改有没有导致其他区间的最大公约数受到影响,没有影响直接输出,有影响的话输出stupid
题目分析:
首先将所有的数初始化为1,为了 满足条件且保证最小,我们把每个查询区间的数更新为当前数和给定数的最小公倍数
然后再跑一遍查询,判断当前修改有没有导致其他区间的最大公约数受到影响,没有影响直接输出,有影响的话输出stupid
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 1007
using namespace std;
typedef long long LL;
int t,n,q;
LL a[MAX],l[MAX],r[MAX],v[MAX];
bool flag;
LL gcd ( LL a , LL b )
{
return b == 0 ? a : gcd ( b , a%b );
}
int main ( )
{
scanf ( "%d" , &t );
while ( t-- )
{
flag = true;
scanf ( "%d%d" , &n , &q );
for ( int i = 1 ; i <= n ; i++ )
a[i] = 1;
for ( int i = 0 ; i < q ; i++ )
scanf ( "%I64d%I64d%I64d" , &l[i] , &r[i] , &v[i] );
for ( int i = 0 ; i < q ; i++ )
{
for ( int j = l[i] ; j <= r[i] ; j++ )
{
LL d = gcd ( a[j], v[i] );
a[j] = a[j]*v[i]/d;
}
}
bool flag = true;
for ( int i = 0 ; i < q ; i ++ )
{
LL d = a[l[i]];
for ( int j = l[i]+1 ; j <= r[i] ; j++ )
d = gcd ( d , a[j] );
if ( d != v[i] ) flag = false;
}
if ( flag )
{
printf ( "%I64d" , a[1] );
for ( int i = 2 ; i <= n ; i++ )
printf ( " %I64d" , a[i] );
puts ("");
}
else puts ( "Stupid BrotherK!");
}
}