Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <chrono>
using namespace std;

//タイマー
class Timer {
	chrono::high_resolution_clock::time_point start, end;
	double limit;

public:
	Timer() {
		start = chrono::high_resolution_clock::now();
	}
	Timer(double l) {
		start = chrono::high_resolution_clock::now();
		limit = l;
	}

	double getTime() {
		end = chrono::high_resolution_clock::now();
		return chrono::duration<double>(end - start).count();
	}

	bool Over() {
		if (getTime() > limit) {
			return true;
		}
		return false;
	}

	void setLimit(double l) {
		limit = l;
	}
	void setStart() { start = chrono::high_resolution_clock::now(); }
	double getLimit() { return limit; }
};

//疑似乱数
class Xor128 {
	unsigned static int x, y, z, w;
public:
	Xor128() {
		x = 31103110, y = 123456789, z = 521288629, w = 88675123;
	}

	unsigned int rand()
	{
		unsigned int t;
		t = (x ^ (x << 11)); x = y; y = z; z = w;
		return(w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
	}
};
unsigned int Xor128::x, Xor128::y, Xor128::z, Xor128::w;

int A[10];
int C[50];
int Cnum[100];

int main()
{
	long long K; cin >> K;
	int ans = 100, num=0;

	for (int i = 1; i < 10; i++)A[i] = K*i;
	vector<vector<int>>B(10);
	for (int i = 1; i < 10; i++) {
		int a = A[i];
		int sum = 0;
		while (a) {
			int c = a % 10;
			B[i].push_back(c);
			a /= 10;

			sum += c;
		}

		if (ans > sum) {
			ans = sum;
			num = i;
		}
	}

	for (int i = 0; i < B[num].size(); i++)C[i] = B[num][i];
	Cnum[0] = num;

	int tmpans = ans;
	int last = 2;

	double startTemp = 5;
	double endTemp = 0.5;
	Xor128 xor128;
	Timer tmr(1.9);
	while (!tmr.Over()) {
		int r = xor128.rand() % last;
		bool flag = xor128.rand() % 2;
		int b;
		if (flag) {
			b = xor128.rand() % (10-Cnum[r]);
		}
		else {
			b = xor128.rand() % (Cnum[r]+1);
		}
		
		if (r + B[b].size() >= 49)continue;

		if (flag)Cnum[r] += b;
		else Cnum[r] -= b;

		int tmp = tmpans;
		int bias = 0;
		int i = 0;
		for (i = 0; i < B[b].size(); i++) {
			int tmpC = C[r + i];

			if(flag)C[r + i] += B[b][i];
			else C[r + i] -= B[b][i];
			
			bias = 0;
			if (C[r + i] >= 10) {
				C[r + i + 1]++;
				bias = 1;
				C[r + i] -= 10;
			}
			else if (C[r + i] < 0) {
				C[r + i + 1]--;
				bias = -1;
				C[r + i] += 10;
			}

			tmp += C[r + i] + bias - tmpC;
		}
		while (bias) {
			int tmpC = C[r + i];
			bias = 0;
			if (C[r + i] >= 10) {
				C[r + i + 1]++;
				bias = 1;
				C[r + i] -= 10;
			}
			else if (C[r + i] < 0) {
				C[r + i + 1]--;
				bias = -1;
				C[r + i] += 10;
			}
			tmp += C[r + i] + bias - tmpC;
			i++;
		}

		bool force = false;
		if (tmp > tmpans) {
			double temp = startTemp + (endTemp - startTemp) * tmr.getTime() / tmr.getLimit();
			double prob = exp((tmpans - tmp) / temp);
			force = prob*UINT32_MAX > xor128.rand();
		}

		if (tmp!=0 && (force || tmp <= tmpans)) {
			if (tmp < ans)ans = tmp;
			tmpans = tmp;

			if (C[r])last = max(last, r + 2);
			else if (C[r] == 0 && last == r + 2) {
				while (r) {
					if (C[r])break;
					last--;
					r--;
				}
			}
		}
		else {
			if (flag)Cnum[r] -= b;
			else Cnum[r] += b;

			int bias = 0;
			int i = 0;
			for (i = 0; i < B[b].size(); i++) {
				if (flag)C[r + i] -= B[b][i];
				else C[r + i] += B[b][i];

				bias = 0;
				if (C[r + i] >= 10) {
					C[r + i + 1]++;
					bias = 1;
					C[r + i] -= 10;
				}
				else if (C[r + i] < 0) {
					C[r + i + 1]--;
					bias = -1;
					C[r + i] += 10;
				}
			}
			while (bias) {
				bias = 0;
				if (C[r + i] >= 10) {
					C[r + i + 1]++;
					bias = 1;
					C[r + i] -= 10;
				}
				else if (C[r + i] < 0) {
					C[r + i + 1]--;
					bias = -1;
					C[r + i] += 10;
				}
				i++;
			}
		}
	}

	cout << ans << endl;

    return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:155:39: error: 'exp' was not declared in this scope
  155 |                         double prob = exp((tmpans - tmp) / temp);
      |                                       ^~~