<commit_before>#ifndef STAN_LANG_AST_HPP
#define STAN_LANG_AST_HPP

#include <stan/lang/ast/base_expr_type.hpp>
#include <stan/lang/ast/expr_type.hpp>
#include <stan/lang/ast/nil.hpp>
#include <stan/lang/ast/var_origin.hpp>
#include <stan/lang/ast/variable_map.hpp>

#include <stan/lang/ast/fun/has_non_param_var_vis.hpp>
#include <stan/lang/ast/fun/has_prob_fun_suffix.hpp>
#include <stan/lang/ast/fun/has_var_vis.hpp>
#include <stan/lang/ast/fun/is_multi_index_vis.hpp>
#include <stan/lang/ast/fun/is_no_op_statement_vis.hpp>
#include <stan/lang/ast/fun/is_nil_vis.hpp>
#include <stan/lang/ast/fun/name_vis.hpp>
#include <stan/lang/ast/fun/returns_type_vis.hpp>
#include <stan/lang/ast/fun/strip_ccdf_suffix.hpp>
#include <stan/lang/ast/fun/strip_cdf_suffix.hpp>
#include <stan/lang/ast/fun/strip_prob_fun_suffix.hpp>
#include <stan/lang/ast/fun/var_decl_base_type_vis.hpp>
#include <stan/lang/ast/fun/var_decl_def_vis.hpp>
#include <stan/lang/ast/fun/var_decl_dims_vis.hpp>
#include <stan/lang/ast/fun/var_decl_has_def_vis.hpp>
#include <stan/lang/ast/fun/var_occurs_vis.hpp>

#include <stan/lang/ast/fun/ends_with.hpp>
#include <stan/lang/ast/fun/fun_name_exists.hpp>
#include <stan/lang/ast/fun/generate_expression.hpp>
#include <stan/lang/ast/fun/get_cdf.hpp>
#include <stan/lang/ast/fun/get_ccdf.hpp>
#include <stan/lang/ast/fun/get_prob_fun.hpp>
#include <stan/lang/ast/fun/has_ccdf_suffix.hpp>
#include <stan/lang/ast/fun/has_cdf_suffix.hpp>
#include <stan/lang/ast/fun/has_lp_suffix.hpp>
#include <stan/lang/ast/fun/has_non_param_var.hpp>
#include <stan/lang/ast/fun/has_rng_suffix.hpp>
#include <stan/lang/ast/fun/has_var.hpp>
#include <stan/lang/ast/fun/indexed_type.hpp>
#include <stan/lang/ast/fun/infer_type_indexing.hpp>
#include <stan/lang/ast/fun/is_assignable.hpp>
#include <stan/lang/ast/fun/is_data_origin.hpp>
#include <stan/lang/ast/fun/is_fun_origin.hpp>
#include <stan/lang/ast/fun/is_multi_index.hpp>
#include <stan/lang/ast/fun/is_nil.hpp>
#include <stan/lang/ast/fun/is_user_defined.hpp>
#include <stan/lang/ast/fun/is_user_defined_prob_function.hpp>
#include <stan/lang/ast/fun/operator_stream_expr_type.hpp>
#include <stan/lang/ast/fun/print_var_origin.hpp>
#include <stan/lang/ast/fun/promote_primitive.hpp>
#include <stan/lang/ast/fun/returns_type.hpp>
#include <stan/lang/ast/fun/total_dims.hpp>
#include <stan/lang/ast/fun/write_base_expr_type.hpp>

#include <stan/lang/ast/sigs/function_signature_t.hpp>
#include <stan/lang/ast/sigs/function_signatures.hpp>

#include <stan/lang/ast/node/arg_decl.hpp>
#include <stan/lang/ast/node/array_expr.hpp>
#include <stan/lang/ast/node/assgn.hpp>
#include <stan/lang/ast/node/assignment.hpp>
#include <stan/lang/ast/node/base_var_decl.hpp>
#include <stan/lang/ast/node/binary_op.hpp>
#include <stan/lang/ast/node/break_continue_statement.hpp>
#include <stan/lang/ast/node/cholesky_corr_var_decl.hpp>
#include <stan/lang/ast/node/cholesky_factor_var_decl.hpp>
#include <stan/lang/ast/node/conditional_op.hpp>
#include <stan/lang/ast/node/conditional_statement.hpp>
#include <stan/lang/ast/node/corr_matrix_var_decl.hpp>
#include <stan/lang/ast/node/cov_matrix_var_decl.hpp>
#include <stan/lang/ast/node/distribution.hpp>
#include <stan/lang/ast/node/double_literal.hpp>
#include <stan/lang/ast/node/double_var_decl.hpp>
#include <stan/lang/ast/node/expression.hpp>
#include <stan/lang/ast/node/expression_type_vis.hpp>
#include <stan/lang/ast/node/for_statement.hpp>
#include <stan/lang/ast/node/fun.hpp>
#include <stan/lang/ast/node/function_decl_def.hpp>
#include <stan/lang/ast/node/function_decl_defs.hpp>
#include <stan/lang/ast/node/idx.hpp>
#include <stan/lang/ast/node/increment_log_prob_statement.hpp>
#include <stan/lang/ast/node/index_op.hpp>
#include <stan/lang/ast/node/index_op_sliced.hpp>
#include <stan/lang/ast/node/integrate_ode.hpp>
#include <stan/lang/ast/node/integrate_ode_control.hpp>
#include <stan/lang/ast/node/int_literal.hpp>
#include <stan/lang/ast/node/int_var_decl.hpp>
#include <stan/lang/ast/node/lb_idx.hpp>
#include <stan/lang/ast/node/lub_idx.hpp>
#include <stan/lang/ast/node/matrix_var_decl.hpp>
#include <stan/lang/ast/node/multi_idx.hpp>
#include <stan/lang/ast/node/no_op_statement.hpp>
#include <stan/lang/ast/node/omni_idx.hpp>
#include <stan/lang/ast/node/ordered_var_decl.hpp>
#include <stan/lang/ast/node/positive_ordered_var_decl.hpp>
#include <stan/lang/ast/node/print_statement.hpp>
#include <stan/lang/ast/node/printable.hpp>
#include <stan/lang/ast/node/program.hpp>
#include <stan/lang/ast/node/range.hpp>
#include <stan/lang/ast/node/reject_statement.hpp>
#include <stan/lang/ast/node/return_statement.hpp>
#include <stan/lang/ast/node/row_vector_var_decl.hpp>
#include <stan/lang/ast/node/sample.hpp>
#include <stan/lang/ast/node/simplex_var_decl.hpp>
#include <stan/lang/ast/node/statement.hpp>
#include <stan/lang/ast/node/statements.hpp>
#include <stan/lang/ast/node/ub_idx.hpp>
#include <stan/lang/ast/node/unary_op.hpp>
#include <stan/lang/ast/node/uni_idx.hpp>
#include <stan/lang/ast/node/unit_vector_var_decl.hpp>
#include <stan/lang/ast/node/variable.hpp>
#include <stan/lang/ast/node/variable_dims.hpp>
#include <stan/lang/ast/node/var_decl.hpp>
#include <stan/lang/ast/node/vector_var_decl.hpp>
#include <stan/lang/ast/node/while_statement.hpp>

#endif
<commit_msg>add scope_access.hpp to ast.hpp<commit_after>#ifndef STAN_LANG_AST_HPP
#define STAN_LANG_AST_HPP

#include <stan/lang/ast/base_expr_type.hpp>
#include <stan/lang/ast/expr_type.hpp>
#include <stan/lang/ast/nil.hpp>
#include <stan/lang/ast/scope_access.hpp>
#include <stan/lang/ast/var_origin.hpp>
#include <stan/lang/ast/variable_map.hpp>

#include <stan/lang/ast/fun/has_non_param_var_vis.hpp>
#include <stan/lang/ast/fun/has_prob_fun_suffix.hpp>
#include <stan/lang/ast/fun/has_var_vis.hpp>
#include <stan/lang/ast/fun/is_multi_index_vis.hpp>
#include <stan/lang/ast/fun/is_no_op_statement_vis.hpp>
#include <stan/lang/ast/fun/is_nil_vis.hpp>
#include <stan/lang/ast/fun/name_vis.hpp>
#include <stan/lang/ast/fun/returns_type_vis.hpp>
#include <stan/lang/ast/fun/strip_ccdf_suffix.hpp>
#include <stan/lang/ast/fun/strip_cdf_suffix.hpp>
#include <stan/lang/ast/fun/strip_prob_fun_suffix.hpp>
#include <stan/lang/ast/fun/var_decl_base_type_vis.hpp>
#include <stan/lang/ast/fun/var_decl_def_vis.hpp>
#include <stan/lang/ast/fun/var_decl_dims_vis.hpp>
#include <stan/lang/ast/fun/var_decl_has_def_vis.hpp>
#include <stan/lang/ast/fun/var_occurs_vis.hpp>

