1397B - Power Sequence - CodeForces Solution


brute force math number theory sortings *1500

Please click on ads to support us..

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_map>
#define endl '\n'
using namespace std;
#define int long long
typedef pair<int, int>pii;
const int mod = 998244353;
const int N = 1e5 + 10;
int ans = 1e18;
int a[N];
signed main()
{
    ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);

    int sum = 0;
    int n; cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    sort(a + 1, a + 1 + n);
    int sum0 = 0;
    for (int i = 1; i <= n; i++)
    {
        sum0 += abs(a[i] - 1);
    }
    ans = sum0;
    for (int i = 2; ; i++)
    {
        int sum1 = 0, p = 1;
        for (int j = 1; j <= n; j++)
        {
            sum1 += abs(a[j] - p);
            p *= i;
            if (sum1 > 1e15)break;
        }
        if (sum1 > 1e15)break;
        if (ans < sum1)
        {
            break;
        }
        else ans = sum1;
    }
    cout << ans << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1523B - Lord of the Values
1406C - Link Cut Centroids
2409. Count Days Spent Together
2410. Maximum Matching of Players With Trainers
1604C - Di-visible Confusion
997A - Convert to Ones
218A - Mountain Scenery
486B - OR in Matrix
1405A - Permutation Forgery
1733A - Consecutive Sum
1733B - Rule of League
1733C - Parity Shuffle Sorting
1264A - Beautiful Regional Contest
1695A - Subrectangle Guess
467B - Fedor and New Game
252C - Points on Line
735C - Tennis Championship
992A - Nastya and an Array
554A - Kyoya and Photobooks
79B - Colorful Field
265B - Roadside Trees (Simplified Edition)
1362C - Johnny and Another Rating Drop
1214C - Bad Sequence
1091B - New Year and the Treasure Geolocation
244A - Dividing Orange
1061C - Multiplicity
1312A - Two Regular Polygons
801A - Vicious Keyboard
510B - Fox And Two Dots
616D - Longest k-Good Segment