Topic link: B. Power Sequence
Title
Give you a sequence a of length n, let you make it into a sequence called "power sequence" through some operations-there is a c such that (0≤i≤n-1), ai = ci {a_i=c ^i}ai=ci。
- You can change the position of each element in the sequence. This operation is not counted in the operand.
- You can choose any element to increase or decrease by 1. Each time this operation is performed, the operand is +1.
Ask the minimum number of operands required to make it "power squence".
answer
Let’s analyze this question first, ai ≤ 1 e 9, n ≤ 1 e 5 {a_i≤1e9, n≤1e5}ai≤1 e 9 ,n≤1 e 5 .
Then we can draw a conclusion that the operand cannot exceed1 e 14 {1e14}1 e 1 4 , because whenai is all 1 e 9 {a_i is all 1e9}aiAll are 1 e 9 , n is1 e 5 {1e5}At 1 e 5 , we can get the minimum value by taking c=1.
Then we continue to analyze
when c = 1 0 4 {10^4}104 o’clock, then the power sequence sequence is1, 1 0 4, 1 0 8, 1 0 12 {1, 10^4,10^8,10^{12}}1,104,108,10. 1 2
When = C. 1 0. 3 ^ {10}. 3103 o'clock, then the power sequence sequence is1, 1 0 3, 1 0 6, 1 0 9, 1 0 12 {1,10^3,10^6,10^{9},10^{12}}1,103,106,109,1012
It is easy to find that when c>=1000, n cannot exceed 4. If it exceeds 4, we can get the answer when we take c=1.
So for this question we can enumerate c. Since ai ≤ 1 e 9, n ≥ 3 {a_i≤1e9,n≥3}ai≤1e9,n≥3 , soc ≤ 1 e 9 {c≤\sqrt{1e9}}c≤1e9.
In calculating the ai {a_i} of the power sequenceai, If ai ≥ 1 e 14 {a_i≥1e14}ai≥1 e 1 4 , then exit the loop.
Due to pruning, the time complexity will not exceed.
Code
#include<iostream>
#include <sstream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
//typedef int fuck;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x
//#define int long long
const double PI=acos(-1.0);
const double eps=1e-6;
//const ll mod=1e9+7;
const ll inf=1e18;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll a[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%lld",&a[i]);
sort(a,a+n);
ll ans=inf;
for(ll c=1;c<=sqrt(1e9)+1;c++)
{
ll tmp=0,tt=1;
int flag=1;
for(int i=0;i<n;i++)
{
if(tt>=1e14) {
flag=0;break;}
tmp+=abs(a[i]-tt);
tt*=c;
}
if(flag) ans=min(ans,tmp);
}
printf("%lld\n",ans);
}