#include <stan/lang/ast/fun/ends_with.hpp>
#include <stan/lang/ast/fun/fun_name_exists.hpp>
#include <stan/lang/ast/fun/generate_expression.hpp>
#include <stan/lang/ast/fun/get_cdf.hpp>
#include <stan/lang/ast/fun/get_ccdf.hpp>
#include <stan/lang/ast/fun/get_prob_fun.hpp>
#include <stan/lang/ast/fun/has_ccdf_suffix.hpp>
#include <stan/lang/ast/fun/has_cdf_suffix.hpp>
#include <stan/lang/ast/fun/has_lp_suffix.hpp>
#include <stan/lang/ast/fun/has_non_param_var.hpp>
#include <stan/lang/ast/fun/has_rng_suffix.hpp>
#include <stan/lang/ast/fun/has_var.hpp>
#include <stan/lang/ast/fun/indexed_type.hpp>
#include <stan/lang/ast/fun/infer_type_indexing.hpp>
#include <stan/lang/ast/fun/is_assignable.hpp>
#include <stan/lang/ast/fun/is_data_origin.hpp>
#include <stan/lang/ast/fun/is_fun_origin.hpp>
#include <stan/lang/ast/fun/is_multi_index.hpp>
#include <stan/lang/ast/fun/is_nil.hpp>
#include <stan/lang/ast/fun/is_user_defined.hpp>
#include <stan/lang/ast/fun/is_user_defined_prob_function.hpp>
#include <stan/lang/ast/fun/operator_stream_expr_type.hpp>
#include <stan/lang/ast/fun/print_var_origin.hpp>
#include <stan/lang/ast/fun/promote_primitive.hpp>
#include <stan/lang/ast/fun/returns_type.hpp>
#include <stan/lang/ast/fun/total_dims.hpp>
#include <stan/lang/ast/fun/write_base_expr_type.hpp>

#include <stan/lang/ast/sigs/function_signature_t.hpp>
#include <stan/lang/ast/sigs/function_signatures.hpp>

#include <stan/lang/ast/node/arg_decl.hpp>
#include <stan/lang/ast/node/array_expr.hpp>
#include <stan/lang/ast/node/assgn.hpp>
#include <stan/lang/ast/node/assignment.hpp>
#include <stan/lang/ast/node/base_var_decl.hpp>
#include <stan/lang/ast/node/binary_op.hpp>
#include <stan/lang/ast/node/break_continue_statement.hpp>
#include <stan/lang/ast/node/cholesky_corr_var_decl.hpp>
#include <stan/lang/ast/node/cholesky_factor_var_decl.hpp>
#include <stan/lang/ast/node/conditional_op.hpp>
#include <stan/lang/ast/node/conditional_statement.hpp>
#include <stan/lang/ast/node/corr_matrix_var_decl.hpp>
#include <stan/lang/ast/node/cov_matrix_var_decl.hpp>
#include <stan/lang/ast/node/distribution.hpp>
#include <stan/lang/ast/node/double_literal.hpp>
#include <stan/lang/ast/node/double_var_decl.hpp>
#include <stan/lang/ast/node/expression.hpp>
#include <stan/lang/ast/node/expression_type_vis.hpp>
#include <stan/lang/ast/node/for_statement.hpp>
#include <stan/lang/ast/node/fun.hpp>
#include <stan/lang/ast/node/function_decl_def.hpp>
#include <stan/lang/ast/node/function_decl_defs.hpp>
#include <stan/lang/ast/node/idx.hpp>
#include <stan/lang/ast/node/increment_log_prob_statement.hpp>
#include <stan/lang/ast/node/index_op.hpp>
#include <stan/lang/ast/node/index_op_sliced.hpp>
#include <stan/lang/ast/node/integrate_ode.hpp>
#include <stan/lang/ast/node/integrate_ode_control.hpp>
#include <stan/lang/ast/node/int_literal.hpp>
#include <stan/lang/ast/node/int_var_decl.hpp>
#include <stan/lang/ast/node/lb_idx.hpp>
#include <stan/lang/ast/node/lub_idx.hpp>
#include <stan/lang/ast/node/matrix_var_decl.hpp>
#include <stan/lang/ast/node/multi_idx.hpp>
#include <stan/lang/ast/node/no_op_statement.hpp>
#include <stan/lang/ast/node/omni_idx.hpp>
#include <stan/lang/ast/node/ordered_var_decl.hpp>
#include <stan/lang/ast/node/positive_ordered_var_decl.hpp>
#include <stan/lang/ast/node/print_statement.hpp>
#include <stan/lang/ast/node/printable.hpp>
#include <stan/lang/ast/node/program.hpp>
#include <stan/lang/ast/node/range.hpp>
#include <stan/lang/ast/node/reject_statement.hpp>
#include <stan/lang/ast/node/return_statement.hpp>
#include <stan/lang/ast/node/row_vector_var_decl.hpp>
#include <stan/lang/ast/node/sample.hpp>
#include <stan/lang/ast/node/simplex_var_decl.hpp>
#include <stan/lang/ast/node/statement.hpp>
#include <stan/lang/ast/node/statements.hpp>
#include <stan/lang/ast/node/ub_idx.hpp>
#include <stan/lang/ast/node/unary_op.hpp>
#include <stan/lang/ast/node/uni_idx.hpp>
#include <stan/lang/ast/node/unit_vector_var_decl.hpp>
#include <stan/lang/ast/node/variable.hpp>
#include <stan/lang/ast/node/variable_dims.hpp>
#include <stan/lang/ast/node/var_decl.hpp>
#include <stan/lang/ast/node/vector_var_decl.hpp>
#include <stan/lang/ast/node/while_statement.hpp>

#endif
<|endoftext|>


<commit_before>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 8/18/2018, 9:08:51 PM
 * Powered by Visual Studio Code
 */

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

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

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

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

class BIT
{ // index starts at 1.
public:
  int N;
  ll *data;

  BIT(int n) : N(n)
  {
    data = new ll[N + 1];
    for (auto i = 1; i <= N; ++i)
    {
      data[i] = 0;
    }
  }

  ~BIT()
  {
    delete[] data;
  }

  ll sum(int i)
  { // [1, i]
    ll s = 0;
    while (i > 0)
    {
      s += data[i];
      i -= i & -i;
    }
    return s;
  }

  ll sum(int a, int b)
  { // [a, b)
    return sum(b - 1) - sum(a - 1);
  }

  void add(int i, ll x)
  {
    while (i <= N)
    {
      data[i] += x;
      i += i & -i;
    }
  }

  void add(int i)
  {
    add(i, 1);
  }
};

int N, M, Q;

typedef tuple<int, int> que;

vector<int> seg[510];
vector<que> X[510];
int ans[100010];

int main()
{
  cin >> N >> M >> Q;
  for (auto i = 0; i < M; i++)
  {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    seg[r].push_back(l);
  }
  for (auto i = 0; i < Q; i++)
  {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    X[r].push_back(que(l, i));
  }
  BIT bit(510);
  for (auto r = 0; r < N; r++)
  {
    for (auto x : seg[r])
    {
      cerr << "l = " << x << endl;
      bit.add(x);
    }
    for (auto x : X[r])
    {
      int l = get<0>(x);
      int ind = get<1>(x);
      ans[ind] = bit.sum(l, r + 1);
    }
  }
  for (auto i = 0; i < Q; i++)
  {
    cout << ans[i] << endl;
  }
}<commit_msg>submit D.cpp to 'D - AtCoder Express 2' (abc106) [C++14 (GCC 5.4.1)]<commit_after>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 8/18/2018, 9:08:51 PM
 * Powered by Visual Studio Code
 */

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

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

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

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

class BIT
{ // index starts at 1.
public:
  int N;
  ll *data;

  BIT(int n) : N(n)
  {
    data = new ll[N + 1];
    for (auto i = 1; i <= N; ++i)
    {
      data[i] = 0;
    }
  }

  ~BIT()
  {
    delete[] data;
  }

  ll sum(int i)
  { // [1, i]
    ll s = 0;
    while (i > 0)
    {
      s += data[i];
      i -= i & -i;
    }
    return s;
  }

  ll sum(int a, int b)
  { // [a, b)
    return sum(b - 1) - sum(a - 1);
  }

  void add(int i, ll x)
  {
    while (i <= N)
    {
      data[i] += x;
      i += i & -i;
    }
  }

  void add(int i)
  {
    add(i, 1);
  }
};

int N, M, Q;

typedef tuple<int, int> que;

vector<int> seg[510];
vector<que> X[510];
int ans[100010];

int main()
{
  cin >> N >> M >> Q;
  for (auto i = 0; i < M; i++)
  {
    int l, r;
    cin >> l >> r;
    seg[r].push_back(l);
  }
  for (auto i = 0; i < Q; i++)
  {
    int l, r;
    cin >> l >> r;
    X[r].push_back(que(l, i));
  }
  BIT bit(510);
  for (auto r = 1; r <= N; r++)
  {
    for (auto x : seg[r])
    {
      bit.add(x);
    }
    for (auto x : X[r])
    {
      int l = get<0>(x);
      int ind = get<1>(x);
      ans[ind] = bit.sum(l, r + 1);
    }
  }
  for (auto i = 0; i < Q; i++)
  {
    cout << ans[i] << endl;
  }
}<|endoftext|>


