hdu 5223 GCD 思维题
来源:互联网 发布:淘宝卖家用的什么软件 编辑:程序博客网 时间:2024/05/16 11:53
GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 506 Accepted Submission(s): 227
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 arrayA 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 answeredQ questions, so Ery can only play with herself, but she don't know any elements in arrayA . Fortunately, Ery remembered all her questions and BrotherK's answer, now she wants to recovery the arrayA .
BrotherK and Ery like playing mathematic games. Today, they are playing a game with GCD.
BrotherK has an array
GCD(
BrotherK feels tired after he has answered
Input
The first line contains a single integer T , indicating the number of test cases.
Each test case begins with two integersN, 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
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, printN 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
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#include <iostream>#include<stdio.h>#include<algorithm>using namespace std;struct node{ int l,r; int gcd;}GCD[1000+10];int a[1000+10];int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b);}int lcm(int a,int b){ return a/gcd(a,b)*b;}int main(){ int t; cin>>t; while(t--) { int N,Q; cin>>N>>Q; for(int i=1;i<=N;i++) a[i]=1; for(int i=1;i<=Q;i++) { int l,r,gcd; scanf("%d%d%d",&l,&r,&gcd); GCD[i].l=l;GCD[i].r=r;GCD[i].gcd=gcd; for(int j=GCD[i].l;j<=GCD[i].r;j++) a[j]=lcm(a[j],GCD[i].gcd);//先按照规则把每个数求出来 } int ok=1; for(int i=1;i<=Q;i++) { int gcd2; gcd2=a[GCD[i].l]; for(int j=GCD[i].l;j<=GCD[i].r;j++) gcd2=gcd(gcd2,a[j]); if(gcd2!=GCD[i].gcd)//验证每个查询区间 { ok=0; break; } } if(ok==0) { printf("Stupid BrotherK!\n"); } else { for(int i=1;i<N;i++) cout<<a[i]<<" "; cout<<a[N]<<endl; } }}
0 0
- hdu 5223 GCD 思维题
- HDU 5223 - GCD (思维)
- Hdu 5726 GCD【思维+二分+区间Gcd】好题!
- HDU 6053 逆向思维 gcd
- hdu-5726 GCD 思维||二分
- Magical GCD ----思维题
- HDU 5505 GT and numbers【思维+GCD】
- HDU 5223 GCD
- hdu 5223 GCD
- 【模拟】 HDU 5223 GCD
- HDU 5223 GCD
- HDU ACM 5223 GCD
- HDU 5223 GCD
- hdu 5223 GCD
- hdu 5101 思维题
- hdu 5744 思维题
- hdu 5873 思维题
- HDU 5963 思维题
- 深入剖析Android四大组件(五)——并行执行的Service
- Scala 相比于 Java 的优势在哪里?
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- PDO执行SQL语句 exec query两种方法
- Cocos2d-JS中使用CocosStudio资源——ListView和ScrollView
- hdu 5223 GCD 思维题
- 《C++Primer读书笔记》(8)
- 不装oracle客户端使用plsql,以及中文乱码问号问题
- 集合详解(二):List
- IOS8下,令cell的分割线左右距离设置为0
- 安卓微博评论
- Java而言,Scala的
- IOS开发—iOS视频拍摄与压缩
- 加载txt文件到Hive表中