<commit_before>// Copyright (C) 2010 - 2015 by Pedro Mendes, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and The University
// of Manchester.
// All rights reserved.

// Copyright (C) 2009 by Pedro Mendes, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

#include "copasi.h"

#include "CCopasiRootContainer.h"
#include "ArtisticLicense.h"
#include "copasi/function/CFunctionDB.h"
#include "copasi/commandline/CConfigurationFile.h"
#include "copasi/commandline/COptions.h"
#include "copasi/function/CFunction.h"
#include "copasi/function/CEvaluationNodeOperator.h"
#include "copasi/function/CEvaluationNodeVariable.h"
#include "copasi/function/CEvaluationNodeConstant.h"
#include "copasi/utilities/CUnit.h"

#include <copasi/layout/CLGlobalRenderInformation.h>

extern CCopasiVector<CLGlobalRenderInformation>* DEFAULT_STYLES;

extern CCopasiRootContainer * pRootContainer;

/**
 * The default constructor should be private so that nobody can create
 * new instances of the root container.
 * The only way to create a root container is through the static init
 * method.
 */
CCopasiRootContainer::CCopasiRootContainer(const bool & withGUI):
  CCopasiContainer("Root", NULL, "CN", CCopasiObject::Root),
  mKeyFactory(),
  mpUnknownResource(NULL),
  mpFunctionList(NULL),
  mpUnitList(NULL),
  mpConfiguration(NULL),
  mpDataModelList(NULL),
  mWithGUI(withGUI),
  mpUndefined(NULL)
{}

// Destructor
CCopasiRootContainer::~CCopasiRootContainer()
{
  // save and delete the configuration
  if (mpConfiguration != NULL &&
      mWithGUI)
    {
      mpConfiguration->save();
    }

  // delete the configuration
  pdelete(mpConfiguration);

  // delete the unkown resource
  pdelete(mpUnknownResource);

  // delete the model list
  pdelete(mpDataModelList);

  // delete the function list
  pdelete(mpFunctionList);

  // delete the unit list
  pdelete(mpUnitList);

  // delete the undefined and the unsupported delay function
  pdelete(mpUndefined);

  // delete other static variables
  pdelete(DEFAULT_STYLES);
}

/**
 * This method creates the only root container.
 */
void CCopasiRootContainer::init(int argc, char *argv[], const bool & withGUI)
{
  COptions::init(argc, argv);

  CCopasiMessage::setIsGUI(withGUI);

  if (pRootContainer == NULL)
    pRootContainer = new CCopasiRootContainer(withGUI);

  if (pRootContainer != NULL)
    pRootContainer->initializeChildren();
}

// static
void CCopasiRootContainer::destroy()
{
  if (pRootContainer != NULL)
    {
      delete pRootContainer;
      pRootContainer = NULL;
    }

  pdelete(CFunctionParameterMap::pUnmappedObject);

  COptions::cleanup();
}

void CCopasiRootContainer::initializeChildren()
{
  mpUnknownResource = new CMIRIAMResource("Unknown Resource");
  mpUnknownResource->setMIRIAMDisplayName("-- select --");
  mpUnknownResource->setMIRIAMURI("urn:miriam:unknown");

  mpFunctionList = new CFunctionDB("FunctionDB", this);
  mpFunctionList->load();

  mpDataModelList = new CCopasiVector<CCopasiDataModel>("ModelList", this);

  mpConfiguration = new CConfigurationFile;
  mpConfiguration->load();

  mpUndefined = new CFunction("undefined", this);
  mpUndefined->setInfix("nan");
  mpUndefined->compile();

  mpUnitList = new CCopasiVectorN< CUnit >;
  CUnit::updateSIUnits(*mpUnitList, CUnit::Avogadro);
}

// static
const CCopasiContainer * CCopasiRootContainer::getRoot()
{
  return pRootContainer;
}

// static
CConfigurationFile * CCopasiRootContainer::getConfiguration()
{
  return pRootContainer->mpConfiguration;
}

// static
CFunctionDB * CCopasiRootContainer::getFunctionList()
{
  return pRootContainer->mpFunctionList;
}

// static
CCopasiVector< CCopasiDataModel > * CCopasiRootContainer::getDatamodelList()
{
  return pRootContainer->mpDataModelList;
}

// static
CCopasiVectorN< CUnit > * CCopasiRootContainer::getUnitList()
{
  return pRootContainer->mpUnitList;
}

// static
const CUnit * CCopasiRootContainer::getUnitFromSymbol(const std::string symbol)
{
  CCopasiVectorN< CUnit >::const_iterator it = pRootContainer->mpUnitList->begin();
  CCopasiVectorN< CUnit >::const_iterator end = pRootContainer->mpUnitList->end();

  const CUnit * pUnit = NULL;

  for (; it != end; ++it)
    {
      if (symbol == (*it)->getSymbol())
        {
          pUnit = *it;
          break;
        }
    }

  return pUnit;
}

// static
CCopasiDataModel * CCopasiRootContainer::addDatamodel()
{
  CCopasiDataModel* pDataModel = new CCopasiDataModel(pRootContainer->mWithGUI);
  pRootContainer->mpDataModelList->add(pDataModel, true);
  return pDataModel;
}

// static
CFunction * CCopasiRootContainer::getUndefinedFunction()
{
  return pRootContainer->mpUndefined;
}

//static
CKeyFactory* CCopasiRootContainer::getKeyFactory()
{
  return &pRootContainer->mKeyFactory;
}

// static
const CMIRIAMResource & CCopasiRootContainer::getUnknownMiriamResource()
{
  return *pRootContainer->mpUnknownResource;
}

// static
bool CCopasiRootContainer::removeDatamodel(const CCopasiDataModel * pDatamodel)
{
  if (!pDatamodel)
    {
      return false;
    }

  pRootContainer->mpDataModelList->remove((CCopasiDataModel *)pDatamodel);
  pdelete(pDatamodel);

  return true;
}

// static
bool CCopasiRootContainer::removeDatamodel(const unsigned C_INT32 index)
{
  const CCopasiDataModel* pDatamodel = (*CCopasiRootContainer::getDatamodelList())[index];
  return CCopasiRootContainer::removeDatamodel(pDatamodel);
}

// static
const char * CCopasiRootContainer::getLicenseTxt()
{
  return CopasiLicenseTxt;
}

// static
const char * CCopasiRootContainer::getLicenseHTML()
{
  return CopasiLicenseHtml;
}
<commit_msg>Fixed the Root Container's mpUnitList to know it's name and parent.<commit_after>// Copyright (C) 2010 - 2015 by Pedro Mendes, Virginia Tech Intellectual
// Properties, Inc., University of Heidelberg, and The University
// of Manchester.
// All rights reserved.

// Copyright (C) 2009 by Pedro Mendes, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

#include "copasi.h"

#include "CCopasiRootContainer.h"
#include "ArtisticLicense.h"
#include "copasi/function/CFunctionDB.h"
#include "copasi/commandline/CConfigurationFile.h"
#include "copasi/commandline/COptions.h"
#include "copasi/function/CFunction.h"
#include "copasi/function/CEvaluationNodeOperator.h"
#include "copasi/function/CEvaluationNodeVariable.h"
#include "copasi/function/CEvaluationNodeConstant.h"
#include "copasi/utilities/CUnit.h"

#include <copasi/layout/CLGlobalRenderInformation.h>

extern CCopasiVector<CLGlobalRenderInformation>* DEFAULT_STYLES;