<commit_before>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-9-1 21:31:09
 * Powered by Visual Studio Code
 */

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

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

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

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

typedef tuple<int, int> path;

ll L;
int N;
vector<path> V[100];

void flush()
{
  int M;
  for (auto i = 0; i < N; i++)
  {
    M += (int)V[i].size();
  }
  cout << N << " " << M << endl;
  for (auto i = 0; i < N; i++)
  {
    for (auto x : V[i])
    {
      cout << i + 1 << " " << get<0>(x) + 1 << " " << get<1>(x) << endl;
    }
  }
}

void add_edge(int v, int w, int c)
{
  if (v > w)
  {
    swap(v, w);
  }
  V[v].push_back(path(w, c));
}

int main()
{
  cin >> L;
  N = 0;
  for (auto i = 0; i < 200; i++)
  {
    if ((L >> i) == 0)
    {
      N = i;
      break;
    }
  }
  assert(0 < N && N <= 20);
  int pow = 1;
  for (auto i = 0; i < N - 1; i++)
  {
    add_edge(i, i + 1, 0);
    add_edge(i, i + 1, pow);
    pow *= 2;
  }
  for (auto i = 0; i < N - 1; i++)
  {
    if ((L >> i) & 1)
    {
      int cost = L & (((1 << N) - 1) << (i + 1));
      add_edge(i, N - 1, cost);
    }
  }
  flush();
}<commit_msg>tried D.cpp to 'D'<commit_after>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-9-1 21:31:09
 * Powered by Visual Studio Code
 */

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

#define DEBUG 0 // change 0 -> 1 if we need debug.

typedef long long ll;

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

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

typedef tuple<int, int> path;

ll L;
int N;
vector<path> V[100];

void flush()
{
  int M = 0;
  for (auto i = 0; i < N; i++)
  {
    M += (int)V[i].size();
  }
  cout << N << " " << M << endl;
  for (auto i = 0; i < N; i++)
  {
    for (auto x : V[i])
    {
      cout << i + 1 << " " << get<0>(x) + 1 << " " << get<1>(x) << endl;
    }
  }
}

void add_edge(int v, int w, int c)
{
  if (v > w)
  {
    swap(v, w);
  }
  V[v].push_back(path(w, c));
}

int main()
{
  cin >> L;
  N = 0;
  for (auto i = 0; i < 200; i++)
  {
    if ((L >> i) == 0)
    {
      N = i;
      break;
    }
  }
  assert(0 < N && N <= 20);
  int pow = 1;
  for (auto i = 0; i < N - 1; i++)
  {
    add_edge(i, i + 1, 0);
    add_edge(i, i + 1, pow);
    pow *= 2;
  }
  for (auto i = 0; i < N - 1; i++)
  {
    if ((L >> i) & 1)
    {
      int cost = L & (((1 << N) - 1) << (i + 1));
      add_edge(i, N - 1, cost);
    }
  }
  flush();
}<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-5-25 21:27:40
 * Powered by Visual Studio Code
 */

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

typedef long long ll;

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

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

class BIT
{ // index starts at 1.
public:
  int N;
  ll *data;

  BIT()
  {
  }

  BIT(int n) : N(n)
  {
    data = new ll[N + 1];
    for (auto i = 1; i <= N; ++i)
    {
      data[i] = 0;
    }
  }

  ~BIT()
  {
    delete[] data;
  }

  ll sum(int i)
  { // [1, i]
    if (i <= 0)
    {
      return 0;
    }
    if (i > N)
    {
      return sum(N);
    }
    ll s = 0;
    while (i > 0)
    {
      s += data[i];
      i -= i & -i;
    }
    return s;
  }

  ll sum(int a, int b)
  {
    return sum(b) - sum(a);
  }

  void add(int i, ll x)
  {
    while (i <= N)
    {
      data[i] += x;
      i += i & -i;
    }
  }

  void add(int i)
  {
    add(i, 1);
  }
};

int Q;
typedef tuple<char, ll, ll> query;
vector<query> V;
char c;
ll B = 0;
map<ll, int> M;
vector<ll> W;
priority_queue<ll> L;
priority_queue<ll, vector<ll>, greater<ll>> R;
BIT bit = BIT(200010);

void merge(ll a, ll b)
{
  ll x = L.top();
  ll y = R.top();
  if (a <= x)
  {
    L.push(a);
  }
  else if (a >= y)
  {
    R.push(a);
  }
  else
  {
    L.push(a);
  }
  while (L.size() > R.size())
  {
    ll t = L.top();
    L.pop();
    R.push(t);
  }
  while (L.size() < R.size())
  {
    ll t = R.top();
    R.pop();
    L.push(t);
  }
  B += b;
  bit.add(M[a], a);
}

void flush()
{
  ll val = L.top();
  ll ans = B;
  ans -= ((ll)M[val]) * val - bit.sum(M[val]);
  ans += ((ll)(W.size() - M[val])) * val + bit.sum(M[val], W.size());
#if DEBUG == 1
  cerr << "M[" << val << "] = " << M[val] << ", W.size() = " << W.size() << endl;
  cerr << "bit.sum(" << M[val] << ") = " << bit.sum(M[val]) << endl;
  cerr << "bit.sum(" << M[val] << ", " << W.size() << ") = " << bit.sum(M[val], W.size()) << endl;
  cerr << ((ll)M[val]) * val - bit.sum(M[val]) << endl;
  cerr << ((ll)(W.size() - M[val])) * val + bit.sum(M[val], W.size()) << endl;
#endif
  cout
      << val << " " << ans << endl;
}

int main()
{
  cin >> Q;
  L.push(-1000000000000LL);
  R.push(1000000000000LL);
  for (auto i = 0; i < Q; i++)
  {
    cin >> c;
    if (c == '1')
    {
      ll a, b;
      cin >> a >> b;
      V.push_back(query(c, a, b));
    }
    else
    {
      V.push_back(query(c, 0, 0));
    }
  }
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      ll a = get<1>(V[i]);
      W.push_back(a);
    }
  }
  sort(W.begin(), W.end());
  for (auto i = 0; i < (int)W.size(); i++)
  {
    M[W[i]] = i + 1;
#if DEBUG == 1
    cerr << "M[" << W[i] << "] = " << M[W[i]] << endl;
#endif
  }
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      merge(get<1>(V[i]), get<2>(V[i]));
    }
    else
    {
      flush();
    }
  }
}<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1

/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-5-25 21:27:40
 * Powered by Visual Studio Code
 */

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

typedef long long ll;

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

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

class BIT
{ // index starts at 1.
public:
  int N;
  ll *data;

  BIT()
  {
  }

  BIT(int n) : N(n)
  {
    data = new ll[N + 1];
    for (auto i = 1; i <= N; ++i)
    {
      data[i] = 0;
    }
  }

  ~BIT()
  {
    delete[] data;
  }

  ll sum(int i)
  { // [1, i]
    if (i <= 0)
    {
      return 0;
    }
    if (i > N)
    {
      return sum(N);
    }
    ll s = 0;
    while (i > 0)
    {
      s += data[i];
      i -= i & -i;
    }
    return s;
  }

  ll sum(int a, int b)
  {
    return sum(b) - sum(a);
  }

  void add(int i, ll x)
  {
    while (i <= N)
    {
      data[i] += x;
      i += i & -i;
    }
  }

  void add(int i)
  {
    add(i, 1);
  }
};

int Q;
typedef tuple<char, ll, ll> query;
vector<query> V;
char c;
ll B = 0;
map<ll, int> M;
vector<ll> W;
priority_queue<ll> L;
priority_queue<ll, vector<ll>, greater<ll>> R;
BIT bit = BIT(200010);

void merge(ll a, ll b)
{
  ll x = L.top();
  ll y = R.top();
  if (a <= x)
  {
    L.push(a);
  }
  else if (a >= y)
  {
    R.push(a);
  }
  else
  {
    L.push(a);
  }
  while (L.size() > R.size())
  {
    ll t = L.top();
    L.pop();
    R.push(t);
  }
  while (L.size() < R.size())
  {
    ll t = R.top();
    R.pop();
    L.push(t);
  }
  B += b;
  bit.add(M[a], a);
}

void flush()
{
  ll val = L.top();
  ll ans = B;
  ans -= ((ll)M[val] - 1) * val - bit.sum(M[val]);
  ans += ((ll)(W.size() - M[val] + 1)) * val + bit.sum(M[val], W.size());
#if DEBUG == 1
  cerr << "M[" << val << "] = " << M[val] << ", W.size() = " << W.size() << endl;
  cerr << "bit.sum(" << M[val] << ") = " << bit.sum(M[val]) << endl;
  cerr << "bit.sum(" << M[val] << ", " << W.size() << ") = " << bit.sum(M[val], W.size()) << endl;
  cerr << ((ll)M[val]) * val - bit.sum(M[val]) << endl;
  cerr << ((ll)(W.size() - M[val])) * val + bit.sum(M[val], W.size()) << endl;
#endif
  cout
      << val << " " << ans << endl;
}

