HDU 5223 GCD, hdu5221_cd

Source: Internet
Author: User
Tags greatest common divisor

HDU 5223 GCD, hdu5221_cd

The question of the competition code cup...

Follow the meaning of the question and finally determine whether it is legal .. That is, whether all the queries are satisfied...

GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission (s): 240 Accepted Submission (s): 131


Problem DescriptionIn 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 .
InputThe 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  
OutputFor 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 already solutions, you shocould print the one with minimal sum of elements. If there are still then solutions, print any of them.
Sample Input
22 21 2 11 2 22 11 2 2
 
Sample Output
Stupid BrotherK!2 2
 
Source competition code "BestCoder" cup Chinese College Students' Program Design Championship
#include <bits/stdc++.h>using namespace std;#define prt(k) cerr<<#k" = "<<k<<endl#define pb push_back#define MP make_pair#define fi  first#define se second#define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define SZ(X) ((int)(v).size())#define For(i,l,r) for (int i = l; i <= r; ++i)#define Cor(i,l,r) for (int i = l; i >= r; --i)#define Fill(a,b) memset(a,b,sizeof(a))#define READ freopen("a.in","r",stdin);freopen("a.out","w",stdout)void read(string t){    freopen((t+".in").c_str(),"r",stdin);    freopen((t+".out").c_str(),"w",stdout);}const int inf = 0x3f3f3f3f;typedef long long ll;typedef vector<int> VI;typedef vector<VI> VII;typedef pair<int,int> pii;template <class T>void Max(T &a, T b) { a=max(a, b); }const int N = 2333;ll a[N];ll gcd(ll a, ll b){return b==0 ? a : gcd(b, a%b);}ll lcm(ll a, ll b){return a / gcd(a, b) * b;}int n, Q;struct P{int l, r, a;} q[N];int main(){int re,ca=1; cin>>re;while (re--) {cin>>n>>Q;for (int i=1;i<=n;i++) a[i] = 1;for (int i=0;i<Q;i++) {scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].a);int l=q[i].l, r = q[i].r;for (int j = l; j <= r; j++) {                                a[j] = lcm(a[j], q[i].a);}}bool ok = true;for (int i=0;i<Q;i++) {int l=q[i].l, r = q[i].r;ll g = a[l];for (int j=l+1;j<=r;j++) g = gcd(g, a[j]);if (g != q[i].a) ok = false;}if (!ok) {puts("Stupid BrotherK!");continue;}for (int i=1;i<=n;i++) {printf("%I64d%c", a[i], i<n ? ' ' : 10);}}return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.