HDU 5223 GCD,hdu5223gcd
賽碼杯大賽的水題。。。
直接按照題意搞,最後判斷一下是否合法。。也就是所有的詢問是否被滿足。。。
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 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;}