牛客网暑期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)7
For each test case, the first line contains four integers n, A, B, C. (2 ≤ n ≤ 10, 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.
思路:
调用题目的给的函数tang(), 生成数a[1]到a[n], 然后调用STL的库函数nth_element(原理是快排,也可以自己手搓) ,选出前100大的数,然后暴力选两个数,使得最小公倍数最大。
附:
关于STL中的nth_element()方法的使用:通过调用nth_element(start, start+n, end) 方法可以使第n大元素处于第n位置(从0开始,其位置是下标为 n的元素),并且比这个元素小的元素都排在这个元素之前,比这个元素大的元素都排在这个元素之后,但不能保证他们是有序的。
代码:
#include<bits/stdc++.h> using namespace std; const int maxn =1e7+20; unsigned x,y,z,A,B,C; typedef unsigned long long ull; unsigned a[maxn]; 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,n; cin>>t; for(int cas=1;cas<=t;cas++){ cin>>n>>A>>B>>C; x=A,y=B,z=C; for(int i=1;i<=n;i++) a[i]=tang(); vector<unsigned> vec; int num=min(n,100); nth_element(a+1,a+1+n-num,a+1+n); ///只要排一次 /// 选出前一百大的数 for(int k=1;k<=num;k++){ vec.push_back(a[n+1-k]); } ///暴力找lcm最大 ull ans=0; for(int i=0;i<vec.size();i++){ for(int j=i+1;j<vec.size();j++){ ull gcd = __gcd((ull)vec[i],(ull)vec[j]); ull tp=vec[i]/gcd*vec[j]; ans = max(ans,tp); } } printf("Case #%d: ",cas); cout<<ans<<endl; } return 0; }