Pagini recente » Cod sursa (job #1177147) | Cod sursa (job #50591) | ONIS 2015, Solutii Runda 1 | Cod sursa (job #574678) | Cod sursa (job #1696305)
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
ifstream fin("bile2.in");
ofstream fout("bile2.out");
#define A (*this)
class Huge : protected vector < int > {
protected:
static const int base = 10, nBase = 1;
public:
Huge() {
this->resize(1000);
}
void operator = (int x) {
for (A[0] = 0; x; x /= base)
A[++A[0]] = x % base;
}
void operator = (char *s) {
A[0] = 0;
for (int i = strlen(s); i > 0; i -= nBase) {
++A[0];
for (int j = max(0, i - nBase); j < i; ++j)
A[A[0]] = A[A[0]] * 10 + s[j] - '0';
}
}
void print(void) {
if (A[0] == 0) {
fout << 0;
return;
}
fout << A[A[0]];
for (int i = A[0] - 1; i > 0; --i) {
int p = base / 10;
while (p > A[i] && p > 1) {
fout << 0;
p /= 10;
}
fout << A[i];
}
}
bool operator < (const Huge &B) {
if (A[0] != B[0])
return A[0] < B[0];
for (int i = A[0]; i; --i) {
if (A[i] < B[i]) return true;
if (B[i] < A[i]) return false;
}
return true;
}
Huge operator + (const Huge &B) {
int i, t = 0;
Huge C;
for (i = 1; i <= A[0] || i <= B[0] || t; ++i, t /= base) {
t += (i <= A[0] ? A[i] : 0);
t += (i <= B[0] ? B[i] : 0);
C[i] = t % base;
}
C[0] = i - 1;
return C;
}
Huge operator - (const Huge &B) {
Huge C = A;
int i, t = 0;
for (i = 1; i <= A[0]; ++i) {
C[i] -= (i <= B[0] ? B[i] : 0) + t;
t = 0;
if (C[i] < 0) C[i] += base, t = 1;
}
while (C[0] > 1 && C[C[0]] == 0)
--C[0];
return C;
}
Huge operator * (int x) {
Huge C = A;
int t = 0;
for (int i = 1; i <= C[0]; ++i) {
C[i] = C[i] * x + t;
t = C[i] / base;
C[i] %= base;
}
while (t) {
C[++C[0]] = t % base;
t /= base;
}
return C;
}
Huge operator * (const Huge &B) {
Huge C;
for (int i = 1; i <= A[0]; ++i) {
long long t = 0; int j;
for (j = 1; j <= B[0] || t; ++j, t /= base) {
t += C[i + j - 1] + (j <= B[0] ? 1LL * A[i] * B[j] : 0);
C[i + j - 1] = t % base;
}
if (i + j - 2 > C[0])
C[0] = i + j - 2;
}
return C;
}
Huge operator / (int x) {
Huge C;
long long R = 0;
for (int i = A[0]; i; --i) {
R = R * base + A[i];
C[i] = R / x;
R %= x;
}
while (C[0] > 1 && C[C[0]] == 0)
--C[0];
return C;
}
int operator % (int x) {
long long R = 0;
for (int i = A[0]; i; --i) {
R = R * base + A[i];
R %= x;
}
return (int)R;
}
};
const int dim = 1005;
int n, dst;
Huge a, b, c, d, aux1, aux2;
char s[dim];
Huge dp[2][dim]; //dp[i][j] := i selected, last one is at most j
Huge comb[2][dim];
int main() {
fin >> n >> dst;
fin >> s;
a = s;
fin >> s;
b = s;
int OLD = 0, NEW = 1;
comb[OLD][0] = 1, comb[OLD][1] = 1;
for (int i = 2; i <= n; ++i) {
comb[NEW][0] = 1;
for (int j = 1; j <= i; ++j)
comb[NEW][j] = comb[OLD][j - 1] + comb[OLD][j];
swap(OLD, NEW);
}
int nn = OLD;
OLD = 0, NEW = 1;
for (int i = 1; i <= n; ++i)
dp[OLD][i] = i;
for (int i = 2; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (j > dst + 1)
dp[NEW][j] = dp[OLD][j - dst - 1];
else
dp[NEW][j] = 0;
dp[NEW][j] = dp[NEW][j] + dp[NEW][j - 1];
}
c = comb[nn][i] - dp[NEW][n];
d = comb[nn][i];
aux1 = a * d;
aux2 = b * c;
if (aux1 < aux2) {
Huge sol;
sol = i;
sol.print();
fout << '\n';
return 0;
}
swap(NEW, OLD);
}
return 0;
}
//Trust me, I'm the Doctor!