链接: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个数,求这n个数任意两个数的最小公倍数的最大值。
解这题需要知道这个知识点:随机两个正整数互质的概率为 。证明看百度文库。
这题需要得出尽可能大的lcm(i,j),故我们只要在前100大的范围找lcm,因为(1- ) ^100(不出现互质的概率)几乎为0,故可以在前100大中暴力找出结果。
你以为这就可以了吗,还差一点呢,我们找前100大时,要是直接sort,T飞你,但这时我们可以用一个库函数 nth_element 完美解决了这个问题。这函数详解可以看下这里 https://blog.csdn.net/LJD201724114126/article/details/81479909
代码如下:第一篇是师兄写的
#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e7 + 7;
int n, m;
unsigned int x, y, z;
unsigned int 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;
}
unsigned long long ans[maxN];
int main(){
int T;
cin >> T;
for(int kase = 1; kase <= T; kase++){
cin >> n >> x >> y >> z;
for(int i = 0; i < n; i++){
ans[i] = (unsigned long long)tang();
}
m = min(n , 75);
nth_element(ans , ans + n - m, ans + n);
unsigned long long res = 0;
for(int i = n - m; i < n; i++){
for(int j = i + 1; j < n; j++){
res = max(res , ans[i] / __gcd(ans[i], ans[j]) * ans[j]);
}
}
cout << "Case #" << kase <<": " << res<<"\n";
}
}
这篇是我写的,在codeblocks运行,样例过不了,但提交上去就能A,不是是不是编译器的原因。
结果发现是输出的问题,应该%llu的,而我写成%lu
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int maxn=1e7+10;
unsigned num[maxn];
unsigned x,y,z;
int n,m;
unsigned gcd(unsigned a,unsigned b)
{
if(!b) return a;
else return gcd(b,a%b);
}
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 Case;
scanf("%d",&Case);
for(int T=1;T<=Case;T++)
{
scanf("%d%u%u%u",&n,&x,&y,&z);
for(int i=1;i<=n;i++)
{
num[i]=(ULL)tang();
}
m=max(1,n-100);
nth_element(num+1,num+m+1,num+1+n);
ULL sum=0;
// for(int i=1;i<=n;i++)
// printf("%u ",num[i]);
// puts("");
for(int i=m;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
sum=max(sum,num[i]/(ULL)gcd(num[i],num[j])*num[j]);
// sum=(ULL)num[i]*(ULL)num[j];
// printf("gcd=%u,sum=%lu\n",gcd(num[i],num[j]),sum);
}
printf("Case #%d: %llu\n",T,sum);
}
return 0;
}
我的标签:做个有情怀的程序员。