链接:https://www.nowcoder.com/acm/contest/144/J
来源:牛客网
Heritage of skywalkert
时间限制: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
分析:由于数据看上去像是随机生成的,只需取出前20(或100)大暴力即可。随机的两个正整数互质的概率为6/pi^2(百分之60左右)。
直接这样写会TLE
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
unsigned a[10000005];
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;
}
ll gcd(ll a,ll b)
{
while(b)
{
ll c=a%b;
a=b;
b=c;
}
return a;
}
ll get_lcm(ll x,ll y)
{
ll d = gcd(x, y);
return x / d * y;
}
int main()
{
int t,i,j;
ll ans;
scanf("%d", &t);
for (int cas = 1; cas <= t; cas++)
{
int n;
cin >> n >> x >> y >> z;
for(i=1;i<=n;i++)
{
a[i]=tang();
}
sort(a+1,a+1+n);
ans=0;
for(i=1;i<=20&&i<=n;i++)
{
for(j=i+1;j<=20&&j<=n;j++)
{
ans=max(ans,get_lcm(a[i],a[j]));
}
}
cout << "Case #" << cas << ": " << ans << endl;
}
}
如何优化?
由于我们只用到了最大的20项,就从这20项下手。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ull unsigned long long
ull gcd(ull a,ull b){
return b?gcd(b,a%b):a;
}
ull get_lcm(ull a,ull b)
{
return a/gcd(a,b)*b;
}
unsigned a[30];
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;
}
/*ll gcd(ll a,ll b)
{
while(b)
{
ll c=a%b;
a=b;
b=c;
}
return a;
}
ll get_lcm(ll x,ll y)
{
ll d = gcd(x, y);
return x / d * y;
}*/
int main()
{
int t,i,j,k,pos,temp;
ull ans;
scanf("%d", &t);
for (int cas = 1; cas <= t; cas++)
{
int n;
cin >> n >> x >> y >> z;
pos=1;
for(i=1;i<=n;i++)
{
if(pos<=20)
{
a[pos]=tang();
pos++;
sort(a+1,a+pos);
}
else
{
temp=tang();
j=1;
while(a[j]<temp&&j<pos) j++;
for(k=1;k<j-1;k++)
{
a[k]=a[k+1];
}
if(j!=1)
a[j-1]=temp;
}
}
ans=0;
for(i=1;i<pos&&i<=n;i++)
{
for(j=i+1;j<pos&&j<=n;j++)
{
ans=max(ans,get_lcm(a[i],a[j]));
}
}
cout << "Case #" << cas << ": " << ans << endl;
}
}
这个题姿势不好还真不好过,找错找了一上午,最后把注释掉的换掉就过了。