constructive algorithms implementation math *2200

Please click on ads to support us..

Python Code:

def next_value(value):
    if (value < 9):
        return value
    
    base = 1
    while (base * 10 + 1 <= value):
        base = base * 10 + 1
    cur = 1
    while (base * (cur + 1) <= value and cur < 9):
        cur += 1
    return base * cur
 
def list_del(value):
    mp = {}
    while (value > 0):
        n_value = next_value(value)
        value -= n_value
        curval = mp.get(n_value) is None and 1 or mp.get(n_value) + 1
        mp[n_value] = curval
    return mp
 
x = input().split()
p = int(x[0])
q = int(x[1])
 
revAfter = p < q
 
beginStr = ''
if (abs(p - q) & 1):
    beginStr = '5000000000000000002'
else:
    beginStr = '6000000000000000002'
    
revStr = beginStr[::-1]
 
begin = int(beginStr)
rev = int(revStr)
 
razn = (abs(begin - rev) - abs(p - q)) >> 1
 
ans = str(begin)
 
for k, v in list_del(razn).items():
    ans += '+0-' + str(k) + '*' + str(v) + '+0'
 
for k, v in list_del(abs(begin - razn - max(p, q))).items():
    ans += '+0-' + str(k) + '*' + str(v) + '-0'
if (revAfter):
    ans = ans[::-1]
print(ans)


Comments

Submit
0 Comments
More Questions

1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft
1562A - The Miracle and the Sleeper
1216A - Prefixes
1490C - Sum of Cubes
868A - Bark to Unlock
873B - Balanced Substring
1401D - Maximum Distributed Tree
1716C - Robot in a Hallway
1688B - Patchouli's Magical Talisman
99A - Help Far Away Kingdom
622B - The Time
1688C - Manipulating History
1169D - Good Triple
1675B - Make It Increasing
588A - Duff and Meat