#include <iostream>
#include <vector>
#include <cstdint>
#include <math.h>
#include <algorithm>

typedef std::vector<std::uint64_t> DType;
//typedef std::pair<DType, double> RType;
typedef std::pair<DType, std::uint64_t> RType;
DType MakePrime(std::uint64_t N){
	DType R;
	R.push_back(2);
	bool F = false;

	for (std::size_t i = 3; i <= N; i++)
	{
		F = true;
		for (auto o : R){
			if (i%o == 0){
				F = false;
				break;
			}
		}
		if (F == true) R.push_back(i);

	}
	return R;
}
DType MnipArray2(DType D){
	std::uint64_t N = D.back();
	D.pop_back();
	D[0] += N;

	return D;
}
DType MnipArray(DType D){
	std::uint64_t N = D.back();
	D.pop_back();
	for (std::size_t i = 0; i < N; i++)
	{
		D[i%D.size()]++;
	}

	return D;
}

std::size_t FindFirstMinnestIndex(DType& D){

	std::size_t Idx = 0;
	std::uint64_t V = D[0];
	for (std::size_t i = 0; i < D.size(); i++)
	{
		if (D[i]<V){
			Idx = i;
			V = D[i];			
		}
	}
	return Idx;
}

DType MnipArray3(DType D){
	std::uint64_t N = D.back();
	D.pop_back();
	bool F = false;;
	std::size_t Idx = FindFirstMinnestIndex(D);

	for (std::size_t i = 0; i < N; i++)
	{
		D[(Idx + i) % D.size()]++;
	}

	return D;
}
RType MakeHoge(std::uint64_t N){
	std::int64_t X = N;
	auto P = MakePrime(static_cast<std::uint64_t>(X));//<- not need??
	DType R;
	double RD = 1;
	//std::uint64_t RD = 1;

	DType T;
	double TD = 1;
	std::uint64_t A = 0;
	std::uint64_t B = 0;

	while (X>1){
		for (auto o : P){
			if (X - o > 1){
				X -= o;
				R.push_back(o);
				break;
			}

		}
	}
	if (X != 0) R.back() += X;
	std::swap(R[0], R.back());

	T = R;

	while (T.size() > 2){
		TD = 1;
		for (auto o : T) TD *= o;
		if (TD > RD){
			RD = TD;
			R = T;
		}
		//std::reverse(T.begin(), T.end());	
		//std::swap(T[0], T.back());
		T = MnipArray3(T);

	}
	return std::make_pair(R,RD);
}

bool Show(RType& D){

	std::cout << D.second << "@" << D.first.size() << "Count!" << std::endl;
	std::cout << "Result:" << std::endl;
	for (auto o : D.first) std::cout << o << ',';
	std::cout << std::endl;
	return true;

}

int main(){

	RType R;

	R=MakeHoge(10);
	Show(R);
	R=MakeHoge(64);
	Show(R);
	R=MakeHoge(100);
	Show(R);
	return 0;
}