int main()
{
  cin >> Q;
  L.push(-1000000000000LL);
  R.push(1000000000000LL);
  for (auto i = 0; i < Q; i++)
  {
    cin >> c;
    if (c == '1')
    {
      ll a, b;
      cin >> a >> b;
      V.push_back(query(c, a, b));
    }
    else
    {
      V.push_back(query(c, 0, 0));
    }
  }
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      ll a = get<1>(V[i]);
      W.push_back(a);
    }
  }
  sort(W.begin(), W.end());
  for (auto i = 0; i < (int)W.size(); i++)
  {
    M[W[i]] = i + 1;
#if DEBUG == 1
    cerr << "M[" << W[i] << "] = " << M[W[i]] << endl;
#endif
  }
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      merge(get<1>(V[i]), get<2>(V[i]));
    }
    else
    {
      flush();
    }
  }
}<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-5-25 21:27:40
 * Powered by Visual Studio Code
 */

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

typedef long long ll;

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

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

class BIT
{ // index starts at 1.
public:
  int N;
  ll *data;

  BIT()
  {
  }

  BIT(int n) : N(n)
  {
    data = new ll[N + 1];
    for (auto i = 1; i <= N; ++i)
    {
      data[i] = 0;
    }
  }

  ~BIT()
  {
    delete[] data;
  }

  ll sum(int i)
  { // [1, i]
    ll s = 0;
    while (i > 0)
    {
      s += data[i];
      i -= i & -i;
    }
    return s;
  }

  ll sum(int a, int b)
  {
    return sum(b) - sum(a);
  }

  void add(int i, ll x)
  {
    while (i <= N)
    {
      data[i] += x;
      i += i & -i;
    }
  }

  void add(int i)
  {
    add(i, 1);
  }
};

int Q;
typedef tuple<char, ll, ll> query;
vector<query> V;
char c;
ll B = 0;
map<ll, int> M;
vector<ll> W;
priority_queue<ll> L;
priority_queue<ll, vector<ll>, greater<ll>> R;
BIT bit;

void merge(ll a, ll b)
{
  ll x = L.top();
  ll y = R.top();
  if (a <= x)
  {
    L.push(a);
  }
  else if (a >= y)
  {
    R.push(a);
  }
  else
  {
    L.push(a);
  }
  while (L.size() > R.size())
  {
    ll t = L.top();
    L.pop();
    R.push(t);
  }
  while (L.size() < R.size())
  {
    ll t = R.top();
    R.pop();
    L.push(t);
  }
  B += b;
  bit.add(M[a], a);
}

void flush()
{
  ll val = L.top();
  ll ans = B;
  ans -= bit.sum(M[val]);
  ans += bit.sum(M[val], W.size());
  cout << val << " " << ans << endl;
}

int main()
{
  cin >> Q;
  L.push(-1000000000000LL);
  R.push(1000000000000LL);
  for (auto i = 0; i < Q; i++)
  {
    cin >> c;
    if (c == '1')
    {
      ll a, b;
      cin >> a >> b;
      V.push_back(query(c, a, b));
    }
    else
    {
      V.push_back(query(c, 0, 0));
    }
  }
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      ll a = get<1>(V[i]);
      W.push_back(a);
    }
  }
  sort(W.begin(), W.end());
  for (auto i = 0; i < (int)W.size(); i++)
  {
    M[W[i]] = i + 1;
  }
  bit = BIT(W.size());
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      merge(get<1>(V[i]), get<2>(V[i]));
    }
    else
    {
      flush();
    }
  }
}<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1

/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2019-5-25 21:27:40
 * Powered by Visual Studio Code
 */

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

typedef long long ll;

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

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

class BIT
{ // index starts at 1.
public:
  int N;
  ll *data;

  BIT()
  {
  }

  BIT(int n) : N(n)
  {
    data = new ll[N + 1];
    for (auto i = 1; i <= N; ++i)
    {
      data[i] = 0;
    }
  }

  ~BIT()
  {
    delete[] data;
  }

  ll sum(int i)
  { // [1, i]
    ll s = 0;
    while (i > 0)
    {
      s += data[i];
      i -= i & -i;
    }
    return s;
  }

  ll sum(int a, int b)
  {
    return sum(b) - sum(a);
  }

  void add(int i, ll x)
  {
    while (i <= N)
    {
      data[i] += x;
      i += i & -i;
    }
  }

  void add(int i)
  {
    add(i, 1);
  }
};

int Q;
typedef tuple<char, ll, ll> query;
vector<query> V;
char c;
ll B = 0;
map<ll, int> M;
vector<ll> W;
priority_queue<ll> L;
priority_queue<ll, vector<ll>, greater<ll>> R;
BIT bit;

void merge(ll a, ll b)
{
  ll x = L.top();
  ll y = R.top();
  if (a <= x)
  {
    L.push(a);
  }
  else if (a >= y)
  {
    R.push(a);
  }
  else
  {
    L.push(a);
  }
  while (L.size() > R.size())
  {
    ll t = L.top();
    L.pop();
    R.push(t);
  }
  while (L.size() < R.size())
  {
    ll t = R.top();
    R.pop();
    L.push(t);
  }
  B += b;
  bit.add(M[a], a);
}

void flush()
{
  ll val = L.top();
  ll ans = B;
  ans -= bit.sum(M[val]);
  ans += bit.sum(M[val], W.size());
  cout << val << " " << ans << endl;
}

int main()
{
  cin >> Q;
  L.push(-1000000000000LL);
  R.push(1000000000000LL);
  for (auto i = 0; i < Q; i++)
  {
    cin >> c;
    if (c == '1')
    {
      ll a, b;
      cin >> a >> b;
      V.push_back(query(c, a, b));
    }
    else
    {
      V.push_back(query(c, 0, 0));
    }
  }
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      ll a = get<1>(V[i]);
      W.push_back(a);
    }
  }
  sort(W.begin(), W.end());
  for (auto i = 0; i < (int)W.size(); i++)
  {
    M[W[i]] = i + 1;
#if DEBUG == 1
    cerr << "M[" << W[i] << "] = " << M[M[i]] << endl;
#endif
  }
  bit = BIT(W.size());
  for (auto i = 0; i < Q; i++)
  {
    if (get<0>(V[i]) == '1')
    {
      merge(get<1>(V[i]), get<2>(V[i]));
    }
    else
    {
      flush();
    }
  }
}<|endoftext|>


<commit_before>#define DEBUG 1

/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 6/9/2019, 9:32:28 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

#define mattype long long

#include <valarray>
#include <random>
using namespace std;

// 行列
struct matrix
{
  int row, col;
  valarray<mattype> a;
  matrix(int N, int M)
  { // matrix A(N, M); で初期化できる。
    a = valarray<mattype>(N * M);
    row = N;
    col = M;
  }
  bool operator<(const matrix &right) const
  { // 使わないけどtupleに必要
    if (row != right.row)
    {
      return row < right.row;
    }
    if (col != right.col)
    {
      return col < right.col;
    }
    for (auto i = 0; i < row * col; i++)
    {
      if (a[i] != right.a[i])
      {
        return a[i] < right.a[i];
      }
    }
    return false;
  }
  bool operator>(const matrix &right) const
  { // 使わないけどtupleに必要
    if (row != right.row)
    {
      return row > right.row;
    }
    if (col != right.col)
    {
      return col > right.col;
    }
    for (auto i = 0; i < row * col; i++)
    {
      if (a[i] != right.a[i])
      {
        return a[i] > right.a[i];
      }
    }
    return false;
  }
  bool operator==(const matrix &right) const
  {
    if (row != right.row)
      return false;
    if (col != right.col)
      return false;
    for (auto i = 0; i < row * col; i++)
    {
      if (a[i] != right.a[i])
      {
        return false;
      }
    }
    return true;
  }
  string to_s() const
  {
    string res = "";
    for (auto i = 0; i < row; i++)
    {
      for (auto j = 0; j < col; j++)
      {
        res += to_string(a[i * col + j]);
        if (j != col - 1)
          res += " ";
      }
      if (i != row - 1)
        res += "\n";
    }
    return res;
  }
  void input()
  { // 大抵行列表示で入力されるからこれで事足りるでしょう。
    for (auto i = 0; i < row * col; i++)
    {
      cin >> a[i];
    }
  }
};

ostream &operator<<(ostream &s, const matrix A)
{ // cout << A << endl; で苦もなく表示。
  return s << A.to_s();
}

matrix multiply(matrix A, matrix B)
{ // AB を出力
  assert(A.col == B.row);
  int N = A.col;
  matrix C(A.row, B.col);
  for (auto i = 0; i < C.row; i++)
  {
    for (auto j = 0; j < C.col; j++)
    {
      C.a[i * C.col + j] = ((valarray<mattype>)A.a[slice(i * A.col, N, 1)] *
                            (valarray<mattype>)B.a[slice(j, N, B.col)])
                               .sum();
    }
  }
  return C;
}

