986D - Perfect Encoding - CodeForces Solution


fft math *3100

Please click on ads to support us..

Python Code:

import decimal
from math import ceil, floor, inf, log

n = input()
if n=='1':
    print(1)
    exit()
decimal.getcontext().prec = len(n)+6
decimal.getcontext().Emax = len(n)+6
log3 = log(10)*(len(n)-1)/log(3)
pref = n
if len(pref)>20:
    pref = pref[:20]
pref = pref[0] + '.' + pref[1:]
log3 += log(float(pref))/log(3)
log3+=1e-8
full = max(0, floor(log3))
small=0

if full>=1 and log3-full<=log(2)/log(3)*2-1:
    small=2
    full-=1
elif log3-full<=log(2)/log(3):
    small = 1

else:
    full+=1


n = decimal.Decimal(n)
ans = full*3 + small*2

def check(f,s):
    global ans
    res = decimal.Decimal(3)**f * (2**s)
    if res>=n:
        ans = min(ans,f*3+s*2)

if small==0 and full>=1:
    full-=1
    small+=1
    check(full,small)
elif small==1 and full>=1:
    full-=1
    small+=1
    check(full,small)
elif small==2:
    small-=2
    full+=1
    check(full,small)

print(ans)

C++ Code:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;

#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)

const int N = 1500005, oo = 0x3f3f3f3f;
const double eps = 1e-8;
int pp[] = {0, 100030001, 100060001, 113111311, 114535411}, delta = 1000000;
char st[N]; 
int n, ans = oo;

int pow(int x, int k, int p) {
    int res = 1;
    for ( ; k; k >>= 1, x = (ll)x * x % p) {
        if (k & 1) {
            res = (ll)res * x % p;
        }
    }
    return res;
}

struct BigInt {

    vi v;

    BigInt(char st[], int n) {
        v.resize(n);
        rep(i, 0, n-1) {
            v[i] = st[n-1-i] - '0';
        }
    }
    
    inline void detrail() {
        while (!v.empty() && !v.back()) v.pop_back();
    }

    inline void comein() {
        v.push_back(0);
        for (int i = 0; i + 1 < (int)v.size(); ++i) {
            v[i+1] += v[i] / 10;
            v[i] %= 10;
        }
        detrail();
    }

    inline void add_one() { 
        v[0]++;
        comein(); 
    }

    inline int hash(int p) {
        ll x = 0; 
        per(i, (int)v.size() - 1, 0) {
            x = x * 10 + v[i];
            x %= p;
        } 
        return x;
    }

    inline void div_2() {
        if (v[0] & 1) add_one();
        v[0] >>= 1;
        for (int i = 0; i + 1 < (int)v.size(); ++i) {
            v[i] += (v[i+1] & 1) * 5;
            v[i+1] >>= 1;
        }
        detrail();
    }

    inline int log_3() {
        int l = min(15, (int)v.size()); 
        ll x = 0;
        per(i, (int)v.size() - 1, (int)v.size() - l) {
            x = x * 10 + v[i];
        }
        double w = (log(x) + ((int)v.size() - l) * log(10)) / log(3);
        int t = w, cnt = 0; 
        t += (t + 1 - w < eps);
        rep(i, 1, 4) {
            int xx = pow(3, t, pp[i]), yy = hash(pp[i]);
            cnt += (xx - yy > 0 && xx - yy <= delta);
        }
        if (cnt == 4) --t;
        rep(i, 1, 4) {
            if (pow(3, t, pp[i]) != hash(pp[i])) {
                return t + 1;
            }
        }
        return t;
    }
};

int main() {
    scanf("%s", st);
    n = strlen(st);
    if (n == 1 && st[0] == '1') {
        printf("1\n");
        return 0;
    }
    BigInt x = BigInt(st, n);
    ans = x.log_3() * 3;
    rep(i, 1, 2) {
        x.div_2();
        ans = min(ans, x.log_3() * 3 + i * 2);
    }
    printf("%d\n",ans);

    return 0;
}/*1695734992.273435*/


Comments

Submit
0 Comments
More Questions

165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts