<commit_before>#include "CMySQLHandle.h"
#include "CMySQLResult.h"
#include "CMySQLQuery.h"
#include "CLog.h"

#include "misc.h"

#include <boost/chrono.hpp>
namespace chrono = boost::chrono;

bool CMySQLQuery::Execute(MYSQL *mysql_connection)
{
	bool ret_val = false;
	char log_funcname[64];
	if (Unthreaded)
		sprintf(log_funcname, "CMySQLQuery::Execute");
	else
		sprintf(log_funcname, "CMySQLQuery::Execute[%s]", Callback.Name.c_str());

	CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "starting query execution");

	chrono::steady_clock::time_point query_exec = chrono::steady_clock::now();
	int query_error = mysql_real_query(mysql_connection, Query.c_str(), Query.length());
	chrono::steady_clock::duration query_exec_duration = chrono::steady_clock::now() - query_exec;

	if (query_error == 0)
	{
		unsigned int
			query_exec_time_milli = static_cast<unsigned int>(chrono::duration_cast<chrono::milliseconds>(query_exec_duration).count()),
			query_exec_time_micro = static_cast<unsigned int>(chrono::duration_cast<chrono::microseconds>(query_exec_duration).count());

		CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "query was successfully executed within %d.%d milliseconds", query_exec_time_milli, query_exec_time_micro-(query_exec_time_milli*1000));

		MYSQL_RES *mysql_result = mysql_store_result(mysql_connection); //this has to be here

		//why should we process the result if it won't and can't be used?
		if (Unthreaded || Callback.Name.length() > 0)
		{
			if (StoreResult(mysql_connection, mysql_result) == false)
				CLog::Get()->LogFunction(LOG_ERROR, log_funcname, "an error occured while storing the result: (error #%d) \"%s\"", mysql_errno(mysql_connection), mysql_error(mysql_connection));
			else
			{
				Result->m_Query = Query;
				Result->m_ExecTime[UNIT_MILLISECONDS] = query_exec_time_milli;
				Result->m_ExecTime[UNIT_MICROSECONDS] = query_exec_time_micro;
			}
		}
		else  //no callback was specified
			CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "no callback specified, skipping result saving");

		if (mysql_result != NULL)
			mysql_free_result(mysql_result);

		ret_val = true;
	}
	else  //mysql_real_query failed
	{
		int error_id = mysql_errno(mysql_connection);
		string error_str(mysql_error(mysql_connection));

		CLog::Get()->LogFunction(LOG_ERROR, log_funcname, "(error #%d) %s", error_id, error_str.c_str());

		if (Callback.Name.size() > 0 && !Unthreaded)
		{
			//forward OnQueryError(error_id, error[], callback[], query[], connectionHandle);
			//recycle these structures, change some data

			Orm.Object = NULL;
			Orm.Type = 0;

			while (Callback.Params.size() > 0)
				Callback.Params.pop();

			Callback.Params.push(static_cast<cell>(error_id));
			Callback.Params.push(error_str);
			Callback.Params.push(Callback.Name);
			Callback.Params.push(Query);
			Callback.Params.push(static_cast<cell>(Handle->GetID()));

			Callback.Name = "OnQueryError";

			CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "error will be triggered in OnQueryError");
		}
		ret_val = false;
	}

	Handle->DecreaseQueryCounter();
	return ret_val;
}

bool CMySQLQuery::StoreResult(MYSQL *mysql_connection, MYSQL_RES *mysql_result)
{
	if (mysql_result != NULL)
	{
		MYSQL_FIELD *mysql_field;
		MYSQL_ROW mysql_row;

		CMySQLResult *result_ptr = Result = new CMySQLResult;

		Result->m_WarningCount = mysql_warning_count(mysql_connection);

		const my_ulonglong num_rows = Result->m_Rows = mysql_num_rows(mysql_result);
		const unsigned int num_fields = Result->m_Fields = mysql_num_fields(mysql_result);

		Result->m_FieldNames.reserve(Result->m_Fields + 1);

		size_t row_data_size = 0;
		while (mysql_field = mysql_fetch_field(mysql_result))
		{
			Result->m_FieldNames.push_back(mysql_field->name);
			row_data_size += mysql_field->max_length + 1;
		}

		size_t
			mem_head_size = sizeof(char **) * static_cast<size_t>(num_rows),
			mem_row_size = (sizeof(char *) * (num_fields + 1)) + ((row_data_size) * sizeof(char));
		//+1 because there is another value in memory pointing to somewhere
		//mem_row_size has to be a multiple of 8
		while (mem_row_size % 8 != 0)
			mem_row_size++;

		const size_t mem_size = mem_head_size + static_cast<size_t>(num_rows) * mem_row_size;
		char ***mem_data = result_ptr->m_Data = static_cast<char ***>(malloc(mem_size));
		char **mem_offset = reinterpret_cast<char **>(&mem_data[num_rows]);

		for (size_t r = 0; r != num_rows; ++r)
		{
			mysql_row = mysql_fetch_row(mysql_result);

			//copy mysql result data to our location
			mem_data[r] = mem_offset;
			mem_offset += mem_row_size / sizeof(char **);
			memcpy(mem_data[r], mysql_row, mem_row_size);

			//correct the pointers of the copied mysql result data
			for (size_t f = 0; f != num_fields; ++f)
			{
				if(mysql_row[f] == NULL)
					continue;
				size_t dist = mysql_row[f] - reinterpret_cast<char *>(mysql_row);
				mem_data[r][f] = reinterpret_cast<char *>(mem_data[r]) + dist;
			}
		}
		return true;
	}
	else if (mysql_field_count(mysql_connection) == 0) //query is non-SELECT query
	{
		Result = new CMySQLResult;
		Result->m_WarningCount = mysql_warning_count(mysql_connection);
		Result->m_AffectedRows = mysql_affected_rows(mysql_connection);
		Result->m_InsertID = mysql_insert_id(mysql_connection);
		return true;
	}
	else //error
	{
		//we clear the callback name and forward it to the callback handler
		//the callback handler free's all memory but doesn't call the callback because there's no callback name
		Callback.Name.clear();
		return false;
	}
}
<commit_msg>fixed a bug regarding `mysql_unprocessed_queries`<commit_after>#include "CMySQLHandle.h"
#include "CMySQLResult.h"
#include "CMySQLQuery.h"
#include "CLog.h"

#include "misc.h"

#include <boost/chrono.hpp>
namespace chrono = boost::chrono;