extern CCopasiRootContainer * pRootContainer;

/**
 * The default constructor should be private so that nobody can create
 * new instances of the root container.
 * The only way to create a root container is through the static init
 * method.
 */
CCopasiRootContainer::CCopasiRootContainer(const bool & withGUI):
  CCopasiContainer("Root", NULL, "CN", CCopasiObject::Root),
  mKeyFactory(),
  mpUnknownResource(NULL),
  mpFunctionList(NULL),
  mpUnitList(NULL),
  mpConfiguration(NULL),
  mpDataModelList(NULL),
  mWithGUI(withGUI),
  mpUndefined(NULL)
{}

// Destructor
CCopasiRootContainer::~CCopasiRootContainer()
{
  // save and delete the configuration
  if (mpConfiguration != NULL &&
      mWithGUI)
    {
      mpConfiguration->save();
    }

  // delete the configuration
  pdelete(mpConfiguration);

  // delete the unkown resource
  pdelete(mpUnknownResource);

  // delete the model list
  pdelete(mpDataModelList);

  // delete the function list
  pdelete(mpFunctionList);

  // delete the unit list
  pdelete(mpUnitList);

  // delete the undefined and the unsupported delay function
  pdelete(mpUndefined);

  // delete other static variables
  pdelete(DEFAULT_STYLES);
}

/**
 * This method creates the only root container.
 */
void CCopasiRootContainer::init(int argc, char *argv[], const bool & withGUI)
{
  COptions::init(argc, argv);

  CCopasiMessage::setIsGUI(withGUI);

  if (pRootContainer == NULL)
    pRootContainer = new CCopasiRootContainer(withGUI);

  if (pRootContainer != NULL)
    pRootContainer->initializeChildren();
}

// static
void CCopasiRootContainer::destroy()
{
  if (pRootContainer != NULL)
    {
      delete pRootContainer;
      pRootContainer = NULL;
    }

  pdelete(CFunctionParameterMap::pUnmappedObject);

  COptions::cleanup();
}

void CCopasiRootContainer::initializeChildren()
{
  mpUnknownResource = new CMIRIAMResource("Unknown Resource");
  mpUnknownResource->setMIRIAMDisplayName("-- select --");
  mpUnknownResource->setMIRIAMURI("urn:miriam:unknown");

  mpFunctionList = new CFunctionDB("FunctionDB", this);
  mpFunctionList->load();

  mpDataModelList = new CCopasiVector<CCopasiDataModel>("ModelList", this);

  mpConfiguration = new CConfigurationFile;
  mpConfiguration->load();

  mpUndefined = new CFunction("undefined", this);
  mpUndefined->setInfix("nan");
  mpUndefined->compile();

  mpUnitList = new CCopasiVectorN< CUnit >("UnitList", this);
  CUnit::updateSIUnits(*mpUnitList, CUnit::Avogadro);
}

// static
const CCopasiContainer * CCopasiRootContainer::getRoot()
{
  return pRootContainer;
}

// static
CConfigurationFile * CCopasiRootContainer::getConfiguration()
{
  return pRootContainer->mpConfiguration;
}

// static
CFunctionDB * CCopasiRootContainer::getFunctionList()
{
  return pRootContainer->mpFunctionList;
}

// static
CCopasiVector< CCopasiDataModel > * CCopasiRootContainer::getDatamodelList()
{
  return pRootContainer->mpDataModelList;
}

// static
CCopasiVectorN< CUnit > * CCopasiRootContainer::getUnitList()
{
  return pRootContainer->mpUnitList;
}

// static
const CUnit * CCopasiRootContainer::getUnitFromSymbol(const std::string symbol)
{
  CCopasiVectorN< CUnit >::const_iterator it = pRootContainer->mpUnitList->begin();
  CCopasiVectorN< CUnit >::const_iterator end = pRootContainer->mpUnitList->end();

  const CUnit * pUnit = NULL;

  for (; it != end; ++it)
    {
      if (symbol == (*it)->getSymbol())
        {
          pUnit = *it;
          break;
        }
    }

  return pUnit;
}

// static
CCopasiDataModel * CCopasiRootContainer::addDatamodel()
{
  CCopasiDataModel* pDataModel = new CCopasiDataModel(pRootContainer->mWithGUI);
  pRootContainer->mpDataModelList->add(pDataModel, true);
  return pDataModel;
}

// static
CFunction * CCopasiRootContainer::getUndefinedFunction()
{
  return pRootContainer->mpUndefined;
}

//static
CKeyFactory* CCopasiRootContainer::getKeyFactory()
{
  return &pRootContainer->mKeyFactory;
}

// static
const CMIRIAMResource & CCopasiRootContainer::getUnknownMiriamResource()
{
  return *pRootContainer->mpUnknownResource;
}

// static
bool CCopasiRootContainer::removeDatamodel(const CCopasiDataModel * pDatamodel)
{
  if (!pDatamodel)
    {
      return false;
    }

  pRootContainer->mpDataModelList->remove((CCopasiDataModel *)pDatamodel);
  pdelete(pDatamodel);

  return true;
}

// static
bool CCopasiRootContainer::removeDatamodel(const unsigned C_INT32 index)
{
  const CCopasiDataModel* pDatamodel = (*CCopasiRootContainer::getDatamodelList())[index];
  return CCopasiRootContainer::removeDatamodel(pDatamodel);
}

// static
const char * CCopasiRootContainer::getLicenseTxt()
{
  return CopasiLicenseTxt;
}

// static
const char * CCopasiRootContainer::getLicenseHTML()
{
  return CopasiLicenseHtml;
}
<|endoftext|>


<commit_before>/*
This file is part of cphVB and copyright (c) 2012 the cphVB team:
http://cphvb.bitbucket.org

cphVB is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

cphVB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the
GNU Lesser General Public License along with cphVB.

If not, see <http://www.gnu.org/licenses/>.
*/
#include <cphvb.h>
#include <cphvb_compute.h>

//This function protects against arrays that have superfluos dimensions,
// which could negatively affect computation speed
void cphvb_compact_dimensions(cphvb_tstate* state)
{
	cphvb_index i, j;

	//As long as the inner dimension has length == 1, we can safely ignore it
	while(state->ndim > 1 && (state->shape[state->ndim - 1] == 1 || state->shape[state->ndim - 1] == 0))
		state->ndim--;

	//We can also remove dimensions inside the seqence
	for(i = state->ndim-2; i >= 0; i--)
	{
		if (state->shape[i] == 1 || state->shape[i] == 0)
		{
			state->ndim--;
			memcpy(&state->shape[i], &state->shape[i+1], (state->ndim - i) * sizeof(cphvb_index));
			for(j = 0; j < state->noperands; j++)
				memcpy(&state->stride[j][i], &state->stride[j][i+1], (state->ndim - i) * sizeof(cphvb_index));
		}
	}
}

void cphvb_tstate_reset( cphvb_tstate *state, cphvb_instruction *instr ) {
	// As all arrays have the same dimensions, we keep a single shared shape
	cphvb_index i, blocksize;

	state->ndim = instr->operand[0]->ndim;
	state->noperands = cphvb_operands(instr->opcode);
	blocksize = state->ndim * sizeof(cphvb_index);
	memcpy(state->shape, instr->operand[0]->shape, blocksize);

	for(i = 0; i < state->noperands; i++) {
		if (!cphvb_is_constant(instr->operand[i]))
		{
			memcpy(state->stride[i], instr->operand[i]->stride, blocksize);
			state->start[i] = instr->operand[i]->start;
		}
	}

	cphvb_compact_dimensions(state);
}

