# 14257: 【原4257】复仇

### 题目描述

author: Guo Linsong 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4257

## Description

(1) 仇恨值减1，助教组的不满值加A.

(2) 仇恨值除以k，助教租的不满值加B.

## Input Format

$$1 \leq n,k,A,B \leq 2\times 10^9$$.

19 3 4 2

12

19 19 19 1

1

## ligongzzz's solution

#include <iostream>
using namespace std;

int main() {
uint64_t n, k, A, B, ans = 0;
cin >> n >> k >> A >> B;

for (; n >= k && (n / k * k - n / k) * A >= B; n /= k) {
ans += B + (n - n / k * k) * A;
}
ans += (n - 1ull) * A;
cout << ans;

return 0;
}


## zqy2018's solution

#include <bits/stdc++.h>
#define INF 2000000000
using namespace std;
typedef long long ll;
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar();}
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int n, k, A, B;
void init(){
}
void solve(){
if (k == 1){
printf("%lld\n", 1ll * (n - 1) * A);
return ;
}
ll ans = 0;
while (n > 1){
if (n < k) {
ans += 1ll * (n - 1) * A;
break;
}
if (n % k == 0){
int to = (n / k);
if (1ll * (n - to) * A < B)
ans += 1ll * (n - to) * A;
else
ans += B;
n = to;
}else {
int to = (n / k) * k;
ans += 1ll * (n - to) * A;
n = to;
}
}
printf("%lld\n", ans);
}
int main(){
init();
solve();
return 0;
}