bool CMySQLQuery::Execute(MYSQL *mysql_connection)
{
	bool ret_val = false;
	char log_funcname[64];
	if (Unthreaded)
		sprintf(log_funcname, "CMySQLQuery::Execute");
	else
		sprintf(log_funcname, "CMySQLQuery::Execute[%s]", Callback.Name.c_str());

	CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "starting query execution");

	chrono::steady_clock::time_point query_exec = chrono::steady_clock::now();
	int query_error = mysql_real_query(mysql_connection, Query.c_str(), Query.length());
	chrono::steady_clock::duration query_exec_duration = chrono::steady_clock::now() - query_exec;

	if (query_error == 0)
	{
		unsigned int
			query_exec_time_milli = static_cast<unsigned int>(chrono::duration_cast<chrono::milliseconds>(query_exec_duration).count()),
			query_exec_time_micro = static_cast<unsigned int>(chrono::duration_cast<chrono::microseconds>(query_exec_duration).count());

		CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "query was successfully executed within %d.%d milliseconds", query_exec_time_milli, query_exec_time_micro-(query_exec_time_milli*1000));

		MYSQL_RES *mysql_result = mysql_store_result(mysql_connection); //this has to be here

		//why should we process the result if it won't and can't be used?
		if (Unthreaded || Callback.Name.length() > 0)
		{
			if (StoreResult(mysql_connection, mysql_result) == false)
				CLog::Get()->LogFunction(LOG_ERROR, log_funcname, "an error occured while storing the result: (error #%d) \"%s\"", mysql_errno(mysql_connection), mysql_error(mysql_connection));
			else
			{
				Result->m_Query = Query;
				Result->m_ExecTime[UNIT_MILLISECONDS] = query_exec_time_milli;
				Result->m_ExecTime[UNIT_MICROSECONDS] = query_exec_time_micro;
			}
		}
		else  //no callback was specified
			CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "no callback specified, skipping result saving");

		if (mysql_result != NULL)
			mysql_free_result(mysql_result);

		ret_val = true;
	}
	else  //mysql_real_query failed
	{
		int error_id = mysql_errno(mysql_connection);
		string error_str(mysql_error(mysql_connection));

		CLog::Get()->LogFunction(LOG_ERROR, log_funcname, "(error #%d) %s", error_id, error_str.c_str());

		if (Callback.Name.size() > 0 && !Unthreaded)
		{
			//forward OnQueryError(error_id, error[], callback[], query[], connectionHandle);
			//recycle these structures, change some data

			Orm.Object = NULL;
			Orm.Type = 0;

			while (Callback.Params.size() > 0)
				Callback.Params.pop();

			Callback.Params.push(static_cast<cell>(error_id));
			Callback.Params.push(error_str);
			Callback.Params.push(Callback.Name);
			Callback.Params.push(Query);
			Callback.Params.push(static_cast<cell>(Handle->GetID()));

			Callback.Name = "OnQueryError";

			CLog::Get()->LogFunction(LOG_DEBUG, log_funcname, "error will be triggered in OnQueryError");
		}
		ret_val = false;
	}

	if(Unthreaded == false) //decrease counter only if threaded query
		Handle->DecreaseQueryCounter();
	return ret_val;
}

bool CMySQLQuery::StoreResult(MYSQL *mysql_connection, MYSQL_RES *mysql_result)
{
	if (mysql_result != NULL)
	{
		MYSQL_FIELD *mysql_field;
		MYSQL_ROW mysql_row;

		CMySQLResult *result_ptr = Result = new CMySQLResult;

		Result->m_WarningCount = mysql_warning_count(mysql_connection);

		const my_ulonglong num_rows = Result->m_Rows = mysql_num_rows(mysql_result);
		const unsigned int num_fields = Result->m_Fields = mysql_num_fields(mysql_result);

		Result->m_FieldNames.reserve(Result->m_Fields + 1);

		size_t row_data_size = 0;
		while (mysql_field = mysql_fetch_field(mysql_result))
		{
			Result->m_FieldNames.push_back(mysql_field->name);
			row_data_size += mysql_field->max_length + 1;
		}

		size_t
			mem_head_size = sizeof(char **) * static_cast<size_t>(num_rows),
			mem_row_size = (sizeof(char *) * (num_fields + 1)) + ((row_data_size) * sizeof(char));
		//+1 because there is another value in memory pointing to somewhere
		//mem_row_size has to be a multiple of 8
		while (mem_row_size % 8 != 0)
			mem_row_size++;

		const size_t mem_size = mem_head_size + static_cast<size_t>(num_rows) * mem_row_size;
		char ***mem_data = result_ptr->m_Data = static_cast<char ***>(malloc(mem_size));
		char **mem_offset = reinterpret_cast<char **>(&mem_data[num_rows]);

		for (size_t r = 0; r != num_rows; ++r)
		{
			mysql_row = mysql_fetch_row(mysql_result);

			//copy mysql result data to our location
			mem_data[r] = mem_offset;
			mem_offset += mem_row_size / sizeof(char **);
			memcpy(mem_data[r], mysql_row, mem_row_size);

			//correct the pointers of the copied mysql result data
			for (size_t f = 0; f != num_fields; ++f)
			{
				if(mysql_row[f] == NULL)
					continue;
				size_t dist = mysql_row[f] - reinterpret_cast<char *>(mysql_row);
				mem_data[r][f] = reinterpret_cast<char *>(mem_data[r]) + dist;
			}
		}
		return true;
	}
	else if (mysql_field_count(mysql_connection) == 0) //query is non-SELECT query
	{
		Result = new CMySQLResult;
		Result->m_WarningCount = mysql_warning_count(mysql_connection);
		Result->m_AffectedRows = mysql_affected_rows(mysql_connection);
		Result->m_InsertID = mysql_insert_id(mysql_connection);
		return true;
	}
	else //error
	{
		//we clear the callback name and forward it to the callback handler
		//the callback handler free's all memory but doesn't call the callback because there's no callback name
		Callback.Name.clear();
		return false;
	}
}
<|endoftext|>


<commit_before>#include <CQCheckGrid.h>

#include <QGridLayout>
#include <QCheckBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QPainter>
#include <QMenu>
#include <QContextMenuEvent>

CQCheckGrid::
CQCheckGrid(QWidget *parent, int cols) :
 CQGridCheckCell(parent), cols_(cols)
{
  setObjectName("grid");

  layout_ = new QGridLayout(this);
  layout_->setSpacing(2); layout_->setMargin(2);

  setContextMenuPolicy(Qt::DefaultContextMenu);

  group_ = new QButtonGroup(this);

  group_->setExclusive(false);
}

void
CQCheckGrid::
setExclusive(bool exclusive)
{
  exclusive_ = exclusive;

  group_->setExclusive(exclusive_);
}

QAbstractButton *
CQCheckGrid::
addCheck(const QString &name)
{
  auto *box = new CQGridCheckBox(this, name, exclusive());

  group_->addButton(box->button());

  if (exclusive() && cells_.empty())
    box->button()->setChecked(true);

  if (col_ > 0 && col_ + 1 > cols_) {
    col_ = 0;
    ++row_;
  }

  layout_->addWidget(box, row_, col_, 1, 1);

  ++col_;

  cells_.push_back(box);

  return box->button();
}

void
CQCheckGrid::
addSpacer()
{
  auto *spacer = new CQGridCheckSpacer(this);

  if (col_ > 0 && col_ + 1 > cols_) {
    col_ = 0;
    ++row_;
  }

  layout_->addWidget(spacer, row_, col_, 1, 1);

  ++col_;

  cells_.push_back(spacer);
}

CQCheckGrid *
CQCheckGrid::
createGroup(const QString &name)
{
  auto *grid = new CQCheckGrid(this);

  grid->setTitle(name);

  return grid;
}

void
CQCheckGrid::
addGroup(CQCheckGrid *grid)
{
  int cols = grid->cols();

  if (col_ > 0 && col_ + cols > cols_) {
    col_ = 0;
    ++row_;
  }

  layout_->addWidget(grid, row_, col_, 1, cols);

  col_ += cols;

  cells_.push_back(grid);
}

void
CQCheckGrid::
setColumns(int cols)
{
  if (cols_ == cols)
    return;

  QLayoutItem *child;

  while ((child = layout_->takeAt(0)) != 0)
    delete child;

  for (uint i = 0; i < cells_.size(); ++i) {
    int row = i / cols_;
    int col = i % cols_;

    layout_->addWidget(cells_[i], row, col);
  }
}

