英国音乐博物馆中有n件乐器,贝多芬决定选出其中7种乐器,来应对一年一度的音波音乐会上的合奏。
每件乐器都表示为一个数字,贝多芬认为,对于选择的乐器组合,它们的总分数由以下规则计算得到:
1.基础分数为组合中不同数字的和。如果有相同的数字,那么只计算一次。
2.如果组合中所有数字的和为质数,那么将基础分数乘1.2
3.假设组合中所有数字的乘积对7取余后为x,那么将基础分数再乘1+0.1x(比如x为6,就要乘1.6)
4.如果组合中所有数字在重新排列后能够组成等差序列,那么将基础分数再乘1.5
求所有乐器组合中,可能出现的最大分数。
第一行输入一个整数T,表示数据组数。
接下来每组数据输入两行:
第一行输入一个整数n,表示乐器数量。
第二行输入n个整数a1 ~ an,表示每种乐器的数字表示。
每组数据输出一行一个浮点数,表示最大分数。保留两位小数
1
10
1 3 5 7 9 2 4 6 8 10
82.56
对于 100% 的数据:1≤n≤40,1≤ai≤106,1≤T≤10
选1,4,5,6,8,9,10七个数,分数为43×1.2×1.6=82.56
5个WA+五个RTE
#include <bits/stdc++.h>
using namespace std;
const int N = 280;
vector<bool> prime(N+1, true);
void init() {
prime[0] = prime[1] = false;
for (int i = 2; i*i <= N; i++) {
if (prime[i]) {
for (int j = i * i; j <= N; j += i) {
prime[j] = false;
}
}
}
}
double mx = 0;
void cal(vector<int>& n, vector<int>& cbt, int start, int depth, int sum, int pro) {
if (depth == 7) {
bool flagP = prime[sum];
bool flagA = ((cbt.back() - cbt.front()) % 6 == 0);
double score = sum;
if (flagP) {
score *= 1.2;
}
int x = pro % 7;
score *= (1 + 0.1 * x);
if (flagA) {
score *= 1.5;
}
mx = max(mx, score);
return;
}
int len = n.size();
for (int i = start; i <= len - (7 - depth); i++) {
cbt[depth] = n[i];
cal(n, cbt, i+1, depth+1, sum+n[i], pro*n[i]);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
init();
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a.begin(), a.end());
mx = 0;
vector<int> cbt(7);
cal(a, cbt, 0, 0, 0, 1);
printf("%.2f\n", mx);
}
return 0;
}
求大佬发布代码!必关!