牛客网暑期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.

输入

2
2 1 2 3
5 3 4 8

输出

Case #1: 68516050958
Case #2: 5751374352923604426

题意:n个数字,给出x,y,z,按照给出的程序返回第i个值为a[i],问这n个数字任意两个数字的lcm的最大值

思路:题解很玄学,最大的一百个数字,暴力两两求lcm,取最大即可。至于为什么?没有为什么!

注意给出的程序用的是unsigned int,不要随便改!!因为在左移右移的时候会去掉抄出范围的部分,改了类型,数字长度范围改变了,得到的数字是不对的,并且,ans定义不能为long long,要用unsigned long long,因为求lcm的数字是unsigned int型的,比int大,所以long long 会爆掉。

这个代码中用到了函数nth_element(a,a+b,a+n),a是数组的头地址,c+n是数组的尾地址,这个函数会使得数组b位置前小于a[b]这个值,而后面的大于这个值,但是前后数字无序,复杂度为O(n)

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
typedef unsigned long long ll;

unsigned x,y,z;
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 a[10000007];
int main(){
	int t;
	scanf("%d",&t);
	int c=1;
	while(t--){
		int n;
		cin>>n>>x>>y>>z;
		for(int i=1;i<=n;i++){
			a[i]=tang();
		}
		int len=max(1,n-100);
		nth_element(a+1,a+1+len,a+1+n);
		ll ans=0;
		for(int i=len;i<=n;i++){
			for(int j=len;j<=n;j++){
				if(i==j)continue;
				ans=max(ans,(ll)a[i]/__gcd(a[i],a[j])*a[j]);
			}
		}
		cout<<"Case #"<<c++<<": "<<ans<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yz467796454/article/details/81415463
今日推荐