Answer the question
In order to leave comments, you need to log in
What's wrong with using long arithmetic?
Hello, I solved the problem (the condition under the spoiler) and, it seems, I solved it correctly, but it falls on the 10th test. As far as I understand, the problem is in my incorrect implementation of long arithmetic. I would be grateful for help.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
const ull BASE = 1e9;
const ull SIZE = 15;
vector<ull> formBI(ull num) {
vector<ull> res(SIZE, 0);
short i = 0;
while (num) {
res[i++] = num % BASE;
num /= BASE;
}
return res;
}
vector<ull> sum(vector<ull> n1, vector<ull> n2) {
for (size_t i=0; i<SIZE; i++) {
n1[i] += n2[i];
}
for (size_t i=0; i<SIZE-1; i++) {
if (n1[i]>=BASE) {
n1[i] -= BASE;
n1[i+1]++;
}
}
return n1;
}
void print(vector<ull> n) {
reverse(n.begin(), n.end());
for(size_t i=0; i<SIZE; i++) {
if (n[i]!=0) {
cout << n[i];
}
}
cout << endl;
}
int main() {
short k, n;
cin >> k >> n;
vector<ull> emp(SIZE, 0);
vector<vector<ull>> b(n+1, emp);
b[0] = formBI(1);
for (size_t i=1; i<n+1; i++) {
short r = (i<k) ? i : k;
for (size_t j=1; j<=r; j++) {
b[i] = sum(b[i], b[i-j]);
}
}
print(b[n]);
return 0;
}
Answer the question
In order to leave comments, you need to log in
The problem is in the output. You do not display zero digits at all:
for(size_t i=0; i<SIZE; i++) {
if (n[i]!=0) {
cout << n[i];
}
}
bool printed
:bool printed = false;
for(size_t i=0; i<SIZE; i++) {
if (printed || n[i]!=0) {
cout << n[i];
printed = true;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question