http://codeforces.com/contest/1183/problem/F
题意:
给出n个数,选择最多三个数,要求相互之间不是因数,并且和最大,输出这个和
思路:
首先想到贪心,直接sort,先选最大的,然后剩下选符合条件并且最大的。然后看到第二个样例30 15 10 6 不符合贪心。
然后想着直接打表观察一下符合三个因数和大于本身的数的规律。结果我傻逼的忘记判断条件因子是否符合要求,就没看出来
正确打表
bool check(int x,int y)
{
if(x%y==0) return 0;
return 1;
}
bool check2(int x,int y,int z)
{
if(x%z==0||y%z==0) return 0;
return 1;
}
int main()
{
ios::sync_with_stdio ( false );
cin.tie ( 0 ); cout.tie(0);
for(int i=1;i<=500;i++)
{
ans=0;
cnt=0;
for(int j=i-1;j>=1;j--)
{
if(i%j==0)
{
if(cnt==1)
{
if(!check(a[1],j)) continue;
}
else if(cnt==2)
{
if(!check2(a[1],a[2],j)) continue;
}
ans+=j;
a[++cnt]=j;
}
if(cnt==3) break;//
}
if(ans>i) cout<<"i "<<i<<" "<<ans<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl;
//cout<<endl;
}
}
正确结果
i 30 31 15 10 6
i 60 62 30 20 12
i 90 93 45 30 18
i 120 124 60 40 24
i 150 155 75 50 30
i 180 186 90 60 36
i 210 217 105 70 42
i 240 248 120 80 48
i 270 279 135 90 54
i 300 310 150 100 60
i 330 341 165 110 66
i 360 372 180 120 72
i 390 403 195 130 78
i 420 434 210 140 84
i 450 465 225 150 90
i 480 496 240 160 96
这样就可以很清晰的发现当x是30的倍数,即使2,3,5的倍数时,并且x/2 x/3 x/5因子存在时,三个因子会大于本身。
1
/
2
+
1
/
3
+
1
/
5
>
1
1/2+1/3+1/5>1
1/2+1/3+1/5>1
思考一下也是只会有这种因子的组合,其三个因子和才会大于本身。所以如果存在这种情况,和上述贪心得到的结果取max即可得到答案
#include<bits/stdc++.h>
#define fi first
#define se second
#define log2(a) log(n)/log(2)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
#define tim printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, ll> LP;
const ll inf = 1e18;
const int N = 1e6 + 10;
const ll mod = 1e9 + 7;
const int base = 131;
const double pi = acos ( -1 );
const double eps = 1e-8;
inline ll ksm(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
ll vis[N], num[N], n, m, k, x, y, z;
ll a[N], b[N];
ll cx, cy, cnt, ans, sum, flag, t, ff,q;
//vector<int> v[N];
map<P, int> mp;
set<string> st;
vector<P> p;
char s[N];
ll dp[105][105];
bool check(int x,int y)
{
if(x%y==0) return 0;
return 1;
}
bool check2(int x,int y,int z)
{
if(x%z==0||y%z==0) return 0;
return 1;
}
int main()
{
ios::sync_with_stdio ( false );
cin.tie ( 0 ); cout.tie(0);
cin>>q;
while(q--)
{
set<int> st;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],st.insert(a[i]);
sort(a+1,a+n+1,greater<int>());
ans=a[1];
cnt=1;
x=a[1];
for(int i=2;i<=n;i++)
{
if(cnt==1&&check(x,a[i]))
{
cnt++;
y=a[i];
ans+=a[i];
}
if(cnt==2&&check2(x,y,a[i]))
{
cnt++;
y=a[i];
ans+=a[i];
}
if(cnt==3) break;
}
x=*st.rbegin();
if(x%2==0&&x%3==0&&x%5==0)
{
if(st.count(x/2)&&st.count(x/3)&&st.count(x/5))
{
ans=max(ans,x/2+x/3+x/5);
}
}
cout<<ans<<endl;
}
}