链接:https://www.nowcoder.com/acm/contest/144/J
来源:牛客网
题目描述
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,D四个参数根据某个奇怪的代码生成,问你在这n个数中选取2两个数,使得lcm(ai,aj)最大。
题解:生成的前一百个数中进行暴力,得到的最大值一定是所有数的最大值。要注意数据类型的定义(unsigned long long)
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
ll a[10000004];
unsigned int n,A,B,C;
unsigned x,y,z;
unsigned tang()
{
unsigned int t;
x^=x<<16;
x^=x>>5;
x^=x<<1;
t=x;x=y;y=z;z=t^x^y;
return z;
}
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
bool cmp(ll x,ll y){
return x>y;
}
int main()
{
int t;
while(scanf("%d",&t)!=EOF)
{
int p=0;
while(t--){
p++;
scanf("%u%u%u%u",&n,&A,&B,&C);
x=A,y=B,z=C;
for(int i=0;i<n;i++)
a[i]=tang();
unsigned int k=min(100u,n);
nth_element(a,a+k,a+n,cmp);
ll ans=0;
for(int i=0;i<k;i++){
for(int j=i+1;j<k;j++)
ans=max(ans,a[i]*a[j]/gcd(a[i],a[j]));
}
printf("Case #%d: %llu\n",p,ans);
}
}
return 0;
}