(赛码"BestCoder"杯中国大学生程序设计冠军赛)GCD

本文深入探讨了一个基于GCD(最大公约数)的数学游戏,通过解决一系列问题和答案,揭示了隐藏的数组元素。文章详细介绍了解题策略,包括最小公倍数的计算和数组元素的恢复过程,提供了一个有趣的数学思维训练案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GCD

 
 Accepts: 433
 
 Submissions: 1753
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 131072/131072 K (Java/Others)
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 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.

Sample Input
2
2 2
1 2 1
1 2 2
2 1
1 2 2
Sample Output
Stupid BrotherK!
2 2

#include <iostream>
#include<cstring>
using namespace std;
long long  v[1005],a[1005][3];

long long mm(long long  m,long long  n)
{
  long long  a,b,c;

        if( n > m) {
            a = n ;
            b = m ;
        }
        else {
            a = m ; b = n;
        }
        while ( b != 0 ) {  /* 最小公倍数 = m*n/GCD(m,n) */
            c = a % b ;
            a = b ;
            b = c ;
        }
    return a ;

}
long long  ma(long long  m,long long n){
    if(m==0)return n;
    else
return (m*n)/mm(m,n);
}
long long  gcd(long long  x,long long y){
long long  l=v[x];
for(int i=x+1;i<=y;i++)
l=mm(l,v[i]);

return l;
}
int main(){
int t;
cin>>t;
int n,m;
long long x,y,z;
while(t--)
{bool p=1;
memset(v,0,sizeof(v));
memset(a,0,sizeof(a));
 cin>>n>>m;
 for(int i=0;i<m;i++)
 {
     cin>>x>>y>>z;
     a[i][0]=x;a[i][1]=y;
     a[i][2]=z;
     for(int k=x;k<=y;k++){
     v[k]=ma(v[k],z);
     }
 }
int i;
for(int i=0;i<m;i++)
    if(gcd(a[i][0],a[i][1])!=a[i][2]){
        p=0;break;
   }

if(p==0)cout<<"Stupid BrotherK!";
else
{
if(v[1]==0)cout<<1;
else cout<<v[1];
for(int i=2;i<=n;i++)

    if(v[i]==0)cout<<" "<<1;
else cout<<" "<<v[i];
}

cout<<endl;
}
return 0;}

转载于:https://www.cnblogs.com/lengxia/p/4472249.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值