cphvb_error cphvb_compute_apply( cphvb_instruction *instr ) {

    cphvb_computeloop comp = cphvb_compute_get( instr );
    cphvb_tstate state;
    cphvb_tstate_reset( &state, instr );

    if (comp == NULL) {
        return CPHVB_TYPE_NOT_SUPPORTED;
    } else {
        return comp( instr, &state, 0 );
    }

}

<commit_msg>Fixed merge-leftover in Makefile of mcache.<commit_after>/*
This file is part of cphVB and copyright (c) 2012 the cphVB team:
http://cphvb.bitbucket.org

cphVB is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3
of the License, or (at your option) any later version.

cphVB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the
GNU Lesser General Public License along with cphVB.

If not, see <http://www.gnu.org/licenses/>.
*/
#include <cphvb.h>
#include <cphvb_compute.h>

//This function protects against arrays that have superfluos dimensions,
// which could negatively affect computation speed
void cphvb_compact_dimensions(cphvb_tstate* state)
{
	cphvb_index i, j;

	//As long as the inner dimension has length == 1, we can safely ignore it
	while(state->ndim > 1 && (state->shape[state->ndim - 1] == 1 || state->shape[state->ndim - 1] == 0))
		state->ndim--;

	//We can also remove dimensions inside the seqence
	for(i = state->ndim-2; i >= 0; i--)
	{
		if (state->shape[i] == 1 || state->shape[i] == 0)
		{
			state->ndim--;
			memcpy(&state->shape[i], &state->shape[i+1], (state->ndim - i) * sizeof(cphvb_index));
			for(j = 0; j < state->noperands; j++)
				memcpy(&state->stride[j][i], &state->stride[j][i+1], (state->ndim - i) * sizeof(cphvb_index));
		}
	}
}

void cphvb_tstate_reset( cphvb_tstate *state, cphvb_instruction *instr ) {

	// As all arrays have the same dimensions, we keep a single shared shape
	cphvb_index i, blocksize;

	state->ndim         = instr->operand[0]->ndim;
	state->noperands    = cphvb_operands(instr->opcode);
	blocksize           = state->ndim * sizeof(cphvb_index);
	memcpy(state->shape, instr->operand[0]->shape, blocksize);

	for(i = 0; i < state->noperands; i++) {
        if (!cphvb_is_constant(instr->operand[i])) {
			memcpy(state->stride[i], instr->operand[i]->stride, blocksize);
			state->start[i] = instr->operand[i]->start;
		}
	}

	cphvb_compact_dimensions(state);
}

/**
 * Execute an instruction using the optimization traversal.
 *
 * @param instr Instruction to execute.
 * @return Status of execution
 */
cphvb_error cphvb_compute_apply( cphvb_instruction *instr ) {

    cphvb_computeloop comp = cphvb_compute_get( instr );
    cphvb_tstate state;
    cphvb_tstate_reset( &state, instr );

    if (comp == NULL) {
        return CPHVB_TYPE_NOT_SUPPORTED;
    } else {
        return comp( instr, &state );
    }

}

