做题记录 P6786

· · 个人记录

好题!!!

首先,题目给了一个式子,首先想到通过这个式子发现一些性质

先做一个变形,令 x = b [i],y = b [j],可以想到,lcm (x,y)=x * y / gcd (x,y)

式子变成 x + y + gcd (x,y)=x * y / gcd (x,y)

还是没发现什么性质,于是尝试把左边的式子变形

令 t1 = x/gcd (x,y),t2 = y/gcd (x,y) 显然有 gcd (t1,t2)=1;

gcd(x,y)(t1+t2+1)=xy/gcd(x,y)

然后发现,右边式子其实可以变成 t1t2gcd(x,y)

哎,现在就很棒了,得到了

t1+t2+1=t1*t2

变形,发现 t2=(t1 + 1)/(t1-1)

然后,t1,t2 还是一个正整数,还有 gcd (t1,t2)=1,t1 < t2

发现这个问题等价于:

求出所有满足 gcd (x,x + 2) 不等于 1 的 x

得到 t1 = 2,t2 = 3

完美收官!!!

现在,把这个东西回代

2*gcd(x,y)=x

3*gcd(x,y)=y

发现,x = 2 / 3 * y!!

也就是,把问题转化为:

存在位置 j 使得 bj > bi 且 bi = 2 / 3 * bj

那么,这道题就变得很简单了

不过,实现起来还是有些小细节

代码如下

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n;
const int N=3e5+10;
map<int,int> mp;
int a[N];
ll ans=0;

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        mp[a[i]]++;
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        if(!mp[a[i]]) continue;
        if(a[i]%2)
        {
            ans=max(ans,(ll)mp[a[i]]*a[i]);
            mp[a[i]]=0;
            continue;
        }
        ll tmp=a[i];
        ll cnt=mp[tmp]*tmp;
        while(mp[tmp])
        {
            mp[tmp]=0;
            tmp/=2;
            tmp*=3;
            cnt+=mp[tmp]*tmp;
            if(tmp%2) break;
        }
        ans=max(ans,cnt);
    }
    cout<<ans;
    return 0;
}