matrix inverse(matrix A, matrix B)
{ // A^{-1} B を出力
  assert(A.row == A.col);
  assert(A.col == B.row);
  int N = A.row;
  int M = B.col;
  for (auto i = 0; i < N; i++)
  {
    mattype taikaku = A.a[i * N + i];
    for (auto k = 0; k < N; k++)
    {
      if (i == k)
        continue;
      mattype keisu = A.a[k * N + i] / taikaku;
      // A.a[k*N+i] = 0;
      for (auto j = i + 1; j < N; j++)
      {
        A.a[k * N + j] = A.a[k * N + j] - keisu * A.a[i * N + j];
      }
      for (auto j = 0; j < M; j++)
      {
        B.a[k * M + j] = B.a[k * M + j] - keisu * B.a[i * M + j];
      }
    }
  }
  for (auto i = 0; i < N; i++)
  {
    mattype taikaku = A.a[i * N + i];
    for (auto j = 0; j < M; j++)
    {
      B.a[i * M + j] = B.a[i * M + j] / taikaku;
    }
  }
  return B;
}

matrix transposed(matrix A)
{ // 転置
  matrix B = matrix(A.col, A.row);
  for (auto i = 0; i < B.row; i++)
  {
    for (auto j = 0; j < B.col; j++)
    {
      B.a[i * B.col + j] = A.a[j * A.col + i];
    }
  }
  return B;
}

bool AB_is_equal_to_C(matrix A, matrix B, matrix C, int times)
{ // AB = C かどうかを判定。timesには回数を指定。
  // N * M が 10^6 くらいなら、times = 10 で 1秒単位がかかるようになるから要注意。
  // でも 10 はほしいところである。
  assert(A.col == B.row);
  assert(A.row == C.row);
  assert(B.col == C.col);
  random_device rd;
  mt19937 mt(rd());
  matrix p = matrix(B.col, 1);
  for (auto i = 0; i < times; i++)
  {
    for (auto j = 0; j < B.col; j++)
    {
      p.a[j] = mt() % 10000;
    }
    if (multiply(A, multiply(B, p)) == multiply(C, p))
    {
      /*
      cerr << "ABp is following:" << endl;
      cerr << multiply(A, multiply(B, p)) << endl;
      cerr << "Cp is following:" << endl;
      cerr << multiply(C, p) << endl;
      */
      continue;
    }
    else
    {
      return false;
    }
  }
  return true;
}

// 此処から先は、modを取るパターン。

mattype MOD = 1000;

matrix mod_multiply(matrix A, matrix B)
{
  assert(A.col == B.row);
  int N = A.col;
  matrix C(A.row, B.col);
  for (auto i = 0; i < C.row; i++)
  {
    for (auto j = 0; j < C.col; j++)
    {
      C.a[i * C.col + j] = ((valarray<mattype>)A.a[slice(i * A.col, N, 1)] *
                            (valarray<mattype>)B.a[slice(j, N, B.col)])
                               .sum() %
                           MOD;
    }
  }
  return C;
}

matrix mod_pow(matrix A, mattype n)
{ // n \geq 1
  if (n % 2 == 0)
  {
    matrix B = mod_pow(A, n / 2);
    return mod_multiply(B, B);
  }
  else if (n == 1)
  {
    return A;
  }
  else
  {
    return mod_multiply(A, mod_pow(A, n - 1));
  }
}

// 此処から先 main

ll L, A, B;
ll upper[100];

matrix choose(ll k, ll n)
{
  matrix K(3, 3);
  ll p = 1;
  for (auto i = 0; i < k; i++)
  {
    p *= 10LL;
  }
  K.a = {p % MOD, 1, 0, 0, 1, B, 0, 0, 1};
  return mod_pow(K, n);
}

ll f(ll i)
{
  return A + B * i;
}