<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 5/20/2020, 11:04:10 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{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++() { 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 << "-1" << endl;
  exit(0);
}
// ----- main() -----

struct Point
{
  ll x, y;
};

ostream &operator<<(ostream &os, Point const &p)
{
  return os << p.x << " " << p.y;
}

Point &operator+=(Point &p, Point const &q)
{
  p.x += q.x;
  p.y += q.y;
  return p;
}

Point operator-(Point const &p, Point const &q)
{
  return {p.x - q.x, p.y - q.y};
}

bool operator==(Point const &p, Point const &q)
{
  return (p.x == q.x && p.y == q.y);
}

bool operator!=(Point const &p, Point const &q)
{
  return !(p == q);
}

class Solve
{
  ll K;
  Point G;
  vector<Point> V;

public:
  Solve()
  {
    cin >> K >> G.x >> G.y;
  }

  void flush()
  {
    if ((G.x + G.y) % 2 == 1 && K % 2 == 0)
    {
      No();
    }
    golf();
    cout << V.size() << endl;
    for (auto const &p : V)
    {
      cout << p << endl;
    }
  }

private:
  void golf()
  {
    Point now({0, 0});
    while (now != G)
    {
      auto delta{shot(G - now)};
      now += delta;
      V.push_back(now);
    }
  }

  Point shot(Point q)
  {
    auto const &x{q.x};
    auto const &y{q.y};
    if (x < 0)
    {
      q.x *= -1;
      Point p{shot(q)};
      p.x *= -1;
      return p;
    }
    if (y < 0)
    {
      q.y *= -1;
      Point p{shot(q)};
      p.y *= -1;
      return p;
    }
    if (x > y)
    {
      swap(q.x, q.y);
      Point p{shot(q)};
      swap(p.x, p.y);
      return p;
    }
    auto sum{x + y};
    if (sum == K)
    {
      return {x, y};
    }
    if (sum > 2 * K || sum % 2 == 1)
    {
      auto t{min(x, K)};
      return {t, K - t};
    }
    auto Z{K - sum / 2};
    return {x - (K - Z), y + Z};
  }
};

int main()
{
  Solve solve;
  solve.flush();
}
<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 5/20/2020, 11:04:10 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{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++() { 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 << "-1" << endl;
  exit(0);
}
// ----- main() -----

struct Point
{
  ll x, y;
};

ostream &operator<<(ostream &os, Point const &p)
{
  return os << p.x << " " << p.y;
}

Point &operator+=(Point &p, Point const &q)
{
  p.x += q.x;
  p.y += q.y;
  return p;
}

Point operator-(Point const &p, Point const &q)
{
  return {p.x - q.x, p.y - q.y};
}

bool operator==(Point const &p, Point const &q)
{
  return (p.x == q.x && p.y == q.y);
}

bool operator!=(Point const &p, Point const &q)
{
  return !(p == q);
}

class Solve
{
  ll K;
  Point G;
  vector<Point> V;

public:
  Solve()
  {
    cin >> K >> G.x >> G.y;
  }

  void flush()
  {
    if ((G.x + G.y) % 2 == 1 && K % 2 == 0)
    {
      No();
    }
    golf();
    cout << V.size() << endl;
    for (auto const &p : V)
    {
      cout << p << endl;
    }
  }

private:
  void golf()
  {
    Point now({0, 0});
    while (now != G)
    {
      auto delta{shot(G - now)};
      now += delta;
      V.push_back(now);
    }
  }

  Point shot(Point q)
  {
    auto const &x{q.x};
    auto const &y{q.y};
    if (x < 0)
    {
      q.x *= -1;
      Point p{shot(q)};
      p.x *= -1;
      return p;
    }
    if (y < 0)
    {
      q.y *= -1;
      Point p{shot(q)};
      p.y *= -1;
      return p;
    }
    if (x > y)
    {
      swap(q.x, q.y);
      Point p{shot(q)};
      swap(p.x, p.y);
      return p;
    }
    auto sum{x + y};
    if (sum == K)
    {
      return q;
    }
    if (sum > 2 * K || sum % 2 == 1)
    {
      auto t{min(x, K)};
      return {t, K - t};
    }
    auto Z{K - sum / 2};
    return {
        y + Z,
        x - (K - Z)};
  }
};

int main()
{
  Solve solve;
  solve.flush();
}
<|endoftext|>


<commit_before>#ifndef __TERMINAL_DATATYPES_HPP__
#define __TERMINAL_DATATYPES_HPP__

#include <cstdint>
#include <cstddef>

namespace TERMINAL
{
	typedef int8_t		T_SINT8;
	typedef uint8_t		T_UINT8;
	typedef int16_t		T_SINT16;
	typedef uint16_t	T_UINT16;
	typedef int32_t		T_SINT32;
	typedef uint32_t	T_UINT32;
	typedef float		T_FLOAT32;

	typedef size_t		T_MEMSIZE;

	typedef T_UINT8		T_BYTE;

	const T_MEMSIZE T_SIZEOF_SINT8 = sizeof( T_SINT8 );
	const T_MEMSIZE T_SIZEOF_UINT8 = sizeof( T_UINT8 );
	const T_MEMSIZE T_SIZEOF_SINT16 = sizeof( T_SINT16 );
	const T_MEMSIZE T_SIZEOF_UINT16 = sizeof( T_UINT16 );
	const T_MEMSIZE T_SIZEOF_SINT32 = sizeof( T_SINT32 );
	const T_MEMSIZE T_SIZEOF_UINT32 = sizeof( T_UINT32 );
	const T_MEMSIZE T_SIZEOF_FLOAT32 = sizeof( T_FLOAT32 );
	const T_MEMSIZE T_SIZEOF_MEMSIZE = sizeof( T_MEMSIZE );
	const T_MEMSIZE T_SIZEOF_BYTE = sizeof( T_BYTE );

	const int T_INVALID_SOCKET = -1;
}

#endif // __TERMINAL_DATATYPES_HPP__

<commit_msg>Added a 64-bit signed and unsigned integer type<commit_after>#ifndef __TERMINAL_DATATYPES_HPP__
#define __TERMINAL_DATATYPES_HPP__

#include <cstdint>
#include <cstddef>

namespace TERMINAL
{
	typedef int8_t		T_SINT8;
	typedef uint8_t		T_UINT8;
	typedef int16_t		T_SINT16;
	typedef uint16_t	T_UINT16;
	typedef int32_t		T_SINT32;
	typedef uint32_t	T_UINT32;
	typedef int64_t		T_SINT64;
	typedef uint64_t	T_UINT64;
	typedef float		T_FLOAT32;

	typedef size_t		T_MEMSIZE;

	typedef T_UINT8		T_BYTE;

	const T_MEMSIZE T_SIZEOF_SINT8 = sizeof( T_SINT8 );
	const T_MEMSIZE T_SIZEOF_UINT8 = sizeof( T_UINT8 );
	const T_MEMSIZE T_SIZEOF_SINT16 = sizeof( T_SINT16 );
	const T_MEMSIZE T_SIZEOF_UINT16 = sizeof( T_UINT16 );
	const T_MEMSIZE T_SIZEOF_SINT32 = sizeof( T_SINT32 );
	const T_MEMSIZE T_SIZEOF_UINT32 = sizeof( T_UINT32 );
	const T_MEMSIZE T_SIZEOF_FLOAT32 = sizeof( T_FLOAT32 );
	const T_MEMSIZE T_SIZEOF_MEMSIZE = sizeof( T_MEMSIZE );
	const T_MEMSIZE T_SIZEOF_BYTE = sizeof( T_BYTE );

	const int T_INVALID_SOCKET = -1;
}

#endif // __TERMINAL_DATATYPES_HPP__

<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 5/23/2020, 3:16: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;
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++() { 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
{
  ll N, K;
  vector<ll> A;
  set<ll> divisor;

public:
  Solve(ll N, ll K) : N{N}, K{K}, A(N)
  {
    for (auto i = 0; i < N; ++i)
    {
      cin >> A[i];
    }
    make_divisor();
    ll ans{1};
    for (auto x : divisor)
    {
      if (ok(x))
      {
        ch_max(ans, x);
      }
    }
    cout << ans << endl;
  }

private:
  bool ok(ll x)
  {
    vector<ll> R(N);
    for (auto i = 0; i < N; ++i)
    {
      R[i] = A[i] % x;
    }
    sort(R.begin(), R.end());
    ll sum{accumulate(R.begin(), R.end(), 0LL)};
    ll tmp{0};
    ll need{max(tmp, sum - tmp)};
    for (auto i = 0; i < N; ++i)
    {
      tmp += R[i];
      ch_min(need, max(tmp, sum - tmp));
    }
#if DEBUG == 1
    cerr << "x = " << x << ", sum = " << sum << ", need = " << need << endl;
#endif
    return need <= K;
  }

  void make_divisor()
  {
    ll sum{accumulate(A.begin(), A.end(), 0LL)};
    for (auto i = 1LL; i < 100000LL; ++i)
    {
      if (sum % i == 0)
      {
        divisor.insert(i);
        divisor.insert(sum / i);
      }
    }
  }
};

int main()
{
  ll N, K;
  cin >> N >> K;
  Solve solve(N, K);
}
<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 5/23/2020, 3:16: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;
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++() { 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
{
  ll N, K;
  vector<ll> A;
  set<ll> divisor;

public:
  Solve(ll N, ll K) : N{N}, K{K}, A(N)
  {
    for (auto i = 0; i < N; ++i)
    {
      cin >> A[i];
    }
    make_divisor();
    ll ans{1};
    for (auto x : divisor)
    {
      if (ok(x))
      {
        ch_max(ans, x);
      }
    }
    cout << ans << endl;
  }

private:
  bool ok(ll x)
  {
    vector<ll> R(N);
    for (auto i = 0; i < N; ++i)
    {
      R[i] = A[i] % x;
#if DEBUG == 1
      cerr << "R[" << i << "] = " << R[i] << endl;
#endif
    }
    sort(R.begin(), R.end());
    ll sum{accumulate(R.begin(), R.end(), 0LL)};
    ll tmp{0};
    ll need{max(tmp, sum - tmp)};
    for (auto i = 0; i < N; ++i)
    {
      tmp += R[i];
      ch_min(need, max(tmp, sum - tmp));
    }
#if DEBUG == 1
    cerr << "x = " << x << ", sum = " << sum << ", need = " << need << endl;
#endif
    return need <= K;
  }

  void make_divisor()
  {
    ll sum{accumulate(A.begin(), A.end(), 0LL)};
    for (auto i = 1LL; i < 100000LL; ++i)
    {
      if (sum % i == 0)
      {
        divisor.insert(i);
        divisor.insert(sum / i);
      }
    }
  }
};

int main()
{
  ll N, K;
  cin >> N >> K;
  Solve solve(N, K);
}
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/1/27 13:16:25
 * 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>;
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; }
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 int infty{10000000};
// 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 H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  for (auto i = 0; i < N; ++i)
  {
    cin >> A[i] >> B[i];
  }
  vector<int> dp(H + 1, infty);
  dp[0] = 0;
  for (auto i = 0; i < H; ++i)
  {
    for (auto j = 0; j < N; ++j)
    {
      if (i + A[j] <= H)
      {
        ch_min(dp[i + A[j]], dp[i] + B[i]);
      }
    }
  }
  cout << dp[H] << endl;
}
<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/1/27 13:16:25
 * 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>;
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; }
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 int infty{10000000};
// 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 H, N;
  cin >> H >> N;
  vector<int> A(N), B(N);
  for (auto i = 0; i < N; ++i)
  {
    cin >> A[i] >> B[i];
  }
  vector<int> dp(H + 2000, infty);
  dp[0] = 0;
  for (auto i = 0; i < H + 2000; ++i)
  {
    for (auto j = 0; j < N; ++j)
    {
      if (i + A[j] < H + 2000)
      {
        ch_min(dp[i + A[j]], dp[i] + B[i]);
      }
    }
  }
  cout << *min_element(dp.begin() + H, dp.end()) << endl;
}
<|endoftext|>


