传送门:https://codeforces.com/contest/1397/problem/B
这是一道神奇的题,问你对数量中某个元素减去多少次并且重新排列之后可以构成等比数列指数递增是很快的,所以直接暴力枚举然后判断是否超出范围即可。
题目:
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
typedef long long int LL ;
LL a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n ;
cin >> n ;
for(int i = 1; i <= n ; i ++)
{
cin >> a[i];
}
LL ans = 1e15;
sort(a+1,a+1+n);
for(int j = 1; j <= 100000 ; j ++)
{
LL sum = 0 ;
bool f = true;
LL now = 1 ;
for(int i = 1; i <= n ; i ++)
{
sum += abs(now - a[i]);
now *= j ;
if(now > 2e14 || sum > 1e14 || now < 0 || sum < 0)
{
f = false;
break;
}
}
// cout << j <<' '<<sum<<endl;
if(f)ans = min(sum,ans);
}
cout << ans <<endl;
return 0 ;
}