void
CQCheckGrid::
setTitle(const QString &title)
{
  title_ = title;

  if (title_.length()) {
    QFontMetrics fm(font());

    setContentsMargins(4, fm.height() + 4, 4, 4);
  }
  else
    setContentsMargins(0, 0, 0, 0);
}

void
CQCheckGrid::
paintEvent(QPaintEvent *)
{
  if (title_.length()) {
    QColor bg = palette().background().color();

    QPainter p(this);

    QFontMetrics fm(font());

    QRect r = rect().adjusted(2, (fm.height() + 4)/2, -2, -2);

    p.setPen(bg.darker(150));

    p.drawRect(r);

    r = fm.boundingRect(title_);

    r.adjust(8, 2 + fm.ascent(), 8, 2 + fm.ascent());

    p.fillRect(r, QBrush(bg));

    p.setPen(QColor(0,0,0));

    p.drawText(8, 2 + fm.ascent(), title_);
  }
}

void
CQCheckGrid::
contextMenuEvent(QContextMenuEvent *e)
{
  if (! exclusive()) {
    auto *menu = new QMenu;

    auto *allAction  = menu->addAction("Select All" );
    auto *noneAction = menu->addAction("Select None");

    allAction ->setEnabled(! isAllChecked());
    noneAction->setEnabled(isAnyChecked());

    connect(allAction , SIGNAL(triggered()), this, SLOT(selectAll()));
    connect(noneAction, SIGNAL(triggered()), this, SLOT(selectNone()));

    (void) menu->exec(e->globalPos());

    delete menu;
  }
}

bool
CQCheckGrid::
isAnyChecked() const
{
  for (uint i = 0; i < cells_.size(); ++i) {
    if (! cells_[i]->isCheckable()) continue;

    if (cells_[i]->isAnyChecked())
      return true;
  }

  return false;
}

bool
CQCheckGrid::
isAllChecked() const
{
  for (uint i = 0; i < cells_.size(); ++i) {
    if (! cells_[i]->isCheckable()) continue;

    if (! cells_[i]->isAllChecked())
      return false;
  }

  return true;
}

void
CQCheckGrid::
selectAll()
{
  setChecked(true);
}

void
CQCheckGrid::
selectNone()
{
  setChecked(false);
}

void
CQCheckGrid::
setChecked(bool checked)
{
  if (exclusive()) return;

  for (uint i = 0; i < cells_.size(); ++i) {
    if (! cells_[i]->isCheckable()) continue;

    cells_[i]->setChecked(checked);
  }
}

int
CQCheckGrid::
rows() const
{
  int rows = cells_.size()/columns();

  if (cells_.size() % columns())
    ++rows;

  return rows;
}

int
CQCheckGrid::
cols() const
{
  if (int(cells_.size()) < columns())
    return cells_.size();
  else
    return columns();
}

//------

CQGridCheckBox::
CQGridCheckBox(QWidget *parent, const QString &name, bool exclusive) :
 CQGridCheckCell(parent)
{
  setObjectName(name);

  auto *layout = new QHBoxLayout(this);
  layout->setSpacing(0); layout->setMargin(0);

  if (exclusive)
    button_ = new QRadioButton(this);
  else
    button_ = new QCheckBox(this);

  button_->setText(name);

  layout->addWidget(button_);
}

bool
CQGridCheckBox::
isAnyChecked() const
{
  return button_->isChecked();
}

bool
CQGridCheckBox::
isAllChecked() const
{
  return isAnyChecked();
}

void
CQGridCheckBox::
setChecked(bool checked)
{
  button_->setChecked(checked);
}
<commit_msg>new files<commit_after>#include <CQCheckGrid.h>

#include <QGridLayout>
#include <QCheckBox>
#include <QRadioButton>
#include <QButtonGroup>
#include <QPainter>
#include <QMenu>
#include <QContextMenuEvent>

CQCheckGrid::
CQCheckGrid(QWidget *parent, int cols) :
 CQGridCheckCell(parent), cols_(cols)
{
  setObjectName("grid");

  layout_ = new QGridLayout(this);
  layout_->setSpacing(2); layout_->setMargin(2);

  setContextMenuPolicy(Qt::DefaultContextMenu);

  group_ = new QButtonGroup(this);

  group_->setExclusive(false);
}

void
CQCheckGrid::
setExclusive(bool exclusive)
{
  exclusive_ = exclusive;

  group_->setExclusive(exclusive_);
}

QAbstractButton *
CQCheckGrid::
addCheck(const QString &name)
{
  auto *box = new CQGridCheckBox(this, name, exclusive());

  group_->addButton(box->button());

  if (exclusive() && cells_.empty())
    box->button()->setChecked(true);

  if (col_ > 0 && col_ + 1 > cols_) {
    col_ = 0;
    ++row_;
  }

  layout_->addWidget(box, row_, col_, 1, 1);

  ++col_;

  cells_.push_back(box);

  return box->button();
}

void
CQCheckGrid::
addSpacer()
{
  auto *spacer = new CQGridCheckSpacer(this);

  if (col_ > 0 && col_ + 1 > cols_) {
    col_ = 0;
    ++row_;
  }

  layout_->addWidget(spacer, row_, col_, 1, 1);

  ++col_;

  cells_.push_back(spacer);
}

CQCheckGrid *
CQCheckGrid::
createGroup(const QString &name)
{
  auto *grid = new CQCheckGrid(this);

  grid->setTitle(name);

  return grid;
}

void
CQCheckGrid::
addGroup(CQCheckGrid *grid)
{
  int cols = grid->cols();

  if (col_ > 0 && col_ + cols > cols_) {
    col_ = 0;
    ++row_;
  }

  layout_->addWidget(grid, row_, col_, 1, cols);

  col_ += cols;

  cells_.push_back(grid);
}

void
CQCheckGrid::
setColumns(int cols)
{
  if (cols_ == cols)
    return;

  QLayoutItem *child;

  while ((child = layout_->takeAt(0)) != 0)
    delete child;

  for (uint i = 0; i < cells_.size(); ++i) {
    int row = int(i) / cols_;
    int col = int(i) % cols_;

    layout_->addWidget(cells_[i], row, col);
  }
}

void
CQCheckGrid::
setTitle(const QString &title)
{
  title_ = title;

  if (title_.length()) {
    QFontMetrics fm(font());

    setContentsMargins(4, fm.height() + 4, 4, 4);
  }
  else
    setContentsMargins(0, 0, 0, 0);
}

void
CQCheckGrid::
paintEvent(QPaintEvent *)
{
  if (title_.length()) {
    auto bg = palette().background().color();

    QPainter p(this);

    QFontMetrics fm(font());

    QRect r = rect().adjusted(2, (fm.height() + 4)/2, -2, -2);

    p.setPen(bg.darker(150));

    p.drawRect(r);

    r = fm.boundingRect(title_);

    r.adjust(8, 2 + fm.ascent(), 8, 2 + fm.ascent());

    p.fillRect(r, QBrush(bg));

    p.setPen(QColor(0,0,0));

    p.drawText(8, 2 + fm.ascent(), title_);
  }
}

void
CQCheckGrid::
contextMenuEvent(QContextMenuEvent *e)
{
  if (! exclusive()) {
    auto *menu = new QMenu;

    auto *allAction  = menu->addAction("Select All" );
    auto *noneAction = menu->addAction("Select None");

    allAction ->setEnabled(! isAllChecked());
    noneAction->setEnabled(isAnyChecked());

    connect(allAction , SIGNAL(triggered()), this, SLOT(selectAll()));
    connect(noneAction, SIGNAL(triggered()), this, SLOT(selectNone()));

    (void) menu->exec(e->globalPos());

    delete menu;
  }
}

