inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
constint MAXN = 1e5 + 10;
int n; int aa[MAXN];
intmain(){ n = read(); for (int i = 1; i <= n; ++i) { aa[i] = read(); } std::sort(aa + 1, aa + 1 + n); int upbound = powl(1e18, 1.0 / n); // 第 n 项至多是这些,再往上就无法承受了 longlongint ans = 0x7f7f7f7f7f7f7f7f; for (int c = 1; c <= upbound; ++c) { longlongint cost = 0, pow = 1; for (int i = 1; i <= n; ++i) { cost += std::abs(aa[i] - pow); pow *= c; } ans = std::min(ans, 1ll * cost); } printf("%lld\n", ans); return0; }
C. Multiples of Length
解题思路
又是™的构造题
当 n = 1 时:
1 2 3 4 5 6
1 1 0 1 1 0 1 1 -a[1]
当 n ≠ 1 时:
1 2 3 4 5 6
1 1 -a[1] 2 n (n-1)*a[2] (n-1)*a[3] ... (n-1) * a[n] 1 n 0 -n*a[2] -n*a[3] ... -n*a[n]
inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
constint MAXN = 100000 + 10;
longlongint n, aa[MAXN];
intmain(){ n = read(); for (int i = 1; i <= n; ++i) aa[i] = read(); if (n == 1) { printf("1 1\n0\n"); printf("1 1\n0\n"); printf("1 1\n%lld\n", -aa[1]); } else { printf("1 1\n%lld\n", -aa[1]); printf("2 %lld\n", n); for (int i = 2; i <= n; ++i) { printf("%lld%c", (n - 1) * aa[i], " \n"[i == n]); } printf("1 %lld\n0 ", n); for (int i = 2; i <= n; ++i) { printf("%lld%c", -n * aa[i], " \n"[i == n]); } } return0; }
inlineintread(){ int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
intmain(){ int T = read(); while (T --> 0) { int n = read(); int mxx = 0, sum = 0; for (int i = 1; i <= n; ++i) { int x = read(); mxx = std::max(mxx, x); sum += x; } printf("%s\n", ans[!((sum & 1) || (mxx > (sum >> 1)))]); } return0; }