int main()
{
  cin >> L >> A >> B >> MOD;
  ll start = to_string(f(0)).size();
  for (auto i = start; i <= 18; i++)
  {
    ll ok = upper[i - 1];
    ll ng = L;
    while (abs(ok - ng) > 1)
    {
      ll t = (ok + ng) / 2;
      if ((int)to_string(f(t)).size() == i)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    upper[i] = ng;
  }
  matrix v(3, 1);
  v.a = {0, A, 1};
  for (auto i = start; i <= 18; i++)
  {
    ll x = upper[i] - upper[i - 1];
    if (x == 0)
    {
      continue;
    }
#if DEBUG == 1
    cerr << "i = " << i << ", upper[" << i << "] = " << upper << endl;
    cerr << "f(" << upper[i] << ") = " << f(upper[i]) << endl;
#endif
    v = mod_multiply(choose(i, x), v);
  }
  cout << v.a[0] << endl;
}<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1

/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 6/9/2019, 9:32:28 PM
 * Powered by Visual Studio Code
 */

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

typedef long long ll;

#define mattype long long

#include <valarray>
#include <random>
using namespace std;

// 行列
struct matrix
{
  int row, col;
  valarray<mattype> a;
  matrix(int N, int M)
  { // matrix A(N, M); で初期化できる。
    a = valarray<mattype>(N * M);
    row = N;
    col = M;
  }
  bool operator<(const matrix &right) const
  { // 使わないけどtupleに必要
    if (row != right.row)
    {
      return row < right.row;
    }
    if (col != right.col)
    {
      return col < right.col;
    }
    for (auto i = 0; i < row * col; i++)
    {
      if (a[i] != right.a[i])
      {
        return a[i] < right.a[i];
      }
    }
    return false;
  }
  bool operator>(const matrix &right) const
  { // 使わないけどtupleに必要
    if (row != right.row)
    {
      return row > right.row;
    }
    if (col != right.col)
    {
      return col > right.col;
    }
    for (auto i = 0; i < row * col; i++)
    {
      if (a[i] != right.a[i])
      {
        return a[i] > right.a[i];
      }
    }
    return false;
  }
  bool operator==(const matrix &right) const
  {
    if (row != right.row)
      return false;
    if (col != right.col)
      return false;
    for (auto i = 0; i < row * col; i++)
    {
      if (a[i] != right.a[i])
      {
        return false;
      }
    }
    return true;
  }
  string to_s() const
  {
    string res = "";
    for (auto i = 0; i < row; i++)
    {
      for (auto j = 0; j < col; j++)
      {
        res += to_string(a[i * col + j]);
        if (j != col - 1)
          res += " ";
      }
      if (i != row - 1)
        res += "\n";
    }
    return res;
  }
  void input()
  { // 大抵行列表示で入力されるからこれで事足りるでしょう。
    for (auto i = 0; i < row * col; i++)
    {
      cin >> a[i];
    }
  }
};

ostream &operator<<(ostream &s, const matrix A)
{ // cout << A << endl; で苦もなく表示。
  return s << A.to_s();
}

matrix multiply(matrix A, matrix B)
{ // AB を出力
  assert(A.col == B.row);
  int N = A.col;
  matrix C(A.row, B.col);
  for (auto i = 0; i < C.row; i++)
  {
    for (auto j = 0; j < C.col; j++)
    {
      C.a[i * C.col + j] = ((valarray<mattype>)A.a[slice(i * A.col, N, 1)] *
                            (valarray<mattype>)B.a[slice(j, N, B.col)])
                               .sum();
    }
  }
  return C;
}

matrix inverse(matrix A, matrix B)
{ // A^{-1} B を出力
  assert(A.row == A.col);
  assert(A.col == B.row);
  int N = A.row;
  int M = B.col;
  for (auto i = 0; i < N; i++)
  {
    mattype taikaku = A.a[i * N + i];
    for (auto k = 0; k < N; k++)
    {
      if (i == k)
        continue;
      mattype keisu = A.a[k * N + i] / taikaku;
      // A.a[k*N+i] = 0;
      for (auto j = i + 1; j < N; j++)
      {
        A.a[k * N + j] = A.a[k * N + j] - keisu * A.a[i * N + j];
      }
      for (auto j = 0; j < M; j++)
      {
        B.a[k * M + j] = B.a[k * M + j] - keisu * B.a[i * M + j];
      }
    }
  }
  for (auto i = 0; i < N; i++)
  {
    mattype taikaku = A.a[i * N + i];
    for (auto j = 0; j < M; j++)
    {
      B.a[i * M + j] = B.a[i * M + j] / taikaku;
    }
  }
  return B;
}

matrix transposed(matrix A)
{ // 転置
  matrix B = matrix(A.col, A.row);
  for (auto i = 0; i < B.row; i++)
  {
    for (auto j = 0; j < B.col; j++)
    {
      B.a[i * B.col + j] = A.a[j * A.col + i];
    }
  }
  return B;
}

bool AB_is_equal_to_C(matrix A, matrix B, matrix C, int times)
{ // AB = C かどうかを判定。timesには回数を指定。
  // N * M が 10^6 くらいなら、times = 10 で 1秒単位がかかるようになるから要注意。
  // でも 10 はほしいところである。
  assert(A.col == B.row);
  assert(A.row == C.row);
  assert(B.col == C.col);
  random_device rd;
  mt19937 mt(rd());
  matrix p = matrix(B.col, 1);
  for (auto i = 0; i < times; i++)
  {
    for (auto j = 0; j < B.col; j++)
    {
      p.a[j] = mt() % 10000;
    }
    if (multiply(A, multiply(B, p)) == multiply(C, p))
    {
      /*
      cerr << "ABp is following:" << endl;
      cerr << multiply(A, multiply(B, p)) << endl;
      cerr << "Cp is following:" << endl;
      cerr << multiply(C, p) << endl;
      */
      continue;
    }
    else
    {
      return false;
    }
  }
  return true;
}

// 此処から先は、modを取るパターン。

mattype MOD = 1000;

matrix mod_multiply(matrix A, matrix B)
{
  assert(A.col == B.row);
  int N = A.col;
  matrix C(A.row, B.col);
  for (auto i = 0; i < C.row; i++)
  {
    for (auto j = 0; j < C.col; j++)
    {
      C.a[i * C.col + j] = ((valarray<mattype>)A.a[slice(i * A.col, N, 1)] *
                            (valarray<mattype>)B.a[slice(j, N, B.col)])
                               .sum() %
                           MOD;
    }
  }
  return C;
}

matrix mod_pow(matrix A, mattype n)
{ // n \geq 1
  if (n % 2 == 0)
  {
    matrix B = mod_pow(A, n / 2);
    return mod_multiply(B, B);
  }
  else if (n == 1)
  {
    return A;
  }
  else
  {
    return mod_multiply(A, mod_pow(A, n - 1));
  }
}

// 此処から先 main

ll L, A, B;
ll upper[100];

matrix choose(ll k, ll n)
{
  matrix K(3, 3);
  ll p = 1;
  for (auto i = 0; i < k; i++)
  {
    p *= 10LL;
  }
  K.a = {p % MOD, 1, 0, 0, 1, B, 0, 0, 1};
  return mod_pow(K, n);
}

ll f(ll i)
{
  return A + B * i;
}

int main()
{
  cin >> L >> A >> B >> MOD;
  ll start = to_string(f(0)).size();
  for (auto i = start; i <= 18; i++)
  {
    ll ok = upper[i - 1];
    ll ng = L;
    while (abs(ok - ng) > 1)
    {
      ll t = (ok + ng) / 2;
      if ((int)to_string(f(t)).size() == i)
      {
        ok = t;
      }
      else
      {
        ng = t;
      }
    }
    upper[i] = ng;
  }
  matrix v(3, 1);
  v.a = {0, A, 1};
  for (auto i = start; i <= 18; i++)
  {
    ll x = upper[i] - upper[i - 1];
    if (x == 0)
    {
      continue;
    }
#if DEBUG == 1
    cerr << "i = " << i << ", upper[" << i << "] = " << upper[i] << endl;
    cerr << "f(" << upper[i] << ") = " << f(upper[i]) << endl;
#endif
    v = mod_multiply(choose(i, x), v);
  }
  cout << v.a[0] << endl;
}<|endoftext|>


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

typedef vector<mint> Poly;

ostream &operator<<(ostream &o_str, const Poly &p)
{
  o_str << "{ ";
  for (auto i = 0u; i < p.size(); i++)
  {
    o_str << p[i];
    if (i < p.size() - 1)
    {
      o_str << ", ";
    }
  }
  return o_str << " }";
}

void reduce(Poly &v)
{
  while (!v.empty() && v.back() == 0)
  {
    v.pop_back();
  }
}

bool is_zero(Poly p)
{
  reduce(p);
  return p.empty();
}

Poly operator+(Poly p, Poly q)
{
  reduce(p);
  reduce(q);
  if (p.size() < q.size())
  {
    return q + p;
  }
  else
  {
    for (auto i = 0u; i < q.size(); i++)
    {
      p[i] += q[i];
    }
  }
  reduce(p);
  return p;
}

Poly operator-(Poly p)
{
  reduce(p);
  for (auto i = 0; i < (int)p.size(); i++)
  {
    p[i] = -p[i];
  }
  reduce(p);
  return p;
}

Poly operator-(Poly p, Poly q)
{
  return p + (-q);
}

Poly operator*(Poly p, Poly q)
{
  reduce(p);
  reduce(q);
  if (is_zero(p) || is_zero(q))
  {
    return {};
  }
  Poly ans(p.size() + q.size() - 1, 0);
  for (auto k = 0u; k < ans.size(); k++)
  {
    for (auto i = 0u; i <= k; i++)
    {
      auto j = k - i;
      if (i < p.size() && j < q.size())
      {
        ans[k] += p[i] * q[j];
      }
    }
  }
  reduce(ans);
  return ans;
}

Poly operator*(Poly p, mint v)
{
  reduce(p);
  for (auto &x : p)
  {
    x *= v;
  }
  return p;
}

Poly operator*(mint v, Poly p)
{
  return p * v;
}

void flush(Poly p)
{
  while ((ll)p.size() < mint::MOD)
  {
    p.push_back(0);
  }
  for (auto i = 0; i < mint::MOD; i++)
  {
    cout << p[i];
    if (i < mint::MOD - 1)
    {
      cout << " ";
    }
    else
    {
      cout << endl;
    }
  }
}

mint eval(Poly const &p, mint v)
{
  mint x = 1;
  mint ans = 0;
  for (auto b : p)
  {
    ans += b * x;
    x *= v;
  }
  return ans;
}

Poly one(mint k)
{
  return {-k, 1};
}

Poly div(Poly const &p, mint k)
{ // p / (x - k)
  vector<mint> ans(p.size() - 1, 0);
  ans[0] = 1;
  for (auto i = 1u; i < ans.size(); i++)
  {
    ans[i] = p[i] - (-k) * ans[i - 1];
  }
  reverse(ans.begin(), ans.end());
  reduce(ans);
  return ans;
}

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

typedef vector<mint> Poly;

ostream &operator<<(ostream &o_str, const Poly &p)
{
  o_str << "{ ";
  for (auto i = 0u; i < p.size(); i++)
  {
    o_str << p[i];
    if (i < p.size() - 1)
    {
      o_str << ", ";
    }
  }
  return o_str << " }";
}

void reduce(Poly &v)
{
  while (!v.empty() && v.back() == 0)
  {
    v.pop_back();
  }
}

bool is_zero(Poly p)
{
  reduce(p);
  return p.empty();
}

Poly operator+(Poly p, Poly q)
{
  reduce(p);
  reduce(q);
  if (p.size() < q.size())
  {
    return q + p;
  }
  else
  {
    for (auto i = 0u; i < q.size(); i++)
    {
      p[i] += q[i];
    }
  }
  reduce(p);
  return p;
}

Poly operator-(Poly p)
{
  reduce(p);
  for (auto i = 0; i < (int)p.size(); i++)
  {
    p[i] = -p[i];
  }
  reduce(p);
  return p;
}

Poly operator-(Poly p, Poly q)
{
  return p + (-q);
}

Poly operator*(Poly p, Poly q)
{
  reduce(p);
  reduce(q);
  if (is_zero(p) || is_zero(q))
  {
    return {};
  }
  Poly ans(p.size() + q.size() - 1, 0);
  for (auto k = 0u; k < ans.size(); k++)
  {
    for (auto i = 0u; i <= k; i++)
    {
      auto j = k - i;
      if (i < p.size() && j < q.size())
      {
        ans[k] += p[i] * q[j];
      }
    }
  }
  reduce(ans);
  return ans;
}

Poly operator*(Poly p, mint v)
{
  reduce(p);
  for (auto &x : p)
  {
    x *= v;
  }
  return p;
}

Poly operator*(mint v, Poly p)
{
  return p * v;
}

void flush(Poly p)
{
  while ((ll)p.size() < mint::MOD)
  {
    p.push_back(0);
  }
  for (auto i = 0; i < mint::MOD; i++)
  {
    cout << p[i];
    if (i < mint::MOD - 1)
    {
      cout << " ";
    }
    else
    {
      cout << endl;
    }
  }
}

mint eval(Poly const &p, mint v)
{
  mint x = 1;
  mint ans = 0;
  for (auto b : p)
  {
    ans += b * x;
    x *= v;
  }
  return ans;
}

Poly one(mint k)
{
  return {-k, 1};
}

Poly div(Poly const &p, mint k)
{ // p / (x - k)
  vector<mint> ans(p.size() - 1, 0);
  ans[0] = 1;
  for (auto i = 1u; i < ans.size(); i++)
  {
    ans[i] = p[i] - (-k) * ans[i - 1];
  }
  reverse(ans.begin(), ans.end());
  reduce(ans);
  return ans;
}

int main()
{
  cin >> mint::MOD;
  combination C{};
  vector<ll> W;
  for (auto i = 0LL; i < mint::MOD; i++)
  {
    int a;
    cin >> a;
    if (a == 1)
    {
      W.push_back(i);
    }
  }
  Poly ans = {};
  /*
  Poly base = {1};
  for (auto i = 0LL; i < mint::MOD; ++i)
  {
    base = base * one(i);
  }
  */
  Poly base(mint::MOD + 1, 0);
  base[mint::MOD] = 1;
  base[1] = mint::MOD - 1;
#if DEBUG == 1
  cerr << base << endl;
#endif
  reverse(base.begin(), base.end());
  for (auto e : W)
  {
#if DEBUG == 1
    cerr << "e = " << e << endl;
#endif
    Poly base_e = div(base, e);
    mint rev = eval(base_e, e);
    rev = C.inv[rev.x];
    base_e = base_e * rev;
#if DEBUG == 1
    cerr << base_e << endl;
#endif
    ans = ans + base_e;
  }
  // flush(ans);
}<|endoftext|>


<commit_before>/*
 * -----------------------------------------------------------------
 * $Revision: 1.2 $
 * $Date: 2008/12/17 19:38:48 $
 * -----------------------------------------------------------------
 * Programmer(s): Radu Serban @ LLNL
 * -----------------------------------------------------------------
 * Example (serial):
 *
 * This example solves a nonlinear system from.
 *
 * Source: "Handbook of Test Problems in Local and Global Optimization",
 *             C.A. Floudas, P.M. Pardalos et al.
 *             Kluwer Academic Publishers, 1999.
 * Test problem 4 from Section 14.1, Chapter 14: Ferraris and Tronconi
 *
 * This problem involves a blend of trigonometric and exponential terms.
 *    0.5 sin(x1 x2) - 0.25 x2/pi - 0.5 x1 = 0
 *    (1-0.25/pi) ( exp(2 x1)-e ) + e x2 / pi - 2 e x1 = 0
 * such that
 *    0.25 <= x1 <=1.0
 *    1.5 <= x2 <= 2 pi
 *
 * The treatment of the bound constraints on x1 and x2 is done using
 * the additional variables
 *    l1 = x1 - x1_min >= 0
 *    L1 = x1 - x1_max <= 0
 *    l2 = x2 - x2_min >= 0
 *    L2 = x2 - x2_max >= 0
 *
 * and using the constraint feature in KINSOL to impose
 *    l1 >= 0    l2 >= 0
 *    L1 <= 0    L2 <= 0
 *
 * The Ferraris-Tronconi test problem has two known solutions.
 * The nonlinear system is solved by KINSOL using different
 * combinations of globalization and Jacobian update strategies
 * and with different initial guesses (leading to one or the other
 * of the known solutions).
 *
 *
 * Constraints are imposed to make all components of the solution
 * positive.
 * -----------------------------------------------------------------
 */

#ifndef _DC_SUPERCAPACITOR_SOLVER_
#define _DC_SUPERCAPACITOR_SOLVER_

#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <kinsol/kinsol.h>
#include <kinsol/kinsol_dense.h>
#include <kinsol/kinsol_impl.h>
#include <nvector/nvector_serial.h>
#include <sundials/sundials_types.h>
#include <sundials/sundials_math.h>

#ifdef __cplusplus
}
#endif