bool
CQCheckGrid::
isAnyChecked() const
{
  for (uint i = 0; i < cells_.size(); ++i) {
    if (! cells_[i]->isCheckable()) continue;

    if (cells_[i]->isAnyChecked())
      return true;
  }

  return false;
}

bool
CQCheckGrid::
isAllChecked() const
{
  for (uint i = 0; i < cells_.size(); ++i) {
    if (! cells_[i]->isCheckable()) continue;

    if (! cells_[i]->isAllChecked())
      return false;
  }

  return true;
}

void
CQCheckGrid::
selectAll()
{
  setChecked(true);
}

void
CQCheckGrid::
selectNone()
{
  setChecked(false);
}

void
CQCheckGrid::
setChecked(bool checked)
{
  if (exclusive()) return;

  for (uint i = 0; i < cells_.size(); ++i) {
    if (! cells_[i]->isCheckable()) continue;

    cells_[i]->setChecked(checked);
  }
}

int
CQCheckGrid::
rows() const
{
  int rows = int(cells_.size())/columns();

  if (int(cells_.size()) % columns())
    ++rows;

  return rows;
}

int
CQCheckGrid::
cols() const
{
  if (int(cells_.size()) < columns())
    return int(cells_.size());
  else
    return columns();
}

//------

CQGridCheckBox::
CQGridCheckBox(QWidget *parent, const QString &name, bool exclusive) :
 CQGridCheckCell(parent)
{
  setObjectName(name);

  auto *layout = new QHBoxLayout(this);
  layout->setSpacing(0); layout->setMargin(0);

  if (exclusive)
    button_ = new QRadioButton(this);
  else
    button_ = new QCheckBox(this);

  button_->setText(name);

  layout->addWidget(button_);
}

bool
CQGridCheckBox::
isAnyChecked() const
{
  return button_->isChecked();
}

bool
CQGridCheckBox::
isAllChecked() const
{
  return isAnyChecked();
}

void
CQGridCheckBox::
setChecked(bool checked)
{
  button_->setChecked(checked);
}
<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2/16/2019, 9:07:46 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <functional>
#include <random> // auto rd = bind(uniform_int_distribution<int>(0, 9), mt19937(10101010));
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int A[10];
int need[10] = {100, 2, 5, 5, 4, 5, 6, 3, 7, 6};
map<int, int> X;
vector<vector<int>> Y;
vector<vector<int>> W;

void dfs(vector<int> L, int R)
{
#if DEBUG == 1
  cerr << "dfs(" << L.size() << ", " << R << ")" << endl;
#endif
  if (R == 0)
  {
    Y.push_back(L);
  }
  else
  {
    for (auto x : X)
    {
      int cost = x.first;
      if ((L.empty() || cost >= L[L.size() - 1]) && cost <= R)
      {
        L.push_back(cost);
        dfs(L, R - cost);
        L.pop_back();
      }
    }
  }
}

int max_score(vector<int> L)
{
  vector<int> F;
  for (auto x : L)
  {
    F.push_back(X[x]);
  }
  sort(F.begin(), F.end());
  reverse(F.begin(), F.end());
  string ans = "";
  for (auto i = 0; i < min(9, (int)F.size()); i++)
  {
    ans = ans + to_string(F[i]);
  }
#if DEBUG == 1
  cerr << "max_score: " << ans << endl;
#endif
  return stoi(ans);
}

void represent(vector<int> L)
{
  vector<int> F;
  for (auto x : L)
  {
    F.push_back(X[x]);
  }
  sort(F.begin(), F.end());
  reverse(F.begin(), F.end());
  for (auto i = 0; i < (int)F.size(); i++)
  {
    cerr << F[i];
  }
}

int main()
{
  cin >> N >> M;
  for (auto i = 0; i < M; i++)
  {
    cin >> A[i];
    int n = need[A[i]];
    if (X.find(n) == X.end())
    {
      X[n] = A[i];
    }
    else
    {
      X[n] = max(X[n], A[i]);
    }
  }
  int cnt = 0;
  int cost = (*X.begin()).first;
  string base = to_string((*X.begin()).second);
  while (N > 200)
  {
    N -= cost;
    cnt++;
  }
  if (cost == 2 && N % 2 == 0)
  {
    while (N > 0)
    {
      N -= cost;
      cnt++;
    }
  }
  else if (cost == 2)
  {
    auto it = X.begin();
    while ((*it).first % 2 == 0)
    {
      it++;
    }
    cout << (*it).second;
    N -= (*it).first;
    while (N > 0)
    {
      N -= cost;
      cnt++;
    }
  }
  else
  {
    vector<int> V = {};
#if DEBUG == 1
    cerr << "N = " << N << endl;
#endif
    dfs(V, N);
    int maxi = 0;
    for (auto v : Y)
    {
      maxi = max((int)v.size(), maxi);
    }
#if DEBUG == 1
    cerr << "maxi = " << maxi << endl;
#endif
    for (auto v : Y)
    {
      if ((int)v.size() == maxi)
      {
        W.push_back(v);
      }
    }
    int maxi_score = 0;
    vector<int> ans;
    for (auto v : W)
    {
      if (maxi_score < max_score(v))
      {
        maxi_score = max_score(v);
        ans = v;
      }
    }
    represent(ans);
  }
  for (auto i = 0; i < cnt; i++)
  {
    cout << base;
  }
  cout << endl;
}<commit_msg>tried D.cpp to 'D'<commit_after>#define DEBUG 1

/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2/16/2019, 9:07:46 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>   // << fixed << setprecision(xxx)
#include <algorithm> // do { } while ( next_permutation(A, A+xxx) ) ;
#include <vector>
#include <string> // to_string(nnn) // substr(m, n) // stoi(nnn)
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map> // if (M.find(key) != M.end()) { }
#include <set>
#include <functional>
#include <random> // auto rd = bind(uniform_int_distribution<int>(0, 9), mt19937(10101010));
#include <chrono> // std::chrono::system_clock::time_point start_time, end_time;
// start = std::chrono::system_clock::now();
// double elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end_time-start_time).count();
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

// const int C = 1e6+10;
// const ll M = 1000000007;

int N, M;
int A[10];
int need[10] = {100, 2, 5, 5, 4, 5, 6, 3, 7, 6};
map<int, int> X;
vector<vector<int>> Y;
vector<vector<int>> W;

void dfs(vector<int> L, int R)
{
#if DEBUG == 1
  cerr << "dfs(" << L.size() << ", " << R << ")" << endl;
  cerr << "L: ";
  for (auto x : L)
  {
    cerr << x << " ";
  }
  cerr << endl;
#endif
  if (R == 0)
  {
    Y.push_back(L);
  }
  else
  {
    for (auto x : X)
    {
      int cost = x.first;
      if ((L.empty() || cost >= L[L.size() - 1]) && cost <= R)
      {
        L.push_back(cost);
        dfs(L, R - cost);
        L.pop_back();
      }
    }
  }
}

int max_score(vector<int> L)
{
  vector<int> F;
  for (auto x : L)
  {
    F.push_back(X[x]);
  }
  sort(F.begin(), F.end());
  reverse(F.begin(), F.end());
  string ans = "";
  for (auto i = 0; i < min(9, (int)F.size()); i++)
  {
    ans = ans + to_string(F[i]);
  }
#if DEBUG == 1
  cerr << "max_score: " << ans << endl;
#endif
  return stoi(ans);
}