<commit_before>#ifndef OCCA_DEFINES_HEADER
#define OCCA_DEFINES_HEADER

#ifndef LINUX_OS
#  define LINUX_OS 1
#endif

#ifndef OSX_OS
#  define OSX_OS 2
#endif

#ifndef WINDOWS_OS
#  define WINDOWS_OS 4
#endif

#ifndef WINUX_OS
#  define WINUX_OS (LINUX_OS | WINDOWS_OS)
#endif

#ifndef OCCA_USING_VS
#  ifdef _MSC_VER
#    define OCCA_USING_VS 1
#  else
#    define OCCA_USING_VS 0
#  endif
#endif

#ifndef OCCA_OS
#  if defined(WIN32) || defined(WIN64)
#    if OCCA_USING_VS
#      define OCCA_OS WINDOWS_OS
#    else
#      define OCCA_OS WINUX_OS
#    endif
#  elif __APPLE__
#    define OCCA_OS OSX_OS
#  else
#    define OCCA_OS LINUX_OS
#  endif
#endif

#if OCCA_USING_VS
#  define OCCA_VS_VERSION _MSC_VER
#  include "vs/defines.hpp"
#endif

#ifndef __PRETTY_FUNCTION__
#  define __PRETTY_FUNCTION__ __FUNCTION__
#endif

#if defined(__x86_64__) || defined(_M_X64) // 64 Bit
#  define OCCA_64_BIT 1
#  define OCCA_32_BIT 0
#elif defined(__i386) || defined(_M_IX86) // 32 Bit
#  define OCCA_64_BIT 0
#  define OCCA_32_BIT 1
#elif defined(__ia64) || defined(__itanium__) || defined(_A_IA64) // Itanium
#  define OCCA_64_BIT 1
#  define OCCA_32_BIT 0
#endif

//---[ Checks and Info ]----------------
#ifndef OCCA_COMPILED_FOR_JULIA
#  define OCCA_THROW abort()
#else
#  define OCCA_THROW exit(1)
#endif

