牛客网暑期ACM多校训练营(第六场)J Heritage of skywalkert(思维)

链接:https://www.nowcoder.com/acm/contest/144/J
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

skywalkert, the new legend of Beihang University ACM-ICPC Team, retired this year leaving a group of newbies again.
 

Rumor has it that he left a heritage when he left, and only the one who has at least 0.1% IQ(Intelligence Quotient) of his can obtain it.


To prove you have at least 0.1% IQ of skywalkert, you have to solve the following problem:

Given n positive integers, for all (i, j) where 1 ≤ i, j ≤ n and i ≠ j, output the maximum value among . means the Lowest Common Multiple.

输入描述:

The input starts with one line containing exactly one integer t which is the number of test cases. (1 ≤ t ≤ 50)

For each test case, the first line contains four integers n, A, B, C. (2 ≤ n ≤ 107, A, B, C are randomly selected in unsigned 32 bits integer range)

The n integers are obtained by calling the following function n times, the i-th result of which is ai, and we ensure all ai > 0. Please notice that for each test case x, y and z should be reset before being called.

No more than 5 cases have n greater than 2 x 106.

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x is the test case number (starting from 1) and y is the maximum lcm.

 

示例1

输入

复制

2
2 1 2 3
5 3 4 8

输出

复制

Case #1: 68516050958
Case #2: 5751374352923604426

题意:给出n,a,b,c,按照题目给的式子求a[1]~a[n]。最后求所有(i,j)的最大LCM。

思路:思维啊。其实我更想说这道题坑爹。保留最大的20项,然后暴力求就能过。大多数人是保留的100项也可以。

而且姿势不好会TLE。。。

用sort会超时,只能用一个sort和直接插入排序。。。

代码:

#include<bits/stdc++.h>
#define ull unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=210;
unsigned n,a[maxn],f;
unsigned ans;
ull gcd(ull a,ull b){
    return b?gcd(b,a%b):a;
}
ull lcm(ull a,ull b)
{
    return a/gcd(a,b)*b;
}
unsigned x,y,z,A,B,C;
unsigned tang()
{
    unsigned t;
    x ^= x << 16;
    x ^= x >> 5;
    x ^= x << 1;
    t = x;
    x = y;
    y = z;
    z = t ^ x ^ y;
    return z;
}
int main()
{
    int T,cas=1;
    cin>>T;
    while(T--){
        cin>>n>>A>>B>>C;
        x=A;y=B;z=C;f=0;
        for(int i=1;i<=n;i++){
            unsigned aa=tang();
            if(f<20) {a[f++]=aa;sort(a,a+f);}
            else
            {
                int j=0;
                while(j<f&&a[j]<aa)j++;
                j--;
                for(int k=0;k<j;k++)
                {
                    a[k]=a[k+1];
                }
                if(j>=0)a[j]=aa;
            }
        }
        ull ans=a[0];
        for(int i=0;i<f;i++)
        for(int j=0;j<f;j++)
        {
            ans=max(ans,lcm(a[i],a[j]));
        }
          cout<<"Case #"<<cas++<<": "<<ans<<endl;
      //  if(flag) puts("Yes"); else puts("No");
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值