链接: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.
输入
2
2 1 2 3
5 3 4 8
输出
Case #1: 68516050958
Case #2: 5751374352923604426
题意:给出初始的A,B,C调用tang()函数n次求n个z,求这n个数中的两个数lcm的最大值。
思路:比赛的时候一直以为是有什么奇怪的结论可以化简,打算打表找规律,后来最近被随机数题坑傻了,但是也不知道怎么写,官方给的思路就是保留最大的100项,然后暴力求就能过。学到了新的,很好用但是之前没怎么用过的函数nth_element(start, start+n, end)百度百科链接,总体来说,这个题,毒性不小...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef unsigned long long ULL;
const int maxn=1e9+7;
unsigned x,y,z;
unsigned data[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;
}
unsigned gcd(unsigned a, unsigned b)
{
if(b == 0) return a;
return gcd(b,a%b);
}
int main()
{
int t;
int n;
scanf("%d",&t);
for (int cases = 1; cases <= t; ++cases)
{
scanf("%d",&n);
scanf("%u %u %u",&x,&y,&z);
for (int i = 1; i <= n; ++i) data[i]=tang();
int m=max(0,n-100);
nth_element(data+1,data+m+1,data+n+1);
unsigned long long ans=0;
for (int i = m; i <= n ; ++i)
{
for (int j = i+1; j <= n ; ++j)
{
ans=max(ans,1ULL*data[i]/gcd(data[i],data[j])*data[j]);
}
}
printf("Case #%d: %llu\n",cases,ans);
}
return 0;
}