HDU 5223 GCD,hdu5223gcd

來源:互聯網
上載者:User

HDU 5223 GCD,hdu5223gcd

賽碼杯大賽的水題。。。

直接按照題意搞,最後判斷一下是否合法。。也就是所有的詢問是否被滿足。。。

GCDTime 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 isAi.

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
 
Source賽碼"BestCoder"杯中國大學生程式設計冠軍賽 
#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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.