#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main() { int i, j, k; long n, d; cin >> n >> d; long jyo = 0, iti = 0, tugi = 0, sum = 0, cnt = 0; vectoramari; amari.push_back(1); cnt++; if (n - 1 == d) { cout << n - 1 << endl; getchar(); getchar(); return 0; } if (n == d) { cout << 0 << endl; getchar(); getchar(); return 0; } if (n%d == 0) { cout << n / d - 1 << endl; getchar(); getchar(); return 0; } else { cout << n / (n + 1 - (((n - 1) / d)*d + 1)) - 1 << endl; getchar(); getchar(); return 0; } while (true) { jyo = (n - amari[cnt-1]) / d; sum += jyo; //amari.push_back() iti = jyo * d + amari[cnt -1]; tugi = d - (n - iti); bool flag = false; for (i = 0; i < cnt; i++) { if (amari[i] == tugi) { flag = true; break; } } if (flag == false) { amari.push_back(tugi); cnt++; sum++; } else { break; } } cout << sum << endl; getchar(); getchar(); return 0; }