/* Problem Constants */

#define NVAR   1
#define NEQ    3*NVAR

#define FTOL   RCONST(1.e-5)  /* function tolerance */
#define STOL   RCONST(1.e-5) /* step tolerance */

#define ZERO   RCONST(0.0)
#define PT25   RCONST(0.25)
#define PT5    RCONST(0.5)
#define ONE    RCONST(1.0)
#define ONEPT5 RCONST(1.5)
#define TWO    RCONST(2.0)

#define PI     RCONST(3.1415926)
#define E      RCONST(2.7182818)

#define TOL	   1.e-3
#define ABSTOL RCONST(1.e-3)
#define ABSTOL2 RCONST(2.e-3)
#define ABSMAX RCONST(1.e3)

const double MinManualStep = 1.e-4;
const double MaxManualStep = 1.e-1;

class DCSolver;

typedef struct {
  realtype lb[NVAR];
  realtype ub[NVAR];
} UserDataStruct;

typedef UserDataStruct *UserData;

/* Accessor macro */
#define Ith(v,i)    NV_Ith_S(v,i-1)

class supcapacitor;
class ees_bank;

class DCSolver {
public:
	DCSolver();
	~DCSolver();
    int SolveItGivenDCInput(double dc_vin, double dc_iin, double &dc_vout, double &dc_iout, double &dc_power, ees_bank *bank);
	int SolveItGivenDCOutput(double dc_vout, double dc_iout, double &dc_vin, double &dc_iin, double &dc_power, ees_bank *bank);
	int ManualSolverGivenDCOutput();
    double ComputeDCPowerBuck(const double &Vin, const double &Vout, const double &Iout);
    double ComputeDCPowerBoost(const double &Vin, const double &Vout, const double &Iout);
    double ComputeDCPowerBuckApproximation(const double &Vin, const double &Vout, const double &Iout);
    double ComputeDCPowerBoostApproximation(const double &Vin, const double &Vout, const double &Iout);
private:
	int KINSolverWapper();
    // Data members for DC-DC converter
    std::vector<double> m_Rsw;
    std::vector<double> m_Qsw;
    double m_RL, m_RC;
    double m_Ictrl;
    double m_fs;
    double m_Lf;

	// Data members for the solver
	UserData data;
	realtype fnormtol, scsteptol;
	N_Vector u1, u, s, c;
	int glstr, mset, flag;
	void *kmem;

	int buck_failed, boost_failed;
};

class DCSolverSolution {
public:
	DCSolverSolution(int qf, double qd, double vin, double iin, double vout, double iout, int bb) :
		quality_flag(qf),
		quality_delta(qd),
		dc_vin(vin),
		dc_iin(iin),
		dc_vout(vout),
		dc_iout(iout),
		buck_boost_mode(bb)
		{}
	bool is_valid() const {
		if ((buck_boost_mode == 1) && (dc_vin < dc_vout))
			return false;
		if ((buck_boost_mode == -1) && (dc_vin > dc_vout))
			return false;
		if ((quality_flag == KIN_SUCCESS) || (quality_delta < ABSTOL2))
			return true;
		return false;
	}
	bool operator<(const DCSolverSolution &d1) const {
		if (is_valid() && !d1.is_valid())
			return false;
		if (!is_valid() && d1.is_valid())
			return true;
		if ((quality_flag == KIN_SUCCESS) && (d1.quality_flag != KIN_SUCCESS))
			return false;
		if ((quality_flag != KIN_SUCCESS) && (d1.quality_flag == KIN_SUCCESS))
			return true;
		return (fabs(quality_delta) > fabs(d1.quality_delta));
	}
	int mode() const { return buck_boost_mode; }
	void output(double &vin, double &iin, double &vout, double &iout) {
		vin = dc_vin;
		iin = dc_iin;
		vout = dc_vout;
		iout = dc_iout;
	}
private:
	int quality_flag;
	double quality_delta;
	double dc_vin;
	double dc_iin;
	double dc_vout;
	double dc_iout;
	int buck_boost_mode; // buck = 1, boost = -1
};

#endif
<commit_msg>Relax the solution for the DCSolver<commit_after>/*
 * -----------------------------------------------------------------
 * $Revision: 1.2 $
 * $Date: 2008/12/17 19:38:48 $
 * -----------------------------------------------------------------
 * Programmer(s): Radu Serban @ LLNL
 * -----------------------------------------------------------------
 * Example (serial):
 *
 * This example solves a nonlinear system from.
 *
 * Source: "Handbook of Test Problems in Local and Global Optimization",
 *             C.A. Floudas, P.M. Pardalos et al.
 *             Kluwer Academic Publishers, 1999.
 * Test problem 4 from Section 14.1, Chapter 14: Ferraris and Tronconi
 *
 * This problem involves a blend of trigonometric and exponential terms.
 *    0.5 sin(x1 x2) - 0.25 x2/pi - 0.5 x1 = 0
 *    (1-0.25/pi) ( exp(2 x1)-e ) + e x2 / pi - 2 e x1 = 0
 * such that
 *    0.25 <= x1 <=1.0
 *    1.5 <= x2 <= 2 pi
 *
 * The treatment of the bound constraints on x1 and x2 is done using
 * the additional variables
 *    l1 = x1 - x1_min >= 0
 *    L1 = x1 - x1_max <= 0
 *    l2 = x2 - x2_min >= 0
 *    L2 = x2 - x2_max >= 0
 *
 * and using the constraint feature in KINSOL to impose
 *    l1 >= 0    l2 >= 0
 *    L1 <= 0    L2 <= 0
 *
 * The Ferraris-Tronconi test problem has two known solutions.
 * The nonlinear system is solved by KINSOL using different
 * combinations of globalization and Jacobian update strategies
 * and with different initial guesses (leading to one or the other
 * of the known solutions).
 *
 *
 * Constraints are imposed to make all components of the solution
 * positive.
 * -----------------------------------------------------------------
 */

#ifndef _DC_SUPERCAPACITOR_SOLVER_
#define _DC_SUPERCAPACITOR_SOLVER_

#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <kinsol/kinsol.h>
#include <kinsol/kinsol_dense.h>
#include <kinsol/kinsol_impl.h>
#include <nvector/nvector_serial.h>
#include <sundials/sundials_types.h>
#include <sundials/sundials_math.h>

#ifdef __cplusplus
}
#endif

/* Problem Constants */

#define NVAR   1
#define NEQ    3*NVAR

#define FTOL   RCONST(1.e-5)  /* function tolerance */
#define STOL   RCONST(1.e-5) /* step tolerance */

#define ZERO   RCONST(0.0)
#define PT25   RCONST(0.25)
#define PT5    RCONST(0.5)
#define ONE    RCONST(1.0)
#define ONEPT5 RCONST(1.5)
#define TWO    RCONST(2.0)

