标签:
赛码杯大赛的水题。。。
直接按照题意搞,最后判断一下是否合法。。也就是所有的询问是否被满足。。。
GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 240 Accepted Submission(s): 131
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 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
2
2 2
1 2 1
1 2 2
2 1
1 2 2
Sample Output
Source
#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;
}
HDU 5223 GCD
标签:
原文地址:http://blog.csdn.net/oilover/article/details/45476403