Stalin69's blog

By Stalin69, history, 3 hours ago, In English

I've been solving this problem name 225B - Well-known Numbers, I coded and submit it but got wrong answer on test case 33, then I went to see that test case and run locally in sublime, but I've got the output correct and different from the output that I was getting after submission.

How is this possible and why ??? I've got the correct answer and after submission it shows different and may be that's why I'm getting wrong output.

And the test case is : 989464701 4

Can somebody check this out ?

This is my code :

// x.cpp

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

#define MOD 1000000007
#define MOD1 998244353
#define INF 1e17
#define endl "\n"
#define nline cout << endl
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define pll pair<ll, ll>
#define PI 3.141592653589793238462
#define set_bits(x) __builtin_popcountll(x)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define pbds tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag ,  tree_order_statistics_node_i - 1date>

// -------------------------------------------------------------------------------------------------------//

template <typename T> void read(T& t) { cin >> t; }
template <typename T, typename... Args> void read(T& t, Args&... args) { read(t); read(args...); }
template <typename T> void read(vector<T>& vec) { for (auto& element : vec) { cin >> element; } }
template <typename T> void print(const T& t) { cout << t << " "; }
template <typename T, typename... Args> void print(const T& t, const Args&... args) { print(t); print(args...); }
template <typename T> void print(const vector<T>& vec) { for (const auto& element : vec) { cout << element << " "; } cout << "\n"; }

// -------------------------------------------------------------------------------------------------------//

using ll = long long;
using ull = unsigned long long;
using lld = long double;

void solve() {
	ll s, k;
	read(s, k);
	vector<ll> ans, kbonacci;

	if (k > 32) {
		ll val = 1;
		kbonacci.pb(0);
		while (val <= s) {
			kbonacci.pb(val);
			val *= 2;
		}
	}
	else {
		vector<ll> b(k, 0);
		b[k - 1] = 1;
		kbonacci.pb(0);
		kbonacci.pb(1);

		while (1) {
			ll next = 0;
			for (ll i = b.size() - k; i < sz(b); i++) {
				next += b[i];
			}
			if (next > s) break;
			kbonacci.pb(next);
			b.pb(next);
		}
	}

	// print(kbonacci);
	set<ll> st;
	for (ll i = sz(kbonacci); i >= 0 && s > 0; i--) {
		if (kbonacci[i] <= s and st.find(kbonacci[i]) == st.end()) {
			s -= kbonacci[i];
			ans.pb(kbonacci[i]);
			st.insert(kbonacci[i]);
		}
	}

	if (sz(ans) == 1)
		ans.pb(0);
	print(sz(ans)); nline;
	print(ans);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll tc;
	tc = 1;
	// read(tc);
	for (ll i = 1; i <= tc; i++) {
		solve();
	}

	return 0;
}
  • Vote: I like it
  • -3
  • Vote: I do not like it

»
2 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

You should run the loop from size()-1 to avoid out of bound

  • »
    »
    2 hours ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Oops, got it AC.