#define PI     RCONST(3.1415926)
#define E      RCONST(2.7182818)

#define TOL	   1.e-3
#define ABSTOL RCONST(1.e-3)
#define ABSTOL2 RCONST(2.e-3)
#define ABSMAX RCONST(1.e3)

const double MinManualStep = 1.e-4;
const double MaxManualStep = 1.e-1;

class DCSolver;

typedef struct {
  realtype lb[NVAR];
  realtype ub[NVAR];
} UserDataStruct;

typedef UserDataStruct *UserData;

/* Accessor macro */
#define Ith(v,i)    NV_Ith_S(v,i-1)

class supcapacitor;
class ees_bank;

class DCSolver {
public:
	DCSolver();
	~DCSolver();
    int SolveItGivenDCInput(double dc_vin, double dc_iin, double &dc_vout, double &dc_iout, double &dc_power, ees_bank *bank);
	int SolveItGivenDCOutput(double dc_vout, double dc_iout, double &dc_vin, double &dc_iin, double &dc_power, ees_bank *bank);
	int ManualSolverGivenDCOutput();
    double ComputeDCPowerBuck(const double &Vin, const double &Vout, const double &Iout);
    double ComputeDCPowerBoost(const double &Vin, const double &Vout, const double &Iout);
    double ComputeDCPowerBuckApproximation(const double &Vin, const double &Vout, const double &Iout);
    double ComputeDCPowerBoostApproximation(const double &Vin, const double &Vout, const double &Iout);
private:
	int KINSolverWapper();
    // Data members for DC-DC converter
    std::vector<double> m_Rsw;
    std::vector<double> m_Qsw;
    double m_RL, m_RC;
    double m_Ictrl;
    double m_fs;
    double m_Lf;

	// Data members for the solver
	UserData data;
	realtype fnormtol, scsteptol;
	N_Vector u1, u, s, c;
	int glstr, mset, flag;
	void *kmem;

	int buck_failed, boost_failed;
};

class DCSolverSolution {
public:
	DCSolverSolution(int qf, double qd, double vin, double iin, double vout, double iout, int bb) :
		quality_flag(qf),
		quality_delta(qd),
		dc_vin(vin),
		dc_iin(iin),
		dc_vout(vout),
		dc_iout(iout),
		buck_boost_mode(bb)
		{}
	bool is_valid() const {
		if ((buck_boost_mode == 1) && (dc_vin < dc_vout))
			return false;
		if ((buck_boost_mode == -1) && (dc_vin > dc_vout))
			return false;
		if ((quality_flag == KIN_SUCCESS) || (quality_delta/dc_vout < ABSTOL2))
			return true;
		return false;
	}
	bool operator<(const DCSolverSolution &d1) const {
		if (is_valid() && !d1.is_valid())
			return false;
		if (!is_valid() && d1.is_valid())
			return true;
		if ((quality_flag == KIN_SUCCESS) && (d1.quality_flag != KIN_SUCCESS))
			return false;
		if ((quality_flag != KIN_SUCCESS) && (d1.quality_flag == KIN_SUCCESS))
			return true;
		return (fabs(quality_delta) > fabs(d1.quality_delta));
	}
	int mode() const { return buck_boost_mode; }
	void output(double &vin, double &iin, double &vout, double &iout) {
		vin = dc_vin;
		iin = dc_iin;
		vout = dc_vout;
		iout = dc_iout;
	}
private:
	int quality_flag;
	double quality_delta;
	double dc_vin;
	double dc_iin;
	double dc_vout;
	double dc_iout;
	int buck_boost_mode; // buck = 1, boost = -1
};

#endif
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2/22/2020, 9:38:37 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() -----

ll k, q;
vector<ll> d;
vector<ll> e;
vector<ll> N, X, M;

ll solve(ll n, ll x, ll m)
{
  for (auto i = 0; i < k; ++i)
  {
    e[i] = d[i] % m;
#if DEBUG == 1
    cerr << "e[" << i << "] = " << e[i] << endl;
#endif
  }
  ll cnt{0};
  for (auto i = 0; i < k; ++i)
  {
    if (e[i] == 0)
    {
      ++cnt;
    }
  }
  ll S{accumulate(e.begin(), e.end(), 0LL)};
  ll T{(n - 1) / k};
  ll U{(n - 1) % k};
  ll A{S * T + x};
#if DEBUG == 1
  cerr << "T = " << T << endl;
  cerr << "U = " << U << endl;
  cerr << "A = " << A << endl;
#endif

  ll Y{cnt * T};
  for (auto i = 0; i < U; ++i)
  {
    A += e[i];
    if (e[i] == 0)
    {
      ++Y;
    }
  }
  ll Z{A / m};
#if DEBUG == 1
  cerr << "Y = " << Y << endl;
  cerr << "Z = " << Z << endl;
#endif
  return n - 1 - Y - Z;
}

int main()
{
  cin >> k >> q;
  d.resize(k);
  e.resize(k);
  for (auto i = 0; i < k; ++i)
  {
    cin >> d[i];
  }
  N.resize(q);
  X.resize(q);
  M.resize(q);
  for (auto i = 0; i < q; ++i)
  {
    cin >> N[i] >> X[i] >> M[i];
  }
  for (auto i = 0; i < q; ++i)
  {
    cout << solve(N[i], X[i], M[i]) << endl;
  }
}
<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2/22/2020, 9:38:37 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() -----

ll k, q;
vector<ll> d;
vector<ll> e;
vector<ll> N, X, M;

ll solve(ll n, ll x, ll m)
{
  for (auto i = 0; i < k; ++i)
  {
    e[i] = d[i] % m;
#if DEBUG == 1
    cerr << "e[" << i << "] = " << e[i] << endl;
#endif
  }
  ll cnt{0};
  for (auto i = 0; i < k; ++i)
  {
    if (e[i] == 0)
    {
      ++cnt;
    }
  }
  ll S{accumulate(e.begin(), e.end(), 0LL)};
  ll T{(n - 1) / k};
  ll U{(n - 1) % k};
  ll A{S * T + x};
#if DEBUG == 1
  cerr << "S = " << S << endl;
  cerr << "T = " << T << endl;
  cerr << "U = " << U << endl;
  cerr << "A = " << A << endl;
#endif

  ll Y{cnt * T};
  for (auto i = 0; i < U; ++i)
  {
    A += e[i];
    if (e[i] == 0)
    {
      ++Y;
    }
  }
  ll Z{A / m};
#if DEBUG == 1
  cerr << "Y = " << Y << endl;
  cerr << "Z = " << Z << endl;
#endif
  return n - 1 - Y - Z;
}

int main()
{
  cin >> k >> q;
  d.resize(k);
  e.resize(k);
  for (auto i = 0; i < k; ++i)
  {
    cin >> d[i];
  }
  N.resize(q);
  X.resize(q);
  M.resize(q);
  for (auto i = 0; i < q; ++i)
  {
    cin >> N[i] >> X[i] >> M[i];
  }
  for (auto i = 0; i < q; ++i)
  {
    cout << solve(N[i], X[i], M[i]) << endl;
  }
}
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/3/7 21:06:26
 * 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() -----

int main()
{
  string S;
  cin >> S;
  int Q;
  cin >> Q;
  bool reversed{false};
  for (auto q = 0; q < Q; ++q)
  {
    int T;
    cin >> T;
    if (T == 1)
    {
      reversed = !reversed;
    }
    else
    {
      int F;
      cin >> F;
      string C;
      cin >> C;
      if (!reversed && F == 1)
      {
        stringstream SS;
        SS << C << S;
        S = SS.str();
      }
      else if (!reversed && F == 2)
      {
        stringstream SS;
        SS << S << C;
        S = SS.str();
      }
      else if (reversed && F == 1)
      {
        reverse(C.begin(), C.end());
        stringstream SS;
        SS << S << C;
        S = SS.str();
      }
      else
      {
        reverse(C.begin(), C.end());
        stringstream SS;
        SS << C << S;
        S = SS.str();
      }
    }
  }
  if (reversed)
  {
    reverse(S.begin(), S.end());
  }
  cout << S << endl;
}
<commit_msg>submit D.cpp to 'D - String Formation' (abc158) [C++14 (GCC 5.4.1)]<commit_after>#define DEBUG 1
/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/3/7 21:06:26
 * 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() -----

int main()
{
  string S;
  cin >> S;
  int Q;
  cin >> Q;
  bool reversed{false};
  for (auto q = 0; q < Q; ++q)
  {
    int T;
    cin >> T;
    if (T == 1)
    {
      reversed = !reversed;
    }
    else
    {
      int F;
      cin >> F;
      char C;
      cin >> C;
      if (!reversed && F == 1)
      {
        S = C + S;
      }
      else if (!reversed && F == 2)
      {
        S = S + C;
      }
      else if (reversed && F == 1)
      {
        S = S + C;
      }
      else
      {
        S = C + S;
      }
    }
  }
  if (reversed)
  {
    reverse(S.begin(), S.end());
  }
  cout << S << endl;
}
<|endoftext|>