void represent(vector<int> L)
{
  vector<int> F;
  for (auto x : L)
  {
    F.push_back(X[x]);
  }
  sort(F.begin(), F.end());
  reverse(F.begin(), F.end());
  for (auto i = 0; i < (int)F.size(); i++)
  {
    cerr << F[i];
  }
}

int main()
{
  cin >> N >> M;
  for (auto i = 0; i < M; i++)
  {
    cin >> A[i];
    int n = need[A[i]];
    if (X.find(n) == X.end())
    {
      X[n] = A[i];
    }
    else
    {
      X[n] = max(X[n], A[i]);
    }
  }
  int cnt = 0;
  int cost = (*X.begin()).first;
  string base = to_string((*X.begin()).second);
  while (N > 200)
  {
    N -= cost;
    cnt++;
  }
  if (cost == 2 && N % 2 == 0)
  {
    while (N > 0)
    {
      N -= cost;
      cnt++;
    }
  }
  else if (cost == 2)
  {
    auto it = X.begin();
    while ((*it).first % 2 == 0)
    {
      it++;
    }
    cout << (*it).second;
    N -= (*it).first;
    while (N > 0)
    {
      N -= cost;
      cnt++;
    }
  }
  else
  {
    vector<int> V = {};
#if DEBUG == 1
    cerr << "N = " << N << endl;
#endif
    dfs(V, N);
    int maxi = 0;
    for (auto v : Y)
    {
      maxi = max((int)v.size(), maxi);
    }
#if DEBUG == 1
    cerr << "maxi = " << maxi << endl;
#endif
    for (auto v : Y)
    {
      if ((int)v.size() == maxi)
      {
        W.push_back(v);
      }
    }
    int maxi_score = 0;
    vector<int> ans;
    for (auto v : W)
    {
      if (maxi_score < max_score(v))
      {
        maxi_score = max_score(v);
        ans = v;
      }
    }
    represent(ans);
  }
  for (auto i = 0; i < cnt; i++)
  {
    cout << base;
  }
  cout << endl;
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 7/20/2019, 10:15:22 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 1e6 + 10;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

int N, K;

int calc(const vector<int> &V)
{
  int ans = 0;
  for (auto i = 0; i < N; i++)
  {
    ans += abs(V[i] - i);
  }
  return ans;
}

vector<int> maxi_strange()
{
  vector<int> V;
  vector<bool> used(N, false);
  for (auto k = 0; k < N; k++)
  {
    int mini = 0, maxi = N - 1;
    for (auto i = 0; i < N; i++)
    {
      if (!used[i])
      {
        mini = i;
        break;
      }
    }
    for (auto i = N - 1; i >= 0; i--)
    {
      if (!used[i])
      {
        maxi = i;
        break;
      }
    }
    while (abs(maxi - mini) > 1)
    {
      int t = (maxi + mini) / 2;
      vector<int> W = V;
      vector<bool> used_t = used;
      W.push_back(t);
      used_t[t] = true;
      for (auto i = 0; i < N; i++)
      {
        if (!used_t[i])
        {
          W.push_back(i);
        }
      }
      if (calc(W) <= K)
      {
        mini = t;
      }
      else
      {
        maxi = t;
      }
    }
    used[mini] = true;
    V.push_back(mini);
  }
  return V;
}

vector<int> mini_strange()
{
  vector<int> V;
  vector<bool> used(N, false);
  for (auto k = 0; k < N; k++)
  {
    int mini = 0, maxi = N - 1;
    for (auto i = 0; i < N; i++)
    {
      if (!used[i])
      {
        mini = i;
        break;
      }
    }
    for (auto i = N - 1; i >= 0; i--)
    {
      if (!used[i])
      {
        maxi = i;
        break;
      }
    }
    while (abs(maxi - mini) > 1)
    {
      int t = (maxi + mini) / 2;
      vector<int> W = V;
      vector<bool> used_t = used;
      W.push_back(t);
      used_t[t] = true;
      for (auto i = 0; i < N; i++)
      {
        if (!used_t[i])
        {
          W.push_back(i);
        }
      }
      if (calc(W) >= K)
      {
        maxi = t;
      }
      else
      {
        mini = t;
      }
    }
    used[maxi] = true;
    V.push_back(maxi);
  }
  return V;
}

int main()
{
  cin >> N >> K;
  auto V = maxi_strange();
  auto W = mini_strange();
#if DEBUG == 1
  for (auto x : V)
  {
    cerr << x << " ";
  }
  cerr << endl;
#endif
#if DEBUG == 1
  for (auto x : W)
  {
    cerr << x << " ";
  }
  cerr << endl;
#endif
}<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 7/20/2019, 10:15:22 PM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 1e6 + 10;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

int N, K;

int calc(const vector<int> &V)
{
  int ans = 0;
  for (auto i = 0; i < N; i++)
  {
    ans += abs(V[i] - i);
  }
  return ans;
}

vector<int> maxi_strange()
{
  vector<int> V;
  vector<bool> used(N, false);
  for (auto k = 0; k < N; k++)
  {
    int mini = 0, maxi = N - 1;
    for (auto i = 0; i < N; i++)
    {
      if (!used[i])
      {
        mini = i;
        break;
      }
    }
    for (auto i = N - 1; i >= 0; i--)
    {
      if (!used[i])
      {
        maxi = i;
        break;
      }
    }
    while (abs(maxi - mini) > 1)
    {
      int t = (maxi + mini) / 2;
      vector<int> W = V;
      vector<bool> used_t = used;
      W.push_back(t);
      used_t[t] = true;
      for (auto i = 0; i < N; i++)
      {
        if (!used_t[i])
        {
          W.push_back(i);
        }
      }
      if (calc(W) <= K)
      {
        mini = t;
      }
      else
      {
        maxi = t;
      }
    }
    used[mini] = true;
    V.push_back(mini);
  }
  return V;
}

vector<int> mini_strange()
{
  vector<int> V;
  vector<bool> used(N, false);
  for (auto k = 0; k < N; k++)
  {
    int mini = 0, maxi = N - 1;
    for (auto i = 0; i < N; i++)
    {
      if (!used[i])
      {
        mini = i;
        break;
      }
    }
    for (auto i = N - 1; i >= 0; i--)
    {
      if (!used[i])
      {
        maxi = i;
        break;
      }
    }
    while (abs(maxi - mini) > 1)
    {
      int t = (maxi + mini) / 2;
      vector<int> W = V;
      vector<bool> used_t = used;
      W.push_back(t);
      used_t[t] = true;
      for (auto i = N - 1; i >= 0; i--)
      {
        if (!used_t[i])
        {
          W.push_back(i);
        }
      }
      if (calc(W) >= K)
      {
        maxi = t;
      }
      else
      {
        mini = t;
      }
    }
    used[maxi] = true;
    V.push_back(maxi);
  }
  return V;
}

int main()
{
  cin >> N >> K;
  auto V = maxi_strange();
  auto W = mini_strange();
#if DEBUG == 1
  for (auto x : V)
  {
    cerr << x << " ";
  }
  cerr << endl;
#endif
#if DEBUG == 1
  for (auto x : W)
  {
    cerr << x << " ";
  }
  cerr << endl;
#endif
}<|endoftext|>


<commit_before>//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

// RUN: cat %s | %cling -I%p | FileCheck %s
#include <cmath>

struct S{int i;} ss;
S s = {12 };

struct U{void f() const {};} uu;

struct V{V(): v(12) {}; int v; } vv;

int i = 12;
float f = sin(12);
int j = i;
extern "C" int printf(const char* fmt, ...);
printf("j=%d\n",j); // CHECK:j=12
#include <string>
std::string str("abc");
printf("str=%s\n",str.c_str()); // CHECK: str=abc

.q
<commit_msg>Temporary breakage to debug cont integration.<commit_after>//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

// RUN: cat %s | %cling -I%p | FileCheck %s
#include <cmath>

TEMPORARY BREAKAGE!

struct S{int i;} ss;
S s = {12 };

struct U{void f() const {};} uu;

struct V{V(): v(12) {}; int v; } vv;

int i = 12;
float f = sin(12);
int j = i;
extern "C" int printf(const char* fmt, ...);
printf("j=%d\n",j); // CHECK:j=12
#include <string>
std::string str("abc");
printf("str=%s\n",str.c_str()); // CHECK: str=abc

.q
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/8/17 21:17:12
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 998244353;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

combination C{};

class info
{
public:
  char c;
  ll l;
  vector<ll> v;

  info() {}
  info(char _c, ll _l) : c{_c}, l{_l}, v{0, 0, 0} {}
};

vector<info> make_vec(string const &S)
{
  vector<info> ans;
  char c{S[0]};
  ll now{0};
  for (auto x : S)
  {
#if DEBUG == 1
    cerr << "x = " << x << endl;
#endif
    if (c == x)
    {
      ++now;
    }
    else
    {
      ans.emplace_back(c, now);
      now = 1;
      c = x;
    }
  }
  ans.emplace_back(c, now);
  return ans;
}

mint calc(vector<info> const &V)
{
  mint ans{1};
  for (auto e : V)
  {
    for (auto x : e.v)
    {
      ans *= C.fact[x];
    }
  }
  return ans;
}

void make_v(vector<info> &V)
{
  map<char, ll> M;
  M['R'] = M['G'] = M['B'] = 0;
  for (auto &e : V)
  {
    char c{e.c};
    ll now{M[c]};
    ll next{now + e.l};
    vector<ll> X;
    for (auto x : M)
    {
      if (x.first == c)
      {
        continue;
      }
      X.push_back(x.second);
    }
    sort(X.begin(), X.end());
    if (now < X[0])
    {
      e.v[0] = min(X[0], next) - now;
    }
    if (X[0] < X[1])
    {
      e.v[1] = min(X[1], next) - max(X[0], now);
    }
    if (X[1] < next)
    {
      e.v[2] = next - max(X[1], now);
    }
    M[c] = next;
  }
}

int main()
{
  string S;
  cin >> S;
  ll N = S.size() / 3;
  vector<info> V{make_vec(S)};
  make_v(V);
#if DEBUG == 1
  for (auto e : V)
  {
    cerr << e.c << e.l << ": {" << e.v[0] << ", " << e.v[1] << ", " << e.v[2] << "}" << endl;
  }
#endif
  mint ans{calc(V) * C.fact[N]};
  cout << ans << endl;
}<commit_msg>tried B.cpp to 'B'<commit_after>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/8/17 21:17:12
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 998244353;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

combination C{};

class info
{
public:
  char c;
  ll l;
  vector<ll> v;

  info() {}
  info(char _c, ll _l) : c{_c}, l{_l}, v{0, 0, 0} {}
};

vector<info> make_vec(string const &S)
{
  vector<info> ans;
  char c{S[0]};
  ll now{0};
  for (auto x : S)
  {
#if DEBUG == 1
    cerr << "x = " << x << endl;
#endif
    if (c == x)
    {
      ++now;
    }
    else
    {
      ans.emplace_back(c, now);
      now = 1;
      c = x;
    }
  }
  ans.emplace_back(c, now);
  return ans;
}

mint calc(vector<info> const &V)
{
  mint ans{1};
  for (auto e : V)
  {
    for (auto x : e.v)
    {
      ans *= C.fact[x];
    }
  }
  return ans;
}

void make_v(vector<info> &V)
{
  map<char, ll> M;
  M['R'] = M['G'] = M['B'] = 0;
  for (auto &e : V)
  {
    char c{e.c};
    ll now{M[c]};
    ll next{now + e.l};
    vector<ll> X;
    for (auto x : M)
    {
      if (x.first == c)
      {
        continue;
      }
      X.push_back(x.second);
    }
    sort(X.begin(), X.end());
    if (now < X[0])
    {
      e.v[0] = min(X[0], next) - now;
    }
    if (X[0] < X[1])
    {
      e.v[1] = min(X[1], next) - max(X[0], now);
    }
    if (X[1] < next)
    {
      e.v[2] = next - max(X[1], now);
    }
    M[c] = next;
  }
}

int main()
{
  ll N;
  cin >> N;
  string S;
  cin >> S;
  vector<info> V{make_vec(S)};
  make_v(V);
#if DEBUG == 1
  for (auto e : V)
  {
    cerr << e.c << e.l << ": {" << e.v[0] << ", " << e.v[1] << ", " << e.v[2] << "}" << endl;
  }
#endif
  mint ans{calc(V) * C.fact[N]};
  cout << ans << endl;
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/8/17 21:17:12
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 998244353;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

combination C{};

class info
{
public:
  char c;
  ll l;
  vector<ll> v;

  info() {}
  info(char _c, ll _l) : c{_c}, l{_l}, v{0, 0, 0} {}
};

vector<info> make_vec(string const &S)
{
  vector<info> ans;
  char c{S[0]};
  ll now{0};
  for (auto x : S)
  {
    if (c == x)
    {
      ++now;
    }
    else
    {
      ans.emplace_back(c, now);
      now = 1;
      c = x;
    }
  }
  ans.emplace_back(c, now);
  return ans;
}

mint calc(vector<info> const &V)
{
  mint ans{1};
  for (auto e : V)
  {
    for (auto x : e.v)
    {
      ans *= C.fact[x];
    }
  }
  return ans;
}

void make_v(vector<info> &V)
{
  map<char, ll> M;
  M['R'] = M['G'] = M['B'] = 0;
  for (auto &e : V)
  {
    char c{e.c};
    ll now{M[c]};
    ll next{now + e.l};
    vector<ll> X;
    for (auto x : M)
    {
      if (x.first == c)
      {
        continue;
      }
      X.push_back(x.second);
    }
    sort(X.begin(), X.end());
    if (now < X[0])
    {
      e.v[0] = min(X[0], next) - now;
    }
    if (X[0] < X[1])
    {
      e.v[1] = min(X[1], next) - max(X[0], now);
    }
    if (X[1] < next)
    {
      e.v[2] = next - max(X[1], now);
    }
    M[c] = next;
  }
}

int main()
{
  string S;
  cin >> S;
  ll N = S.size() / 3;
  vector<info> V{make_vec(S)};
  make_v(V);
#if DEBUG == 1
  for (auto e : V)
  {
    cerr << e.c << e.l << ": {" << e.v[0] << ", " << e.v[1] << ", " << e.v[2] << "}" << endl;
  }
#endif
  mint ans{calc(V) * C.fact[N]};
  cout << ans << endl;
}<commit_msg>tried B.cpp to 'B'<commit_after>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 2019/8/17 21:17:12
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 998244353;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

combination C{};

class info
{
public:
  char c;
  ll l;
  vector<ll> v;

  info() {}
  info(char _c, ll _l) : c{_c}, l{_l}, v{0, 0, 0} {}
};

vector<info> make_vec(string const &S)
{
  vector<info> ans;
  char c{S[0]};
  ll now{0};
  for (auto x : S)
  {
#if DEBUG == 1
    cerr << "x = " << x << endl;
#endif
    if (c == x)
    {
      ++now;
    }
    else
    {
      ans.emplace_back(c, now);
      now = 1;
      c = x;
    }
  }
  ans.emplace_back(c, now);
  return ans;
}

mint calc(vector<info> const &V)
{
  mint ans{1};
  for (auto e : V)
  {
    for (auto x : e.v)
    {
      ans *= C.fact[x];
    }
  }
  return ans;
}

void make_v(vector<info> &V)
{
  map<char, ll> M;
  M['R'] = M['G'] = M['B'] = 0;
  for (auto &e : V)
  {
    char c{e.c};
    ll now{M[c]};
    ll next{now + e.l};
    vector<ll> X;
    for (auto x : M)
    {
      if (x.first == c)
      {
        continue;
      }
      X.push_back(x.second);
    }
    sort(X.begin(), X.end());
    if (now < X[0])
    {
      e.v[0] = min(X[0], next) - now;
    }
    if (X[0] < X[1])
    {
      e.v[1] = min(X[1], next) - max(X[0], now);
    }
    if (X[1] < next)
    {
      e.v[2] = next - max(X[1], now);
    }
    M[c] = next;
  }
}

int main()
{
  string S;
  cin >> S;
  ll N = S.size() / 3;
  vector<info> V{make_vec(S)};
  make_v(V);
#if DEBUG == 1
  for (auto e : V)
  {
    cerr << e.c << e.l << ": {" << e.v[0] << ", " << e.v[1] << ", " << e.v[2] << "}" << endl;
  }
#endif
  mint ans{calc(V) * C.fact[N]};
  cout << ans << endl;
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 6/2/2020, 3:28:11 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
using ld = long double;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(Mint const &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(Mint const &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(Mint const &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(Mint const &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(Mint const &a) const { return Mint(*this) += a; }
  Mint operator-(Mint const &a) const { return Mint(*this) -= a; }
  Mint operator*(Mint const &a) const { return Mint(*this) *= a; }
  Mint operator/(Mint const &a) const { return Mint(*this) /= a; }
  bool operator<(Mint const &a) const { return x < a.x; }
  bool operator<=(Mint const &a) const { return x <= a.x; }
  bool operator>(Mint const &a) const { return x > a.x; }
  bool operator>=(Mint const &a) const { return x >= a.x; }
  bool operator==(Mint const &a) const { return x == a.x; }
  bool operator!=(Mint const &a) const { return !(*this == a); }
  Mint power(ll N) const
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, Mint<MOD> const &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, Mint<MOD> const &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, Mint<MOD> const &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, Mint<MOD> const &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, Mint<MOD> const &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1'000'000'000'000'010LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "-1" << endl;
  exit(0);
}
// ----- main() -----

int main()
{
  string S, T;
  cin >> S >> T;
  ll N{static_cast<ll>(S.size())};
  S = S + S;
  vector<vector<ll>> V(S.size(), vector<ll>(26, -1));
  for (auto i = 2 * N - 2; i >= 0; --i)
  {
    V[i] = V[i + 1];
    V[i][S[i + 1] - 'a'] = i + 1;
  }
  ll ans{0};
  ll now{0};
  if (T[0] == S[0])
  {
    T.erase(T.begin());
  }
  for (auto e : T)
  {
    ll next{V[now][e - 'a']};
    if (next == -1)
    {
      No();
    }
    ans += next;
    now = next % N;
  }
  cout << ans + 1 << endl;
}
<commit_msg>submit E.cpp to 'E - Strings of Impurity' (abc138) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 6/2/2020, 3:28:11 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
using ld = long double;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(Mint const &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(Mint const &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(Mint const &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(Mint const &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(Mint const &a) const { return Mint(*this) += a; }
  Mint operator-(Mint const &a) const { return Mint(*this) -= a; }
  Mint operator*(Mint const &a) const { return Mint(*this) *= a; }
  Mint operator/(Mint const &a) const { return Mint(*this) /= a; }
  bool operator<(Mint const &a) const { return x < a.x; }
  bool operator<=(Mint const &a) const { return x <= a.x; }
  bool operator>(Mint const &a) const { return x > a.x; }
  bool operator>=(Mint const &a) const { return x >= a.x; }
  bool operator==(Mint const &a) const { return x == a.x; }
  bool operator!=(Mint const &a) const { return !(*this == a); }
  Mint power(ll N) const
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, Mint<MOD> const &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, Mint<MOD> const &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, Mint<MOD> const &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, Mint<MOD> const &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, Mint<MOD> const &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1'000'000'000'000'010LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "-1" << endl;
  exit(0);
}
// ----- main() -----

int main()
{
  string S, T;
  cin >> S >> T;
  ll N{static_cast<ll>(S.size())};
  S = S + S;
  vector<vector<ll>> V(S.size(), vector<ll>(26, -1));
  for (auto i = 2 * N - 2; i >= 0; --i)
  {
    V[i] = V[i + 1];
    V[i][S[i + 1] - 'a'] = i + 1;
  }
  ll ans{0};
  ll now{0};
  if (T[0] == S[0])
  {
    T.erase(T.begin());
  }
  for (auto e : T)
  {
    ll next{V[now][e - 'a']};
    if (next == -1)
    {
      No();
    }
    ans += next - now;
    now = next % N;
  }
  cout << ans + 1 << endl;
}
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/1/19 21:02:40
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
cpp_int lcm(cpp_int x, cpp_int y) { return x / gcd(x, y) * y; }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

int main()
{
  int N;
  cin >> N;
  vector<cpp_int> A(N);
  for (auto i = 0; i < N; ++i)
  {
    cin >> A[i];
  }
  if (N == 1)
  {
    cout << 1 << endl;
    return 0;
  }
  cpp_int L{lcm(A[0], A[1])};
  for (auto i = 2; i < N; ++i)
  {
    L = lcm(L, A[i]);
  }
  vector<cpp_int> B(N);
  for (auto i = 0; i < N; ++i)
  {
    B[i] = L / A[i];
  }
  cpp_int sum{accumulate(B.begin(), B.end(), cpp_int{0})};
  cout << sum % MOD << endl;
}
<commit_msg>submit E.cpp to 'E - Flatten' (abc152) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/1/19 21:02:40
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1000000007LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
cpp_int gcd(cpp_int x, cpp_int y) { return y ? gcd(y, x % y) : x; }
cpp_int lcm(cpp_int x, cpp_int y) { return x / gcd(x, y) * y; }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

int main()
{
  int N;
  cin >> N;
  vector<cpp_int> A(N);
  for (auto i = 0; i < N; ++i)
  {
    cin >> A[i];
  }
  if (N == 1)
  {
    cout << 1 << endl;
    return 0;
  }
  cpp_int L{lcm(A[0], A[1])};
  for (auto i = 2; i < N; ++i)
  {
    L = lcm(L, A[i]);
  }
  vector<cpp_int> B(N);
  for (auto i = 0; i < N; ++i)
  {
    B[i] = L / A[i];
  }
  cpp_int sum{accumulate(B.begin(), B.end(), cpp_int{0})};
  cout << sum % MOD << endl;
}
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 3/21/2020, 10:21:50 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{2LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint &operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint &operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

class Solve
{
  int N;
  vector<int> V;
  bool zero_two;
  mint ans;

public:
  Solve(int N, vector<int> V) : N{N}, V{V}
  {
    zero_two_init();
    two_zero_conv();
    calc_ans();
    flush();
  }

private:
  void flush()
  {
    if (zero_two && ans == 1)
    {
      cout << 2 << endl;
    }
    else
    {
      cout << ans << endl;
    }
  }

  void two_zero_conv()
  {
    for (auto i = 0; i < N; ++i)
    {
      if (V[i] == 2)
      {
        V[i] = 0;
      }
    }
#if DEBUG == 1
    for (auto x : V)
    {
      cerr << x;
    }
    cerr << endl;
#endif
  }

  void calc_ans()
  {
    vector<mint> C(N);
    C[0] = 1;
    for (auto i = 1; i < N; ++i)
    {
      C[i] *= C[0] * (N - i);
#if DEBUG == 1
      cerr << "C[" << i << "] = " << C[i] << endl;
#endif
      C[i] /= i;
#if DEBUG == 1
      cerr << "C[" << i << "] = " << C[i] << endl;
#endif
    }
    ans = 0;
    for (auto i = 0; i < N; ++i)
    {
      ans += C[i] * V[i];
    }
  }

  void zero_two_init()
  {
    zero_two = true;
    for (auto i = 0; i < N; ++i)
    {
      if (V[i] == 1)
      {
        zero_two = false;
        break;
      }
    }
    if (zero_two)
    {
      for (auto i = 0; i < N; ++i)
      {
        if (V[i] == 2)
        {
          V[i] = 1;
        }
      }
    }
  }
};

int main()
{
  int N;
  cin >> N;
  vector<int> A(N);
  string S;
  cin >> S;
  for (auto i = 0; i < N; ++i)
  {
    A[i] = S[i] - '0';
    A[i]--;
  }
  Solve solve(N, A);
}
<commit_msg>tried B.cpp to 'B'<commit_after>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 3/21/2020, 10:21:50 PM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::multiprecision::cpp_int;
using ll = long long;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{2LL};
// constexpr ll MOD{998244353LL}; // be careful
constexpr ll MAX_SIZE{3000010LL};
// constexpr ll MAX_SIZE{30000010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
void ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
  }
}
template <typename T>
void ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
  }
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint &operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint &operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
template <typename T>
T gcd(T x, T y) { return y ? gcd(y, x % y) : x; }
template <typename T>
T lcm(T x, T y) { return x / gcd(x, y) * y; }
// ----- for C++17 -----
template <typename T>
int popcount(T x) // C++20
{
  int ans{0};
  while (x != 0)
  {
    ans += x & 1;
    x >>= 1;
  }
  return ans;
}
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----

class Solve
{
  int N;
  vector<int> V;
  bool zero_two;
  mint ans;

public:
  Solve(int N, vector<int> V) : N{N}, V{V}
  {
    zero_two_init();
    two_zero_conv();
    calc_ans();
    flush();
  }

private:
  void flush()
  {
    if (zero_two && ans == 1)
    {
      cout << 2 << endl;
    }
    else
    {
      cout << ans << endl;
    }
  }

  void two_zero_conv()
  {
    for (auto i = 0; i < N; ++i)
    {
      if (V[i] == 2)
      {
        V[i] = 0;
      }
    }
#if DEBUG == 1
    for (auto x : V)
    {
      cerr << x;
    }
    cerr << endl;
#endif
  }

  void calc_ans()
  {
    vector<mint> C(N);
    C[0] = 1;
    for (auto i = 1; i < N; ++i)
    {
      C[i] *= C[i - 1] * (N - i);
#if DEBUG == 1
      cerr << "C[" << i << "] = " << C[i] << endl;
#endif
      C[i] /= i;
#if DEBUG == 1
      cerr << "C[" << i << "] = " << C[i] << endl;
#endif
    }
    ans = 0;
    for (auto i = 0; i < N; ++i)
    {
      ans += C[i] * V[i];
    }
  }

  void zero_two_init()
  {
    zero_two = true;
    for (auto i = 0; i < N; ++i)
    {
      if (V[i] == 1)
      {
        zero_two = false;
        break;
      }
    }
    if (zero_two)
    {
      for (auto i = 0; i < N; ++i)
      {
        if (V[i] == 2)
        {
          V[i] = 1;
        }
      }
    }
  }
};

int main()
{
  int N;
  cin >> N;
  vector<int> A(N);
  string S;
  cin >> S;
  for (auto i = 0; i < N; ++i)
  {
    A[i] = S[i] - '0';
    A[i]--;
  }
  Solve solve(N, A);
}
<|endoftext|>


<commit_before>
#include "../src/AtomicStack.h"
#include <thread>

using namespace peloton;
using namespace index;

void BasicTest() {
  dbg_printf("========== Basic Test ==========\n");

  AtomicStack<uint64_t> as{};

  for(uint64_t i = 0;i < 100;i++) {
    as.Push(i);
  }

  for(uint64_t i = 99;i >= 0;i--) {
    uint64_t top;
    as.Pop(top);

    assert(top == i);
  }

  return;
}

void ThreadTest(int thread_num, int op_num) {
  dbg_printf("========== Thread Test ==========\n");

  AtomicStack<int> as{};

  auto push_func = [&as, thread_num, op_num](uint64_t id) {
                     for(int i = static_cast<int>(id);i < thread_num * op_num;i += thread_num) {
                       as.Push(i);
                     }
                   };

  // We use this to count what we have fetched from the stack
  std::atomic<int> sum;
  sum.store(0);

  auto pop_func = [&as, &sum, thread_num, op_num](uint64_t id) {
                    for(int i = 0;i < op_num;i++) {
                      int data;
                      as.Pop(data));

                      // Atomically adding the poped value onto the atomic
                      sum.fetch_add(data);
                    }
                  };

  StartThreads(thread_num, push_func);
  StartThreads(thread_num, pop_func);

  dbg_printf("Sum = %d\n", sum.load());
  assert(sum.load() == (op_num * thread_num));

  return;
}

int main() {
  BasicTest();
  ThreadTest();

  return 0;
}
<commit_msg>Fix compilation error<commit_after>
#include "../src/AtomicStack.h"
#include <thread>

using namespace peloton;
using namespace index;

void BasicTest() {
  dbg_printf("========== Basic Test ==========\n");

  AtomicStack<uint64_t> as{};

  for(uint64_t i = 0;i < 100;i++) {
    as.Push(i);
  }

  for(uint64_t i = 99;i >= 0;i--) {
    uint64_t top;
    as.Pop(top);

    assert(top == i);
  }

  return;
}

void ThreadTest(int thread_num, int op_num) {
  dbg_printf("========== Thread Test ==========\n");

  AtomicStack<int> as{};

  auto push_func = [&as, thread_num, op_num](uint64_t id) {
                     for(int i = static_cast<int>(id);i < thread_num * op_num;i += thread_num) {
                       as.Push(i);
                     }
                   };

  // We use this to count what we have fetched from the stack
  std::atomic<int> sum;
  sum.store(0);

  auto pop_func = [&as, &sum, thread_num, op_num](uint64_t id) {
                    for(int i = 0;i < op_num;i++) {
                      int data;
                      as.Pop(data);

                      // Atomically adding the poped value onto the atomic
                      sum.fetch_add(data);
                    }
                  };

  StartThreads(thread_num, push_func);
  StartThreads(thread_num, pop_func);

  dbg_printf("Sum = %d\n", sum.load());
  assert(sum.load() == (op_num * thread_num));

  return;
}

int main() {
  BasicTest();
  ThreadTest();

  return 0;
}
<|endoftext|>