Problem:
Score : 500 points

Problem Statement

Let S(n) denote the sum of the digits in the decimal notation of n.
For example, S(123) = 1 + 2 + 3 = 6.

We will call an integer n a Snuke number when, for all positive integers m such that m > n, \frac{n}{S(n)} \leq \frac{m}{S(m)} holds.

Given an integer K, list the K smallest Snuke numbers.

Constraints

1 \leq K

The K-th smallest Snuke number is not greater than 10^{15}.

Input

Input is given from Standard Input in the following format:

K

Output

Print K lines. The i-th line should contain the i-th smallest Snuke number.

Sample Input 1

10

Sample Output 1

1
2
3
4
5
6
7
8
9
19

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <vector>
#include <set>
#include <regex>
#include <math.h>

using namespace std;

#define FOR(i, a, b) for (auto i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define DEBUG(_debuged) cerr << #_debuged << ":" << _debuged << endl;

struct L
{
	int t;
	int x;
	int y;
};

vector<int> loadn(int n)
{
	vector<int> v(n);
	REP(i, n)
		cin >> v[i];
	return v;
}

uint64_t modpower(const unsigned int N, const unsigned int K, const unsigned int MOD)
{
	uint64_t ret = 1;
	uint64_t k2 = N;
	REP(i, 31)
	{
		if ((K >> i) & 0x1)
		{
			ret *= k2;
			ret = ret % MOD;
		}
		k2 = (k2 * k2) % MOD;
	}
	return ret;
}

class ModCombinationMemo
{
public:
	const unsigned int N;
	const unsigned int MOD;
	vector<uint64_t> memo = { 1 };

	ModCombinationMemo(uint32_t N, uint32_t MOD)
		: N(N), MOD(MOD)
	{
	}

	uint64_t c(uint64_t k)
	{
		if (memo.size() > k)
		{
			return memo[k];
		}

		FOR(i, memo.size(), k + 1)
		{
			const auto number = (memo[i - 1] * (N - i + 1)) % MOD;
			const auto reverceDeon = modpower(i, MOD - 2, MOD);
			memo.push_back(number * reverceDeon % MOD);
		}

		return memo[k];
	}
};

uint64_t c(const unsigned int N, const unsigned int K, const unsigned int MOD)
{
	if (K == 0)
	{
		return 1;
	}

	uint64_t n = 1, k = 1;

	REP(i, K)
	{
		n *= N - i;
		k *= i + 1;

		n = n % MOD;
		k = k % MOD;
	}

	uint64_t powerk = modpower(k, MOD - 2, MOD);

	return (n * powerk) % MOD;
}

unsigned trailing_zeros(unsigned n)
{
	if (!n)
		return -1;
	unsigned int c = 32; // c will be the number of zero bits on the right
	n &= -(signed)n;
	if (n)
		c--;
	if (n & 0x0000FFFF)
		c -= 16;
	if (n & 0x00FF00FF)
		c -= 8;
	if (n & 0x0F0F0F0F)
		c -= 4;
	if (n & 0x33333333)
		c -= 2;
	if (n & 0x55555555)
		c -= 1;
	return c;
}

unsigned int digitssum(uint64_t source) {
	unsigned int result = 0;

	while (source > 0) {
		result += source % 10;
		source /= 10;
	}

	return result;
}

unsigned int digitssum(string source) {
	unsigned int result = 0;

	REP (i, source.length()){
		result += source[i] - '0';
	}

	return result;
}

struct oishisa
{
	int64_t x;
	int64_t y;
	int64_t z;
};

int64_t cint()
{
	int64_t i;
	cin >> i;
	return i;
}

int64_t lastDigit(uint64_t n) {
	if (n == 0) {
		return 0;
	}
	while (n % 10 == 0) {
		n /= 10;
	}
	return n % 10;
}

int64_t lastPos(uint64_t n) {
	uint64_t i=0;
	while (n % 10 == 0) {
		i++;
		n /= 10;
	}
	return i;
}

int64_t pow64(const uint64_t base, int exp) {
	uint64_t ret = 1;
	for (; exp > 0; --exp) {
		ret *= base;
	}
	return ret;
}

int64_t nextSameSum(int digitSum, uint64_t startExclusive) {
	uint64_t start = startExclusive + 1;
	while (digitssum(start) > digitSum) {
		start += (10 - lastDigit(start)) * pow64(10, lastPos(start));
	}
	for (int i = 0; digitssum(start) < digitSum && i < 16; ++i) {
		start += std::min<long>(9U - (start / pow64(10, i) % 10), digitSum - digitssum(start)) * pow64(10, i);
	}
	return start;
}

int main(int argc, char *argv[], char *envp[])
{
	const int64_t K(cint());

	vector<double> v;

	uint64_t base = 1;
	uint64_t next = 0;
	int k = 0;
	while(k < K) {
		const uint64_t sameNext = nextSameSum(base, next);
		const uint64_t plusOneNext = nextSameSum(base + 1, next);

		const uint64_t nextBase = sameNext < plusOneNext ? base : base + 1;
		next = std::min(sameNext, plusOneNext);

		bool bre = false;
		FOR(n, 1, 90) {
			const auto target = nextSameSum(n, next);
			if (double(next) / digitssum(next) > double(nextSameSum(n, next)) / n) {
				bre = true;
				break;
			}
		}

		if (bre) {
			continue;
		}

		cout << next << endl;
		base = nextBase;
		k++;
	}
}