#define OCCA_EMPTY_FORCE_CHECK2( _expr , file , line , func )           \
  do {                                                                  \
    intptr_t expr = (_expr);                                            \
    if( !expr ){                                                        \
      std::cout << '\n'                                                 \
                << "---[ Error ]--------------------------------------------\n" \
                << "    File     : " << file << '\n'                    \
                << "    Function : " << func << '\n'                    \
                << "    Line     : " << line << '\n'                    \
                << "========================================================\n"; \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)

#define OCCA_FORCE_CHECK2( _expr , _msg , file , line , func )          \
  do {                                                                  \
    intptr_t expr = (_expr);                                            \
    if( !expr ){                                                        \
      std::cout << '\n'                                                 \
                << "---[ Error ]--------------------------------------------\n" \
                << "    File     : " << file << '\n'                    \
                << "    Function : " << func << '\n'                    \
                << "    Line     : " << line << '\n'                    \
                << "    Error    : " << _msg << '\n'                    \
                << "========================================================\n"; \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)

#define OCCA_EMPTY_FORCE_CHECK( _expr )  OCCA_EMPTY_FORCE_CHECK2( _expr , __FILE__ , __LINE__ , __PRETTY_FUNCTION__)
#define OCCA_FORCE_CHECK( _expr , _msg ) OCCA_FORCE_CHECK2( _expr , _msg , __FILE__ , __LINE__ , __PRETTY_FUNCTION__)

#if OCCA_CHECK_ENABLED
#  define OCCA_EMPTY_CHECK( _expr )  OCCA_EMPTY_FORCE_CHECK( _expr )
#  define OCCA_CHECK( _expr , _msg ) OCCA_FORCE_CHECK( _expr , _msg )
#else
#  define OCCA_EMPTY_CHECK( _expr )
#  define OCCA_CHECK( _expr , _msg )
#endif

#if (OCCA_OS & OSX_OS)
#  define OCCA_MEM_ALIGN 16
#else
#  define OCCA_MEM_ALIGN 16
#endif

//---[ Vectorization ]--------
#ifdef __MIC__
#  define OCCA_MIC 1
#else
#  define OCCA_MIC 0
#endif

#ifdef __AVX2__
#  define OCCA_AVX2 1
#else
#  define OCCA_AVX2 0
#endif

#ifdef __AVX__
#  define OCCA_AVX 1
#else
#  define OCCA_AVX 0
#endif

#ifdef __SSE4_2__
#  define OCCA_SSE4_2 1
#else
#  define OCCA_SSE4_2 0
#endif

#ifdef __SSE4_1__
#  define OCCA_SSE4_1 1
#else
#  define OCCA_SSE4_1 0
#endif

#if OCCA_SSE4_1 || OCCA_SSE4_2
#  define OCCA_SSE4 1
#else
#  define OCCA_SSE4 0
#endif

#ifdef __SSSE3__
#  define OCCA_SSE3 1
#else
#  define OCCA_SSE3 0
#endif

#ifdef __SSE3__
#  define OCCA_SSE3 1
#else
#  define OCCA_SSE3 0
#endif

#ifdef __SSE2__
#  define OCCA_SSE2 1
#else
#  define OCCA_SSE2 0
#endif

#ifdef __SSE__
#  define OCCA_SSE 1
#else
#  define OCCA_SSE 0
#endif

#ifdef __MMX__
#  define OCCA_MMX 1
#else
#  define OCCA_MMX 0
#endif

#if OCCA_MIC
#  define OCCA_VECTOR_SET "MIC AVX-512"
#elif OCCA_AVX2
#  define OCCA_VECTOR_SET "AVX2"
#elif OCCA_AVX
#  define OCCA_VECTOR_SET "AVX"
#elif OCCA_SSE4
#  define OCCA_VECTOR_SET "SSE4"
#elif OCCA_SSE3
#  define OCCA_VECTOR_SET "SSE3"
#elif OCCA_SSE2
#  define OCCA_VECTOR_SET "SSE2"
#elif OCCA_SSE
#  define OCCA_VECTOR_SET "SSE"
#elif OCCA_MMX
#  define OCCA_VECTOR_SET "MMX"
#else
#  define OCCA_VECTOR_SET "[Vector Instruction Set Not Found]"
#endif

#if   OCCA_MIC
#  define OCCA_SIMD_WIDTH 16
#elif OCCA_AVX
#  define OCCA_SIMD_WIDTH 8
#elif OCCA_SSE
#  define OCCA_SIMD_WIDTH 4
#elif OCCA_MMX
#  define OCCA_SIMD_WIDTH 2
#else
#  define OCCA_SIMD_WIDTH 1
#endif
//============================

#define OCCA_MAX_ARGS 50
//======================================

//---[ Base ]---------------------------
#define OCCA_KERNEL_ARG_CONSTRUCTOR(TYPE)         \
  template <>                                     \
  inline kernelArg::kernelArg(const TYPE &arg_){  \
    dHandle = NULL;                               \
    mHandle = NULL;                               \
                                                  \
    arg.TYPE##_ = arg_;                           \
    size      = sizeof(TYPE);                     \
                                                  \
    pointer    = false;                           \
    hasTwoArgs = false;                           \
  }

#define OCCA_KERNEL_ARG_CONSTRUCTOR_ALIAS(TYPE, ALIAS)  \
  template <>                                           \
  inline kernelArg::kernelArg(const TYPE &arg_){        \
    dHandle = NULL;                                     \
    mHandle = NULL;                                     \
                                                        \
    arg.ALIAS##_ = arg_;                                \
    size         = sizeof(TYPE);                        \
                                                        \
    pointer    = false;                                 \
    hasTwoArgs = false;                                 \
  }

#define OCCA_EXTRACT_DATA(MODE, CLASS)                          \
  MODE##CLASS##Data_t &data_ = *((MODE##CLASS##Data_t*) data);
//======================================

//---[ OpenCL ]-------------------------
#if OCCA_CHECK_ENABLED
;
#  define OCCA_CL_CHECK( _str , _statement ) OCCA_CL_CHECK2( _str , _statement , __FILE__ , __LINE__ )
#  define OCCA_CL_CHECK2( _str , _statement , file , line )             \
  do {                                                                  \
    cl_int _error = _statement;                                         \
    if(_error){                                                         \
      _error = _error < 0  ? _error : -_error;                          \
      _error = _error < 65 ? _error : 15;                               \
                                                                        \
      std::cout << "Error\n"                                            \
                << "    File    : " << file << '\n'                     \
                << "    Line    : " << line << '\n'                     \
                << "    Error   : OpenCL Error [ " << _error << " ]: " << occa::openclError(_error) << '\n' \
                << "    Message : " << _str << '\n';                    \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)

#else
#  define OCCA_CL_CHECK( _str , _statement ) do { _statement; } while(0)
#endif
//======================================

//---[ CUDA ]---------------------------
#if OCCA_CHECK_ENABLED
#  define OCCA_CUDA_CHECK( _str , _statement ) OCCA_CUDA_CHECK2( _str , _statement , __FILE__ , __LINE__ )
#  define OCCA_CUDA_CHECK2( _str , _statement , file , line )           \
  do {                                                                  \
    CUresult errorCode = _statement;                                    \
    if(errorCode){                                                      \
      std::cout << "Error\n"                                            \
                << "    File    : " << file << '\n'                     \
                << "    Line    : " << line << '\n'                     \
                << "    Error   : CUDA Error [ " << errorCode << " ]: " << occa::cudaError(errorCode) << '\n' \
                << "    Message : " << _str << '\n';                    \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)
#else
#  define OCCA_CUDA_CHECK( _str , _statement ) do { _statement; } while(0)
#endif
//======================================

//---[ COI ]----------------------------
#if OCCA_CHECK_ENABLED
#  define OCCA_COI_CHECK( _str , _statement ) OCCA_COI_CHECK2( _str , _statement , __FILE__ , __LINE__ )
#  define OCCA_COI_CHECK2( _str , _statement , file , line )            \
  do {                                                                  \
    COIRESULT errorCode = _statement;                                   \
    if(errorCode != COI_SUCCESS){                                       \
      std::cout << "Error\n"                                            \
                << "    File    : " << file << '\n'                     \
                << "    Line    : " << line << '\n'                     \
                << "    Error   : " << occa::coiError(errorCode) << '\n' \
                << "    Message : " << _str << '\n';                    \
      throw 1;                                                          \
    }                                                                   \
  } while(0);
#else
#  define OCCA_COI_CHECK( _str , _statement ) do { _statement; } while(0);
#endif
//======================================
#endif
<commit_msg>[Core] Added check on intrinsic defines<commit_after>#ifndef OCCA_DEFINES_HEADER
#define OCCA_DEFINES_HEADER

#ifndef LINUX_OS
#  define LINUX_OS 1
#endif

#ifndef OSX_OS
#  define OSX_OS 2
#endif

#ifndef WINDOWS_OS
#  define WINDOWS_OS 4
#endif

#ifndef WINUX_OS
#  define WINUX_OS (LINUX_OS | WINDOWS_OS)
#endif

#ifndef OCCA_USING_VS
#  ifdef _MSC_VER
#    define OCCA_USING_VS 1
#  else
#    define OCCA_USING_VS 0
#  endif
#endif

#ifndef OCCA_OS
#  if defined(WIN32) || defined(WIN64)
#    if OCCA_USING_VS
#      define OCCA_OS WINDOWS_OS
#    else
#      define OCCA_OS WINUX_OS
#    endif
#  elif __APPLE__
#    define OCCA_OS OSX_OS
#  else
#    define OCCA_OS LINUX_OS
#  endif
#endif

#if OCCA_USING_VS
#  define OCCA_VS_VERSION _MSC_VER
#  include "vs/defines.hpp"
#endif

#ifndef __PRETTY_FUNCTION__
#  define __PRETTY_FUNCTION__ __FUNCTION__
#endif

#if defined(__x86_64__) || defined(_M_X64) // 64 Bit
#  define OCCA_64_BIT 1
#  define OCCA_32_BIT 0
#elif defined(__i386) || defined(_M_IX86) // 32 Bit
#  define OCCA_64_BIT 0
#  define OCCA_32_BIT 1
#elif defined(__ia64) || defined(__itanium__) || defined(_A_IA64) // Itanium
#  define OCCA_64_BIT 1
#  define OCCA_32_BIT 0
#endif

//---[ Checks and Info ]----------------
#ifndef OCCA_COMPILED_FOR_JULIA
#  define OCCA_THROW abort()
#else
#  define OCCA_THROW exit(1)
#endif

#define OCCA_EMPTY_FORCE_CHECK2( _expr , file , line , func )           \
  do {                                                                  \
    intptr_t expr = (_expr);                                            \
    if( !expr ){                                                        \
      std::cout << '\n'                                                 \
                << "---[ Error ]--------------------------------------------\n" \
                << "    File     : " << file << '\n'                    \
                << "    Function : " << func << '\n'                    \
                << "    Line     : " << line << '\n'                    \
                << "========================================================\n"; \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)

#define OCCA_FORCE_CHECK2( _expr , _msg , file , line , func )          \
  do {                                                                  \
    intptr_t expr = (_expr);                                            \
    if( !expr ){                                                        \
      std::cout << '\n'                                                 \
                << "---[ Error ]--------------------------------------------\n" \
                << "    File     : " << file << '\n'                    \
                << "    Function : " << func << '\n'                    \
                << "    Line     : " << line << '\n'                    \
                << "    Error    : " << _msg << '\n'                    \
                << "========================================================\n"; \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)

#define OCCA_EMPTY_FORCE_CHECK( _expr )  OCCA_EMPTY_FORCE_CHECK2( _expr , __FILE__ , __LINE__ , __PRETTY_FUNCTION__)
#define OCCA_FORCE_CHECK( _expr , _msg ) OCCA_FORCE_CHECK2( _expr , _msg , __FILE__ , __LINE__ , __PRETTY_FUNCTION__)

#if OCCA_CHECK_ENABLED
#  define OCCA_EMPTY_CHECK( _expr )  OCCA_EMPTY_FORCE_CHECK( _expr )
#  define OCCA_CHECK( _expr , _msg ) OCCA_FORCE_CHECK( _expr , _msg )
#else
#  define OCCA_EMPTY_CHECK( _expr )
#  define OCCA_CHECK( _expr , _msg )
#endif

#if (OCCA_OS & OSX_OS)
#  define OCCA_MEM_ALIGN 16
#else
#  define OCCA_MEM_ALIGN 16
#endif

//---[ Vectorization ]--------
#ifdef __MIC__
#  define OCCA_MIC 1
#else
#  define OCCA_MIC 0
#endif

#ifdef __AVX2__
#  define OCCA_AVX2 1
#else
#  define OCCA_AVX2 0
#endif

#ifdef __AVX__
#  define OCCA_AVX 1
#else
#  define OCCA_AVX 0
#endif

#ifdef __SSE4_2__
#  define OCCA_SSE4_2 1
#else
#  define OCCA_SSE4_2 0
#endif

#ifdef __SSE4_1__
#  define OCCA_SSE4_1 1
#else
#  define OCCA_SSE4_1 0
#endif

#ifndef OCCA_SSE4
#  if OCCA_SSE4_1 || OCCA_SSE4_2
#    define OCCA_SSE4 1
#  else
#    define OCCA_SSE4 0
#  endif
#endif

#ifdef __SSSE3__
#  define OCCA_SSE3 1
#else
#  define OCCA_SSE3 0
#endif

#ifndef OCCA_SSE3
#  ifdef __SSE3__
#    define OCCA_SSE3 1
#  else
#    define OCCA_SSE3 0
#  endif
#endif

#ifdef __SSE2__
#  define OCCA_SSE2 1
#else
#  define OCCA_SSE2 0
#endif

#ifdef __SSE__
#  define OCCA_SSE 1
#else
#  define OCCA_SSE 0
#endif

#ifdef __MMX__
#  define OCCA_MMX 1
#else
#  define OCCA_MMX 0
#endif

#if OCCA_MIC
#  define OCCA_VECTOR_SET "MIC AVX-512"
#elif OCCA_AVX2
#  define OCCA_VECTOR_SET "AVX2"
#elif OCCA_AVX
#  define OCCA_VECTOR_SET "AVX"
#elif OCCA_SSE4
#  define OCCA_VECTOR_SET "SSE4"
#elif OCCA_SSE3
#  define OCCA_VECTOR_SET "SSE3"
#elif OCCA_SSE2
#  define OCCA_VECTOR_SET "SSE2"
#elif OCCA_SSE
#  define OCCA_VECTOR_SET "SSE"
#elif OCCA_MMX
#  define OCCA_VECTOR_SET "MMX"
#else
#  define OCCA_VECTOR_SET "[Vector Instruction Set Not Found]"
#endif

#if   OCCA_MIC
#  define OCCA_SIMD_WIDTH 16
#elif OCCA_AVX
#  define OCCA_SIMD_WIDTH 8
#elif OCCA_SSE
#  define OCCA_SIMD_WIDTH 4
#elif OCCA_MMX
#  define OCCA_SIMD_WIDTH 2
#else
#  define OCCA_SIMD_WIDTH 1
#endif
//============================

#define OCCA_MAX_ARGS 50
//======================================

//---[ Base ]---------------------------
#define OCCA_KERNEL_ARG_CONSTRUCTOR(TYPE)         \
  template <>                                     \
  inline kernelArg::kernelArg(const TYPE &arg_){  \
    dHandle = NULL;                               \
    mHandle = NULL;                               \
                                                  \
    arg.TYPE##_ = arg_;                           \
    size      = sizeof(TYPE);                     \
                                                  \
    pointer    = false;                           \
    hasTwoArgs = false;                           \
  }

#define OCCA_KERNEL_ARG_CONSTRUCTOR_ALIAS(TYPE, ALIAS)  \
  template <>                                           \
  inline kernelArg::kernelArg(const TYPE &arg_){        \
    dHandle = NULL;                                     \
    mHandle = NULL;                                     \
                                                        \
    arg.ALIAS##_ = arg_;                                \
    size         = sizeof(TYPE);                        \
                                                        \
    pointer    = false;                                 \
    hasTwoArgs = false;                                 \
  }

#define OCCA_EXTRACT_DATA(MODE, CLASS)                          \
  MODE##CLASS##Data_t &data_ = *((MODE##CLASS##Data_t*) data);
//======================================

//---[ OpenCL ]-------------------------
#if OCCA_CHECK_ENABLED
;
#  define OCCA_CL_CHECK( _str , _statement ) OCCA_CL_CHECK2( _str , _statement , __FILE__ , __LINE__ )
#  define OCCA_CL_CHECK2( _str , _statement , file , line )             \
  do {                                                                  \
    cl_int _error = _statement;                                         \
    if(_error){                                                         \
      _error = _error < 0  ? _error : -_error;                          \
      _error = _error < 65 ? _error : 15;                               \
                                                                        \
      std::cout << "Error\n"                                            \
                << "    File    : " << file << '\n'                     \
                << "    Line    : " << line << '\n'                     \
                << "    Error   : OpenCL Error [ " << _error << " ]: " << occa::openclError(_error) << '\n' \
                << "    Message : " << _str << '\n';                    \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)

#else
#  define OCCA_CL_CHECK( _str , _statement ) do { _statement; } while(0)
#endif
//======================================

//---[ CUDA ]---------------------------
#if OCCA_CHECK_ENABLED
#  define OCCA_CUDA_CHECK( _str , _statement ) OCCA_CUDA_CHECK2( _str , _statement , __FILE__ , __LINE__ )
#  define OCCA_CUDA_CHECK2( _str , _statement , file , line )           \
  do {                                                                  \
    CUresult errorCode = _statement;                                    \
    if(errorCode){                                                      \
      std::cout << "Error\n"                                            \
                << "    File    : " << file << '\n'                     \
                << "    Line    : " << line << '\n'                     \
                << "    Error   : CUDA Error [ " << errorCode << " ]: " << occa::cudaError(errorCode) << '\n' \
                << "    Message : " << _str << '\n';                    \
      OCCA_THROW;                                                       \
    }                                                                   \
  } while(0)
#else
#  define OCCA_CUDA_CHECK( _str , _statement ) do { _statement; } while(0)
#endif
//======================================

//---[ COI ]----------------------------
#if OCCA_CHECK_ENABLED
#  define OCCA_COI_CHECK( _str , _statement ) OCCA_COI_CHECK2( _str , _statement , __FILE__ , __LINE__ )
#  define OCCA_COI_CHECK2( _str , _statement , file , line )            \
  do {                                                                  \
    COIRESULT errorCode = _statement;                                   \
    if(errorCode != COI_SUCCESS){                                       \
      std::cout << "Error\n"                                            \
                << "    File    : " << file << '\n'                     \
                << "    Line    : " << line << '\n'                     \
                << "    Error   : " << occa::coiError(errorCode) << '\n' \
                << "    Message : " << _str << '\n';                    \
      throw 1;                                                          \
    }                                                                   \
  } while(0);
#else
#  define OCCA_COI_CHECK( _str , _statement ) do { _statement; } while(0);
#endif
//======================================
#endif
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 6/17/2020, 1:00:53 AM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#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/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::integer::gcd; // for C++14 or for cpp_int
using boost::integer::lcm; // for C++14 or for cpp_int
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{1'000'000'007LL};
// constexpr ll MOD{998'244'353LL}; // be careful
constexpr ll MAX_SIZE{3'000'010LL};
// constexpr ll MAX_SIZE{30'000'010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
bool ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
    return true;
  }
  return false;
}
template <typename T>
bool ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
    return true;
  }
  return false;
}
// ----- 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>;
// ----- for C++17 -----
template <typename T, typename enable_if<is_integral<T>::value>::type * = nullptr>
size_t popcount(T x) { return bitset<64>(x).count(); }
size_t popcount(string const &S) { return bitset<200010>{S}.count(); }
// ----- Infty -----
template <typename T>
constexpr T Infty() { return numeric_limits<T>::max() / 3; }
template <typename T>
constexpr T mInfty() { return numeric_limits<T>::min() / 3; }
// ----- 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 << "No" << endl;
  exit(0);
}

// ----- Solve -----

class Solve
{
  ll N, K;
  ll P, M, SP, SM, SZ;
  vector<ll> plus, minus, zero;

public:
  Solve(ll N, ll K) : N{N}, K{K}
  {
    for (auto i{0}; i < N; ++i)
    {
      ll A;
      cin >> A;
      if (A == 0)
      {
        zero.push_back(A);
      }
      else if (A > 0)
      {
        plus.push_back(A);
      }
      else
      {
        minus.push_back(A);
      }
    }
    sort(plus.begin(), plus.end());
    sort(minus.begin(), minus.end());
    P = plus.size();
    M = minus.size();
    SP = P * P + M * M;
    SM = P * M;
    SZ = N * (N - 1) / 2 - SP - SM;
  }

  void flush()
  {
    ll ans{0};
    if (K <= SM)
    {
      ans = answer_minus();
    }
    else if (K > SM + SZ)
    {
      ans = answer_plus();
    }
    cout << ans << endl;
  }

private:
  ll answer_plus()
  {
    K -= (SM + SZ);
#if DEBUG == 1
    cerr << "K = " << K << endl;
    cerr << "SP = " << SP << endl;
#endif
    sort(minus.rbegin(), minus.rend());
    ll ok{0};
    ll ng{Infty<ll>()};
    while (abs(ng - ok) > 1)
    {
      ll t{(ng + ok) / 2};
#if DEBUG == 1
      cerr << "count_plus(" << t << ") = " << count_plus(t) << endl;
#endif
      if (count_plus(t) < K)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok;
  }

  ll count_plus(ll T)
  {
    ll ans{0};
    for (auto i{0LL}; i < P; ++i)
    {
      ans += count_plus(T, i, plus);
    }
    for (auto i{0LL}; i < M; ++i)
    {
      ans += count_plus(T, i, minus);
    }
    return ans;
  }

  ll count_plus(ll T, ll ind, vector<ll> const &V)
  {
    ll ok{ind};
    ll ng = V.size();
    while (abs(ok - ng) > 1)
    {
      ll t{(ok + ng) / 2};
      if (V[ind] * V[t] < T)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok - ind;
  }

  ll answer_minus()
  {
    sort(plus.rbegin(), plus.rend());
    ll ok{mInfty<ll>()};
    ll ng{0};
    while (abs(ng - ok) > 1)
    {
      ll t{(ng + ok) / 2};
      if (count_minus(t) < K)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok;
  }

  ll count_minus(ll T)
  {
    ll ans{0};
    for (auto e : minus)
    {
      ans += count_minus(T, e);
    }
    return ans;
  }

  ll count_minus(ll T, ll A)
  {
    ll ok{-1}, ng{P};
    while (abs(ok - ng) > 1)
    {
      ll t{(ok + ng) / 2};
      if (A * plus[t] < T)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok + 1;
  }
};

// ----- main() -----

int main()
{
  ll N, K;
  cin >> N >> K;
  Solve solve(N, K);
  solve.flush();
}
<commit_msg>submit D.cpp to 'D - Pairs' (abc155) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1
/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 6/17/2020, 1:00:53 AM
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#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/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::integer::gcd; // for C++14 or for cpp_int
using boost::integer::lcm; // for C++14 or for cpp_int
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{1'000'000'007LL};
// constexpr ll MOD{998'244'353LL}; // be careful
constexpr ll MAX_SIZE{3'000'010LL};
// constexpr ll MAX_SIZE{30'000'010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
bool ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
    return true;
  }
  return false;
}
template <typename T>
bool ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
    return true;
  }
  return false;
}
// ----- 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>;
// ----- for C++17 -----
template <typename T, typename enable_if<is_integral<T>::value>::type * = nullptr>
size_t popcount(T x) { return bitset<64>(x).count(); }
size_t popcount(string const &S) { return bitset<200010>{S}.count(); }
// ----- Infty -----
template <typename T>
constexpr T Infty() { return numeric_limits<T>::max() / 3; }
template <typename T>
constexpr T mInfty() { return numeric_limits<T>::min() / 3; }
// ----- 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 << "No" << endl;
  exit(0);
}

// ----- Solve -----

class Solve
{
  ll N, K;
  ll P, M, SP, SM, SZ;
  vector<ll> plus, minus, zero;

public:
  Solve(ll N, ll K) : N{N}, K{K}
  {
    for (auto i{0}; i < N; ++i)
    {
      ll A;
      cin >> A;
      if (A == 0)
      {
        zero.push_back(A);
      }
      else if (A > 0)
      {
        plus.push_back(A);
      }
      else
      {
        minus.push_back(A);
      }
    }
    sort(plus.begin(), plus.end());
    sort(minus.begin(), minus.end());
    P = plus.size();
    M = minus.size();
    SP = P * (P - 1) / 2 + M * (M - 1) / 2;
    SM = P * M;
    SZ = N * (N - 1) / 2 - SP - SM;
  }

  void flush()
  {
    ll ans{0};
    if (K <= SM)
    {
      ans = answer_minus();
    }
    else if (K > SM + SZ)
    {
      ans = answer_plus();
    }
    cout << ans << endl;
  }

private:
  ll answer_plus()
  {
    K -= (SM + SZ);
#if DEBUG == 1
    cerr << "K = " << K << endl;
    cerr << "SP = " << SP << endl;
#endif
    sort(minus.rbegin(), minus.rend());
    ll ok{0};
    ll ng{Infty<ll>()};
    while (abs(ng - ok) > 1)
    {
      ll t{(ng + ok) / 2};
#if DEBUG == 1
      cerr << "count_plus(" << t << ") = " << count_plus(t) << endl;
#endif
      if (count_plus(t) < K)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok;
  }

  ll count_plus(ll T)
  {
    ll ans{0};
    for (auto i{0LL}; i < P; ++i)
    {
      ans += count_plus(T, i, plus);
    }
    for (auto i{0LL}; i < M; ++i)
    {
      ans += count_plus(T, i, minus);
    }
    return ans;
  }

  ll count_plus(ll T, ll ind, vector<ll> const &V)
  {
    ll ok{ind};
    ll ng = V.size();
    while (abs(ok - ng) > 1)
    {
      ll t{(ok + ng) / 2};
      if (V[ind] * V[t] < T)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok - ind;
  }

  ll answer_minus()
  {
    sort(plus.rbegin(), plus.rend());
    ll ok{mInfty<ll>()};
    ll ng{0};
    while (abs(ng - ok) > 1)
    {
      ll t{(ng + ok) / 2};
      if (count_minus(t) < K)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok;
  }

  ll count_minus(ll T)
  {
    ll ans{0};
    for (auto e : minus)
    {
      ans += count_minus(T, e);
    }
    return ans;
  }

  ll count_minus(ll T, ll A)
  {
    ll ok{-1}, ng{P};
    while (abs(ok - ng) > 1)
    {
      ll t{(ok + ng) / 2};
      if (A * plus[t] < T)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    return ok + 1;
  }
};

// ----- main() -----

int main()
{
  ll N, K;
  cin >> N >> K;
  Solve solve(N, K);
  solve.flush();
}
<|endoftext|>