<commit_before>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-4-7 21:10:12
 * 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 <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 1 // 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;

ll A, B;

int solve()
{
  ll S = A * B;
  if (A > B)
    swap(A, B);
  ll ub = B;
  ll lb = A;
  while (ub - lb > 1)
  {
    ll t = (ub + lb) / 2;
    if (t * t < S)
    {
      lb = t;
    }
    else
    {
      ub = t;
    }
  }
  if ((S+A)/(A+1) - (S+A-1)/A >= 1)
  {
    return 2 * lb - 1;
  }
  else
  {
    return 2 * lb - 2;
  }
}

int main()
{
  int Q;
  cin >> Q;
  for (auto i = 0; i < Q; i++)
  {
    cin >> A >> B;
    cout << solve() << endl;
  }
}<commit_msg>tried D.cpp to 'D'<commit_after>/**
 * File    : D.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-4-7 21:10:12
 * 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 <random> // random_device rd; mt19937 mt(rd());
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;

#define DEBUG 1 // 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;

ll A, B;

int solve()
{
  ll S = A * B;
  if (A > B)
    swap(A, B);
  ll ub = B;
  ll lb = A;
  while (ub - lb > 1)
  {
    ll t = (ub + lb) / 2;
    if (t * t < S)
    {
      lb = t;
    }
    else
    {
      ub = t;
    }
  }
  if ((S+A)/(A+1) - (S+A-1)/A >= 2)
  {
    return 2 * lb - 1;
  }
  else
  {
    return 2 * lb - 2;
  }
}

int main()
{
  int Q;
  cin >> Q;
  for (auto i = 0; i < Q; i++)
  {
    cin >> A >> B;
    cout << solve() << endl;
  }
}<|endoftext|>


<commit_before>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-4-14 21:58:19
 * 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 <random> // random_device rd; mt19937 mt(rd());
#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;

int H, W;
string S[20];
typedef tuple<int, int> P;
vector<vector<P>> pairs;

void dfs(vector<P> temp, vector<bool> used, int R)
{
  if (R%2 == 1)
  {
    for (auto i = 0; i < H; i++)
    {
      if (!used[i])
      {
        vector<bool> n_used = used;
        n_used[i] = true;
        vector<P> n_temp = temp;
        n_temp.push_back(P(i, i));
        dfs(n_temp, n_used, R - 1);
      }
    }
    return;
  }
  if (R == 0)
  {
    pairs.push_back(temp);
    return;
  }
  assert(R >= 2);
  int first = -1;
  for (auto i = 0; i < H; i++)
  {
    if (!used[i])
    {
      first = i;
      used[i] = true;
      break;
    }
  }
  assert(first >= 0);
  for (auto i = 0; i < H; i++)
  {
    if (!used[i])
    {
      vector<bool> n_used = used;
      n_used[i] = true;
      vector<P> n_temp = temp;
      n_temp.push_back(P(first, i));
      dfs(n_temp, n_used, R - 2);
    }
  }
}

vector<string> make_strings(vector<P>& V)
{
  vector<int> X;
  int N = V.size();
  for (auto i = 0; i < N; i++)
  {
    X.push_back(get<0>(V[i]));
  }
  for (auto i = N-1; i >= 0; i--)
  {
    X.push_back(get<1>(V[i]));
  }
  vector<string> res;
  for (auto j = 0; j < W; j++)
  {
    string str = "";
    for (auto i : X)
    {
      string t = {S[i][j]};
      str += t;
    }
    res.push_back(str);
  }
  return res;
}

vector<string> reverse_strings(const vector<string>& V)
{
  vector<string> res;
  for (auto x : V)
  {
    reverse(x.begin(), x.end());
    res.push_back(x);
  }
  return res;
}

int main()
{
  cin >> H >> W;
  for (auto i = 0; i < H; i++)
  {
    cin >> S[i];
  }
  vector<P> emp_P;
  vector<bool> emp_used = vector<bool>(H, false);
  dfs(emp_P, emp_used, H);
#if DEBUG == 1
  cerr << "pairs.size() = " << pairs.size() << endl;
  if (pairs.size() < 5)
  {
    for (auto V : pairs)
    {
      for (auto e : V)
      {
        cerr << "[" << get<0>(e) << ", " << get<1>(e) << "] ";
      }
      cerr << endl;
    }
  }
#endif
  for (auto e : pairs)
  {
    vector<string> V1 = make_strings(e);
    vector<string> V2 = reverse_strings(V1);
    assert((int)V1.size() == W && (int)V2.size() == W);
#if DEBUG == 1
    if (pairs.size() < 5)
    {
      for (auto x : V1)
      {
        cerr << x << " ";
      }
      cerr << endl;
      for (auto x : V2)
      {
        cerr << x << " ";
      }
      cerr << endl;
    }
#endif
    vector<bool> used = vector<bool>(W, false);
    bool found = true;
    bool has_center = (W%2 == 0);
    for (auto i = 0; i < W; i++)
    {
      if (used[i])
      {
        continue;
      }
      found = false;
      used[i] = true;
      for (auto j = i + 1; j < W; j++)
      {
        if (!used[j] && V1[i] == V2[j])
        {
          used[j] = true;
          found = true;
          break;
        }
      }
      if (!has_center && V1[i] == V2[i])
      {
        if (W == 1)
        {
          assert(false);
        }
        has_center = true;
        found = true;
      }
      if (!found)
      {
        break;
      }
    }
    if (found)
    {
      cout << "YES" << endl;
      return 0;
    }
  }
  // assert(false);
  cout << "NO" << endl;
}<commit_msg>tried E.cpp to 'E'<commit_after>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-4-14 21:58:19
 * 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 <random> // random_device rd; mt19937 mt(rd());
#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;

int H, W;
string S[20];
typedef tuple<int, int> P;
vector<vector<P>> pairs;

void dfs(vector<P> temp, vector<bool> used, int R)
{
  if (R%2 == 1)
  {
    for (auto i = 0; i < H; i++)
    {
      if (!used[i])
      {
        vector<bool> n_used = used;
        n_used[i] = true;
        vector<P> n_temp = temp;
        n_temp.push_back(P(i, i));
        dfs(n_temp, n_used, R - 1);
      }
    }
    return;
  }
  if (R == 0)
  {
    pairs.push_back(temp);
    return;
  }
  assert(R >= 2);
  int first = -1;
  for (auto i = 0; i < H; i++)
  {
    if (!used[i])
    {
      first = i;
      used[i] = true;
      break;
    }
  }
  assert(first >= 0);
  for (auto i = 0; i < H; i++)
  {
    if (!used[i])
    {
      vector<bool> n_used = used;
      n_used[i] = true;
      vector<P> n_temp = temp;
      n_temp.push_back(P(first, i));
      dfs(n_temp, n_used, R - 2);
    }
  }
}

vector<string> make_strings(vector<P>& V)
{
  vector<int> X;
  int N = V.size();
  for (auto i = 0; i < N; i++)
  {
    X.push_back(get<0>(V[i]));
  }
  for (auto i = N-1; i >= 0; i--)
  {
    X.push_back(get<1>(V[i]));
  }
  vector<string> res;
  for (auto j = 0; j < W; j++)
  {
    string str = "";
    for (auto i : X)
    {
      string t = {S[i][j]};
      str += t;
    }
    res.push_back(str);
  }
  return res;
}

vector<string> reverse_strings(const vector<string>& V)
{
  vector<string> res;
  for (auto x : V)
  {
    reverse(x.begin(), x.end());
    res.push_back(x);
  }
  return res;
}

int main()
{
  cin >> H >> W;
  for (auto i = 0; i < H; i++)
  {
    cin >> S[i];
  }
  vector<P> emp_P;
  vector<bool> emp_used = vector<bool>(H, false);
  dfs(emp_P, emp_used, H);
#if DEBUG == 1
  cerr << "pairs.size() = " << pairs.size() << endl;
  if (pairs.size() < 5)
  {
    for (auto V : pairs)
    {
      for (auto e : V)
      {
        cerr << "[" << get<0>(e) << ", " << get<1>(e) << "] ";
      }
      cerr << endl;
    }
  }
#endif
  for (auto e : pairs)
  {
    vector<string> V1 = make_strings(e);
    vector<string> V2 = reverse_strings(V1);
    assert((int)V1.size() == W && (int)V2.size() == W);
#if DEBUG == 1
    if (pairs.size() < 5)
    {
      for (auto x : V1)
      {
        cerr << x << " ";
      }
      cerr << endl;
      for (auto x : V2)
      {
        cerr << x << " ";
      }
      cerr << endl;
    }
#endif
    vector<bool> used = vector<bool>(W, false);
    bool found = true;
    bool has_center = (W%2 == 0);
    for (auto i = 0; i < W; i++)
    {
      if (used[i])
      {
        continue;
      }
      found = false;
      used[i] = true;
      for (auto j = i + 1; j < W; j++)
      {
        if (!used[j] && V1[i] == V2[j])
        {
          used[j] = true;
          found = true;
          break;
        }
      }
      if (!has_center && V1[i] == V2[i])
      {
        if (W == 3)
        {
          assert(false);
        }
        has_center = true;
        found = true;
      }
      if (!found)
      {
        break;
      }
    }
    if (found)
    {
      cout << "YES" << endl;
      return 0;
    }
  }
  // assert(false);
  cout << "NO" << endl;
}<|endoftext|>


<commit_before>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-5-26 21:42:14
 * 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 <random> // random_device rd; mt19937 mt(rd());
#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);
  }
};

typedef pair<int, int> P;

int N;
int K;
int Q;
int A[2010];
vector<P> V;
bool visited[2010];

int main()
{
  cin >> N >> K >> Q;
  for (auto i = 0; i < N; i++)
  {
    cin >> A[i];
    V.push_back(P(A[i], i));
  }
  sort(V.begin(), V.end());
  reverse(V.begin(), V.end());
  int ans = 1000000007;
  fill(visited, visited + 2010, false);
  for (auto i = 0; i < N; i++)
  {
    int k = V[i].second;
    visited[k] = true;
    int left[2010];
    int right[2010];
    int t = -1;
    for (auto j = 0; j < N; j++)
    {
      if (!visited[j])
      {
        t = j;
      }
      left[j] = t;
    }
    t = N;
    for (auto j = N; j >= 0; j--)
    {
      if (!visited[j])
      {
        t = j;
      }
      right[j] = t;
    }
    BIT bit(N + 1);
    int cnt[2010];
    for (auto j = 0; j <= i; j++)
    {
      k = V[k].second;
      bit.add(k);
      cnt[k] = bit.sum(left[k] + 1, right[k]);
    }
    int Y = V[i].first;
    cerr << "Y = " << Y << endl;
    int sel = 0;
    for (auto j = i; j >= 0; j--)
    {
      k = V[k].second;
      if (cnt[k] >= K)
      {
        sel++;
        if (sel == Q)
        {
          int X = V[j].first;
          ans = min(ans, X - Y);
          break;
        }
      }
    }
  }
  cout << ans << endl;
}<commit_msg>tried E.cpp to 'E'<commit_after>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-5-26 21:42:14
 * 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 <random> // random_device rd; mt19937 mt(rd());
#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);
  }
};

typedef pair<int, int> P;

int N;
int K;
int Q;
int A[2010];
vector<P> V;
bool visited[2010];

int main()
{
  cin >> N >> K >> Q;
  for (auto i = 0; i < N; i++)
  {
    cin >> A[i];
    V.push_back(P(A[i], i));
  }
  sort(V.begin(), V.end());
  reverse(V.begin(), V.end());
  int ans = 1000000007;
  fill(visited, visited + 2010, false);
  for (auto i = 0; i < N; i++)
  {
    int Y = V[i].first;
    cerr << "Y = " << Y << endl;
    int k = V[i].second;
    visited[k] = true;
    int left[2010];
    int right[2010];
    int t = -1;
    for (auto j = 0; j < N; j++)
    {
      if (!visited[j])
      {
        t = j;
      }
      left[j] = t;
    }
    t = N;
    for (auto j = N; j >= 0; j--)
    {
      if (!visited[j])
      {
        t = j;
      }
      right[j] = t;
    }
    BIT bit(N + 1);
    int cnt[2010];
    for (auto j = 0; j <= i; j++)
    {
      k = V[k].second;
      bit.add(k);
      cnt[k] = bit.sum(left[k] + 1, right[k]);
    }
    int sel = 0;
    for (auto j = i; j >= 0; j--)
    {
      k = V[k].second;
      if (cnt[k] >= K)
      {
        sel++;
        if (sel == Q)
        {
          int X = V[j].first;
          ans = min(ans, X - Y);
          break;
        }
      }
    }
  }
  cout << ans << endl;
}<|endoftext|>


<commit_before>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-9-1 21:54:36
 * 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 = 998244353;

const int MAX_SIZE = 1000010;
const long long MOD = 998244353;

long long inv[MAX_SIZE];
long long fact[MAX_SIZE];
long long factinv[MAX_SIZE];

void init()
{
  inv[1] = 1;
  for (int i = 2; i < MAX_SIZE; i++)
  {
    inv[i] = ((MOD - inv[MOD % i]) * (MOD / i)) % MOD;
  }
  fact[0] = factinv[0] = 1;
  for (int i = 1; i < MAX_SIZE; i++)
  {
    fact[i] = (i * fact[i - 1]) % MOD;
    factinv[i] = (inv[i] * factinv[i - 1]) % MOD;
  }
}

long long C(int n, int k)
{
  if (n >= 0 && k >= 0 && n - k >= 0)
  {
    return ((fact[n] * factinv[k]) % MOD * factinv[n - k]) % MOD;
  }
  return 0;
}

long long power(long long x, long long n)
{
  if (n == 0)
  {
    return 1;
  }
  else if (n % 2 == 1)
  {
    return (x * power(x, n - 1)) % MOD;
  }
  else
  {
    long long half = power(x, n / 2);
    return (half * half) % MOD;
  }
}

long long gcm(long long a, long long b)
{
  if (a < b)
  {
    return gcm(b, a);
  }
  if (b == 0)
    return a;
  return gcm(b, a % b);
}

ll K, N;
ll ans[10010];

void flush()
{
  cerr << "total = " << C(N + K - 1, K - 1) << endl;
  for (auto i = 2; i <= 2 * K; i++)
  {
    ll c = (C(N + K - 1, K - 1) + M - (ans[i] % M)) % M;
    cout << c << endl;
  }
}

void calc(ll X)
{
  ll Y = (2 * C(N + K - 2, K - 2) + M - C(N + K - 3, K - 3)) % M;
  ll c = (((X - 1) / 2) * Y) % M;
  if (X % 2 == 0)
  {
    c += C(N + K - 2, K - 2);
    c %= M;
    c += C(N + K - 3, K - 2);
    c %= M;
  }
  ans[X] = c % M;
}

int main()
{
  init();
  cin >> K >> N;
  for (auto i = 2; i <= 2 * K; i++)
  {
    calc(i);
  }
  flush();
}<commit_msg>tried E.cpp to 'E'<commit_after>/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 2018-9-1 21:54:36
 * 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 = 998244353;

const int MAX_SIZE = 1000010;
const long long MOD = 998244353;

long long inv[MAX_SIZE];
long long fact[MAX_SIZE];
long long factinv[MAX_SIZE];

void init()
{
  inv[1] = 1;
  for (int i = 2; i < MAX_SIZE; i++)
  {
    inv[i] = ((MOD - inv[MOD % i]) * (MOD / i)) % MOD;
  }
  fact[0] = factinv[0] = 1;
  for (int i = 1; i < MAX_SIZE; i++)
  {
    fact[i] = (i * fact[i - 1]) % MOD;
    factinv[i] = (inv[i] * factinv[i - 1]) % MOD;
  }
}

long long C(int n, int k)
{
  if (n >= 0 && k >= 0 && n - k >= 0)
  {
    return ((fact[n] * factinv[k]) % MOD * factinv[n - k]) % MOD;
  }
  return 0;
}

long long power(long long x, long long n)
{
  if (n == 0)
  {
    return 1;
  }
  else if (n % 2 == 1)
  {
    return (x * power(x, n - 1)) % MOD;
  }
  else
  {
    long long half = power(x, n / 2);
    return (half * half) % MOD;
  }
}

long long gcm(long long a, long long b)
{
  if (a < b)
  {
    return gcm(b, a);
  }
  if (b == 0)
    return a;
  return gcm(b, a % b);
}

ll K, N;
ll ans[10010];

void flush()
{
  cerr << "total = " << C(N + K - 1, K - 1) << endl;
  cerr << "chance = " << (2 * C(N + K - 2, K - 2) + M - C(N + K - 3, K - 3)) % M << endl;
  for (auto i = 2; i <= 2 * K; i++)
  {
    ll c = (C(N + K - 1, K - 1) + M - (ans[i] % M)) % M;
    cout << c << endl;
  }
}

void calc(ll X)
{
  ll Y = (2 * C(N + K - 2, K - 2) + M - C(N + K - 3, K - 3)) % M;
  ll c = (((X - 1) / 2) * Y) % M;
  if (X % 2 == 0)
  {
    c += C(N + K - 2, K - 2);
    c %= M;
    c += C(N + K - 3, K - 2);
    c %= M;
  }
  ans[X] = c % M;
}

int main()
{
  init();
  cin >> K >> N;
  for (auto i = 2; i <= 2 * K; i++)
  {
    calc(i);
  }
  flush();
}<|endoftext|>


<commit_before>extern "C"
{
#include "cones.h"
}

#include <mex.h>
#include <math.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	const double *input;

	input = mxGetPr(prhs[0]);
	float fieldOfViewXY = input[0];
	float fieldOfViewZ = input[1];
	input = mxGetPr(prhs[1]);
	float spatialResolutionXY = input[0];
	float spatialResolutionZ = input[1];
	float readoutDuration = *mxGetPr(prhs[2]);

	float filterFieldOfView = fmaxf(fieldOfViewXY, fieldOfViewZ);

	float samplingInterval = 4e-6;
	float maxGradientAmplitude = 4;
	float maxSlewRate = 15000;

	generateCones(fieldOfViewXY, fieldOfViewZ, NULL, spatialResolutionXY, spatialResolutionZ, 16, 1, NoCompensation, readoutDuration, samplingInterval, filterFieldOfView, maxGradientAmplitude, maxSlewRate, StoreAll);
}

<commit_msg>outputs gradients and kspace coordinates<commit_after>extern "C"
{
#include "cones.h"
}

#include <mex.h>
#include <math.h>
#include <string.h>
#include <vector>

mxArray* arrayToMxArray(void* array, mxClassID classID, mxComplexity complexity, std::vector<mwSize> size)
{
	int elementSize;
	switch(classID)
	{
		case mxSINGLE_CLASS:
			elementSize = sizeof(float);
			break;
		default:
			exit(1);
	}
	mxArray* mxarray = mxCreateNumericArray(3, size.data(), classID, complexity);
	int points = size[0];
	for(int n=1; n<size.size(); n++)
		points *= size[n];
	memcpy(mxGetData(mxarray), array, points*elementSize);

	return mxarray;
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	const double *input;

	input = mxGetPr(prhs[0]);
	float fieldOfViewXY = input[0];
	float fieldOfViewZ = input[1];
	input = mxGetPr(prhs[1]);
	float spatialResolutionXY = input[0];
	float spatialResolutionZ = input[1];
	float readoutDuration = *mxGetPr(prhs[2]);

	float filterFieldOfView = fmaxf(fieldOfViewXY, fieldOfViewZ);

	float samplingInterval = 4e-6;
	float maxGradientAmplitude = 4;
	float maxSlewRate = 15000;

	Cones* cones = generateCones(fieldOfViewXY, fieldOfViewZ, NULL, spatialResolutionXY, spatialResolutionZ, 16, 1, NoCompensation, readoutDuration, samplingInterval, filterFieldOfView, maxGradientAmplitude, maxSlewRate, StoreAll);
	Trajectory* trajectory = &cones->trajectory;

	if(!nlhs)
		return;

	const char* infoFields[] = {"fieldOfView", "spatialResolution"};
	mxArray* info = mxCreateStructMatrix(1, 1, 2, infoFields);
	mxSetField(info, 0, "fieldOfView", mxCreateDoubleMatrix(3,1,mxREAL));
	mxSetField(info, 0, "spatialResolution", mxCreateDoubleMatrix(3,1,mxREAL));
	for(int d=0; d<3; d++)
	{
		mxGetPr(mxGetField(info, 0, "fieldOfView"))[d] = cones->trajectory.fieldOfView[d];
		mxGetPr(mxGetField(info, 0, "spatialResolution"))[d] = cones->trajectory.spatialResolution[d];
	}

	const char* outputFields[] = {"info","gradients","kspace"};
	mxArray* output = mxCreateStructMatrix(1, 1, 3, outputFields);
	mxSetField(output, 0, "info", info);

	std::vector<mwSize> gradientDimensions = {trajectory->waveformPoints, 3, trajectory->readouts};
	mxArray* gradientWaveforms = arrayToMxArray(trajectory->gradientWaveforms, mxSINGLE_CLASS, mxREAL, gradientDimensions);
	mxSetField(output, 0, "gradients", gradientWaveforms);

	std::vector<mwSize> trajectoryDimensions = {trajectory->readoutPoints, 3, trajectory->readouts};
	mxArray* kSpaceCoordinates = arrayToMxArray(trajectory->kSpaceCoordinates, mxSINGLE_CLASS, mxREAL, trajectoryDimensions);
	mxSetField(output, 0, "kspace", kSpaceCoordinates);

	plhs[0] = output;

	freeCones(cones);
}

<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 7/14/2019, 10:10:17 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) { return (*this *= power(MOD - 2)); }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 1e6 + 10;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// const double epsilon = 1e-10;
// const ll infty = 1000000000000000LL;
// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

int N, M;
using edge = tuple<int, int>;
using info = tuple<int, int>;
set<int> V[100010];
vector<edge> ans;
bool visited[100010];
int cnt[100010];
priority_queue<info, vector<info>, greater<info>> Q;

void Yes()
{
  assert((int)ans.size() == M);
  for (auto x : ans)
  {
    cout << get<0>(x) << " " << get<1>(x) << endl;
  }
  exit(0);
}
void No()
{
  cout << "-1" << endl;
  exit(0);
}

void solve()
{
  for (auto i = 0; i < N; i++)
  {
    Q.push(info(V[i].size(), i));
  }
  while (!Q.empty())
  {
    // int sz = get<0>(Q.top());
    int v = get<1>(Q.top());
    Q.pop();
    if (!visited[v])
    {
      visited[v] = true;
      for (auto x : V[v])
      {
        ans.push_back(edge(x, v));
        cnt[x]++;
        V[x].erase(v);
        Q.push(info(V[x].size(), x));
      }
      V[v].clear();
      if (cnt[v] % 2 == 1)
      {
        auto it = ans.end();
        --it;
        cnt[get<0>(*it)]--;
        swap(get<0>(*it), get<1>(*it));
      }
    }
  }
}

int main()
{
  cin >> N >> M;
  for (auto i = 0; i < M; i++)
  {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    V[a].insert(b);
    V[b].insert(a);
  }
  if (M % 2 == 1)
  {
    No();
  }
  solve();
  Yes();
}<commit_msg>tried B.cpp to 'B'<commit_after>#define DEBUG 1
/**
 * File    : B.cpp
 * Author  : Kazune Takahashi
 * Created : 7/14/2019, 10:10:17 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) { return (*this *= power(MOD - 2)); }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 1e6 + 10;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// const double epsilon = 1e-10;
// const ll infty = 1000000000000000LL;
// const int dx[4] = {1, 0, -1, 0};
// const int dy[4] = {0, 1, 0, -1};

int N, M;
using edge = tuple<int, int>;
using info = tuple<int, int>;
set<int> V[100010];
vector<edge> ans;
bool visited[100010];
int cnt[100010];
priority_queue<info, vector<info>, greater<info>> Q;

void Yes()
{
  assert((int)ans.size() == M);
  for (auto x : ans)
  {
    cout << get<0>(x) + 1 << " " << get<1>(x) + 1 << endl;
  }
  exit(0);
}
void No()
{
  cout << "-1" << endl;
  exit(0);
}

void solve()
{
  for (auto i = 0; i < N; i++)
  {
    Q.push(info(V[i].size(), i));
  }
  while (!Q.empty())
  {
    // int sz = get<0>(Q.top());
    int v = get<1>(Q.top());
    Q.pop();
    if (!visited[v])
    {
      visited[v] = true;
      for (auto x : V[v])
      {
        ans.push_back(edge(x, v));
        cnt[x]++;
        V[x].erase(v);
        Q.push(info(V[x].size(), x));
      }
      V[v].clear();
      if (cnt[v] % 2 == 1)
      {
        auto it = ans.end();
        --it;
        cnt[get<0>(*it)]--;
        swap(get<0>(*it), get<1>(*it));
      }
    }
  }
}

int main()
{
  cin >> N >> M;
  for (auto i = 0; i < M; i++)
  {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    V[a].insert(b);
    V[b].insert(a);
  }
  if (M % 2 == 1)
  {
    No();
  }
  solve();
  Yes();
}<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 7/28/2019, 1:18:49 AM
 * 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 << "-1" << endl;
  exit(0);
}

class UnionFind
{
  vector<long long> par;

public:
  UnionFind() {}
  UnionFind(int n) : par(n, -1) {}

  bool is_same(int x, int y)
  {
    return root(x) == root(y);
  }

  bool merge(int x, int y)
  {
    x = root(x);
    y = root(y);
    if (x == y)
    {
      return false;
    }
    if (par[x] > par[y])
    {
      swap(x, y);
    }
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  long long size(int x)
  {
    return -par[root(x)];
  }

private:
  int root(int x)
  {
    if (par[x] < 0)
    {
      return x;
    }
    return par[x] = root(par[x]);
  }
};

class RollingHash
{ // S の方を変更する処理はまだ書いていない。
  string S;
  static mint B;
  vector<mint> H;

public:
  RollingHash(string s, int t = -1) : S{s}
  {
    if (t == -1)
    {
      t = s.size();
    }
    assert(1 <= t && t <= static_cast<int>(s.size()));
    H = make_init_hash(t);
  }

  RollingHash(string s, size_t t)
  {
    RollingHash{s, static_cast<int>(t)};
  }

  size_t size() { return S.size() + 1u - H.size(); }

  const mint operator[](size_t t) const { return H[t]; }

private:
  mint Sm(size_t k) { return static_cast<mint>(S[k]); }
  vector<mint> make_init_hash(int);
};

mint RollingHash::B = 1234567;

vector<mint> RollingHash::make_init_hash(int t)
{
  vector<mint> res(S.size() + 1 - t);
  mint now = 0;
  const mint pb = B.power(t);
  for (auto i = 0; i < t; i++)
  {
    now = now * B + Sm(i);
  }
  res[0] = now;
  for (auto i = 0u; i < res.size() - 1; i++)
  {
    res[i + 1] = res[i] * B - Sm(i) * pb + Sm(i + t);
  }
  return res;
}

int main()
{
  string S, T;
  cin >> S >> T;
  int N = S.size();
  stringstream U{};
  U << S << S << S;
  while (U.str().size() < T.size() * 2)
  {
    U << S;
  }
  S = U.str();
#if DEBUG == 1
  cerr << "S = " << S << endl;
#endif
  vector<bool> ok(N, false);
  RollingHash rh_S{S, T.size()};
  RollingHash rh_T{T};
  const mint base = rh_T[0];
#if DEBUG == 1
  cerr << "rh_S.size() = " << rh_S.size() << endl;
  cerr << "rh_T.size() = " << rh_T.size() << endl;
  cerr << "base = " << base << endl;
#endif
  for (auto i = 0; i < N; i++)
  {
    ok[i] = (rh_S[i] == base);
#if DEBUG == 1
    cerr << "rh_S[" << i << "] = " << rh_S[i] << endl;
    if (ok[i])
    {
      cerr << "ok[" << i << "]" << endl;
    }
#endif
  }
  UnionFind uf{N};
  for (auto i = 0; i < N; i++)
  {
    int j = (i + T.size()) % N;
    if (ok[i] && ok[j])
    {
      if (!uf.merge(i, j))
      {
        No();
      }
    }
  }
  ll ans = 0;
  for (auto i = 0; i < N; i++)
  {
    if (ok[i])
    {
      maxs(ans, uf.size(i));
    }
  }
  cout << ans << endl;
}<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 7/28/2019, 1:18:49 AM
 * 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 << "-1" << endl;
  exit(0);
}

class UnionFind
{
  vector<long long> par;

public:
  UnionFind() {}
  UnionFind(int n) : par(n, -1) {}

  bool is_same(int x, int y)
  {
    return root(x) == root(y);
  }

  bool merge(int x, int y)
  {
    x = root(x);
    y = root(y);
    if (x == y)
    {
      return false;
    }
    if (par[x] > par[y])
    {
      swap(x, y);
    }
    par[x] += par[y];
    par[y] = x;
    return true;
  }

  long long size(int x)
  {
    return -par[root(x)];
  }

private:
  int root(int x)
  {
    if (par[x] < 0)
    {
      return x;
    }
    return par[x] = root(par[x]);
  }
};

class RollingHash
{
  string S;
  static mint B;
  vector<mint> H;

public:
  RollingHash(string s, int t = -1) : S{s}
  {
    if (t == -1)
    {
      t = s.size();
    }
    assert(1 <= t && t <= static_cast<int>(s.size()));
    H = make_init_hash(t);
  }

  RollingHash(string s, size_t t)
  {
    RollingHash{s, static_cast<int>(t)};
  }

  size_t size() { return S.size() + 1u - H.size(); }

  const mint operator[](size_t t) const { return H[t]; }

private:
  mint Sm(size_t k) { return static_cast<mint>(S[k]); }
  vector<mint> make_init_hash(int);
};

mint RollingHash::B = 1234567;

vector<mint> RollingHash::make_init_hash(int t)
{
  vector<mint> res(S.size() + 1 - t);
#if DEBUG == 1
  cerr << "res.size() = " << res.size() << endl;
#endif
  mint now = 0;
  const mint pb = B.power(t);
  for (auto i = 0; i < t; i++)
  {
    now = now * B + Sm(i);
  }
  res[0] = now;
  for (auto i = 0u; i < res.size() - 1; i++)
  {
    res[i + 1] = res[i] * B - Sm(i) * pb + Sm(i + t);
  }
  return res;
}

int main()
{
  string S, T;
  cin >> S >> T;
  int N = S.size();
  stringstream U{};
  U << S << S << S;
  while (U.str().size() < T.size() * 2)
  {
    U << S;
  }
  S = U.str();
#if DEBUG == 1
  cerr << "S = " << S << endl;
#endif
  vector<bool> ok(N, false);
  RollingHash rh_S{S, T.size()};
  RollingHash rh_T{T};
  const mint base = rh_T[0];
#if DEBUG == 1
  cerr << "rh_S.size() = " << rh_S.size() << endl;
  cerr << "rh_T.size() = " << rh_T.size() << endl;
  cerr << "base = " << base << endl;
#endif
  for (auto i = 0; i < N; i++)
  {
    ok[i] = (rh_S[i] == base);
#if DEBUG == 1
    cerr << "rh_S[" << i << "] = " << rh_S[i] << endl;
    if (ok[i])
    {
      cerr << "ok[" << i << "]" << endl;
    }
#endif
  }
  UnionFind uf{N};
  for (auto i = 0; i < N; i++)
  {
    int j = (i + T.size()) % N;
    if (ok[i] && ok[j])
    {
      if (!uf.merge(i, j))
      {
        No();
      }
    }
  }
  ll ans = 0;
  for (auto i = 0; i < N; i++)
  {
    if (ok[i])
    {
      maxs(ans, uf.size(i));
    }
  }
  cout << ans << endl;
}<|endoftext|>


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

template <typename T>
class SegTree
{ // 0-indexed, [0, N).
private:
  int N;
  T *dat;
  T UNIT; // モノイドの単位元

  static T func(T x, T y)
  { // ここで演算を定義する。圏、特にモノイドであればなんでも良い。
    // 実装はモノイドであるとする。
    // return min(x, y);
    return x + y;
  }

  static T _update(T x, T y)
  { // update で 値をどうするかを書く。
    // return y;
    return func(x, y);
  }

public:
  SegTree() {}

  SegTree(int n, T unit)
  { // ここで unit を定義するのも変な実装だけどめんどいからこれでいい。
    // min の場合は INFTY = (1LL << 60)
    // + の場合は 0 とする。
    UNIT = unit;
    N = 1;
    while (N < n)
    {
      N *= 2;
    }
    dat = new T[2 * N - 1];
    for (auto i = 0; i < 2 * N - 1; ++i)
    {
      dat[i] = UNIT;
    }
  }

  void update(int k, T a)
  {
    k += N - 1;
    dat[k] = _update(dat[k], a);
    while (k > 0)
    {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

private:
  T find(int a, int b, int k, int l, int r)
  {
    if (r <= a || b <= l)
      return UNIT;
    if (a <= l && r <= b)
      return dat[k];
    T vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return func(vl, vr);
  }

public:
  T find(int a, int b)
  { // [a, b) の find をする。
    return find(a, b, 0, 0, N);
  }
};

using info = tuple<ll, ll, ll, ll>;
using point = tuple<ll, ll, int>;

vector<point> V;
vector<info> I;

int main()
{
  int N;
  cin >> N;
  set<int> set_x, set_y;
  for (auto i = 0; i < N; i++)
  {
    int x, y;
    cin >> x >> y;
    V.emplace_back(x, y, i);
    set_x.insert(x);
    set_y.insert(y);
  }
  I.resize(N);
  map<int, int> map_x, map_y;
  {
    auto it = set_x.begin();
    for (auto i = 0; i < N; i++)
    {
      map_x[*it] = i;
      ++it;
    }
  }
  {
    auto it = set_y.begin();
    for (auto i = 0; i < N; i++)
    {
      map_y[*it] = i;
      ++it;
    }
  }
  for (auto i = 0; i < N; i++)
  {
    get<0>(V[i]) = map_x[get<0>(V[i])];
    get<1>(V[i]) = map_y[get<0>(V[i])];
  }
  sort(V.begin(), V.end());
  {
    SegTree<ll> tree{N, 0};
    for (auto i = 0; i < N; i++)
    {
      int x, y, ind;
      tie(x, y, ind) = V[i];
      ll k = tree.find(0, y);
      get<0>(I[ind]) = k;
      get<1>(I[ind]) = i - k;
      tree.update(y, 1);
    }
  }
  reverse(V.begin(), V.end());
  {
    SegTree<ll> tree{N, 0};
    for (auto i = 0; i < N; i++)
    {
      int x, y, ind;
      tie(x, y, ind) = V[i];
      ll k = tree.find(0, y);
      get<2>(I[ind]) = k;
      get<3>(I[ind]) = i - k;
      tree.update(y, 1);
    }
  }
  mint ans = 0;
  for (auto i = 0; i < N; i++)
  {
    ll a, b, c, d;
    tie(a, b, c, d) = I[i];
#if DEBUG == 1
    cerr << "(" << a << ", " << b << ", " << c << ", " << d << ")" << endl;
#endif
    ans += mint{2}.power(N) - 1;
    ans -= mint{2}.power(a + b);
    ans -= mint{2}.power(a + c);
    ans -= mint{2}.power(b + d);
    ans -= mint{2}.power(c + d);
    ans += mint{2}.power(a);
    ans += mint{2}.power(b);
    ans += mint{2}.power(c);
    ans += mint{2}.power(d);
  }
  cout << ans << endl;
}<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 8/5/2019, 1:06:42 AM
 * Powered by Visual Studio Code
 */
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <complex>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <functional>
#include <random>
#include <chrono>
#include <cctype>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint
{
public:
  static ll MOD;
  ll x;
  mint() : x(0) {}
  mint(ll x) : x(x % MOD) {}
  mint operator-() const { return x ? MOD - x : 0; }
  mint &operator+=(const mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  mint &operator-=(const mint &a) { return *this += -a; }
  mint &operator*=(const mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint &operator/=(const mint &a)
  {
    mint b{a};
    return *this *= b.power(MOD - 2);
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
  const mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      mint half = power(N / 2);
      return half * half;
    }
  }
};
ll mint::MOD = 998244353;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination
{
public:
  vector<mint> inv, fact, factinv;
  static int MAX_SIZE;
  combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1; i < MAX_SIZE; i++)
    {
      fact[i] = mint(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  mint operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

template <typename T>
class SegTree
{ // 0-indexed, [0, N).
private:
  int N;
  T *dat;
  T UNIT; // モノイドの単位元

  static T func(T x, T y)
  { // ここで演算を定義する。圏、特にモノイドであればなんでも良い。
    // 実装はモノイドであるとする。
    // return min(x, y);
    return x + y;
  }

  static T _update(T x, T y)
  { // update で 値をどうするかを書く。
    // return y;
    return func(x, y);
  }

public:
  SegTree() {}

  SegTree(int n, T unit)
  { // ここで unit を定義するのも変な実装だけどめんどいからこれでいい。
    // min の場合は INFTY = (1LL << 60)
    // + の場合は 0 とする。
    UNIT = unit;
    N = 1;
    while (N < n)
    {
      N *= 2;
    }
    dat = new T[2 * N - 1];
    for (auto i = 0; i < 2 * N - 1; ++i)
    {
      dat[i] = UNIT;
    }
  }

  void update(int k, T a)
  {
    k += N - 1;
    dat[k] = _update(dat[k], a);
    while (k > 0)
    {
      k = (k - 1) / 2;
      dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }

private:
  T find(int a, int b, int k, int l, int r)
  {
    if (r <= a || b <= l)
      return UNIT;
    if (a <= l && r <= b)
      return dat[k];
    T vl = find(a, b, k * 2 + 1, l, (l + r) / 2);
    T vr = find(a, b, k * 2 + 2, (l + r) / 2, r);
    return func(vl, vr);
  }

public:
  T find(int a, int b)
  { // [a, b) の find をする。
    return find(a, b, 0, 0, N);
  }
};

using info = tuple<ll, ll, ll, ll>;
using point = tuple<ll, ll, int>;

int N;
vector<point> V;
vector<info> I;

mint f(info x)
{
  ll a, b, c, d;
  tie(a, b, c, d) = x;
  mint res = 0;
#if DEBUG == 1
  cerr << "(" << a << ", " << b << ", " << c << ", " << d << ")" << endl;
#endif
  res += mint{2}.power(N) - 1;
  res -= mint{2}.power(a + b);
  res -= mint{2}.power(a + c);
  res -= mint{2}.power(b + d);
  res -= mint{2}.power(c + d);
  res += mint{2}.power(a);
  res += mint{2}.power(b);
  res += mint{2}.power(c);
  res += mint{2}.power(d);
#if DEBUG == 1
  cerr << res << endl;
#endif
  return res;
}

int main()
{
  cin >> N;
  set<int> set_x, set_y;
  for (auto i = 0; i < N; i++)
  {
    int x, y;
    cin >> x >> y;
    V.emplace_back(x, y, i);
    set_x.insert(x);
    set_y.insert(y);
  }
  I.resize(N);
  map<int, int> map_x, map_y;
  {
    auto it = set_x.begin();
    for (auto i = 0; i < N; i++)
    {
      map_x[*it] = i;
      ++it;
    }
  }
  {
    auto it = set_y.begin();
    for (auto i = 0; i < N; i++)
    {
      map_y[*it] = i;
      ++it;
    }
  }
  for (auto i = 0; i < N; i++)
  {
    get<0>(V[i]) = map_x[get<0>(V[i])];
    get<1>(V[i]) = map_y[get<0>(V[i])];
  }
  sort(V.begin(), V.end());
  {
    SegTree<ll> tree{N, 0};
    for (auto i = 0; i < N; i++)
    {
      int x, y, ind;
      tie(x, y, ind) = V[i];
      ll k = tree.find(0, y);
      get<0>(I[ind]) = k;
      get<1>(I[ind]) = i - k;
      tree.update(y, 1);
    }
  }
  reverse(V.begin(), V.end());
  {
    SegTree<ll> tree{N, 0};
    for (auto i = 0; i < N; i++)
    {
      int x, y, ind;
      tie(x, y, ind) = V[i];
      ll k = tree.find(0, y);
      get<2>(I[ind]) = k;
      get<3>(I[ind]) = i - k;
      tree.update(y, 1);
    }
  }
  mint ans = 0;
  for (auto i = 0; i < N; i++)
  {
    ans += f(I[i]);
  }
  cout << ans << endl;
}<|endoftext|>


<commit_before>// This file may be redistributed and modified only under the terms of
// the GNU Lesser General Public License (See COPYING for details).
// Copyright (C) 2000 Stefanus Du Toit

#include <string>
#include "Decoder.h"

using namespace Atlas::Message;
using namespace std;

#define ARR(a, b) if (parent == a) { \
    b obj; \
    for (Object::MapType::const_iterator I = o.AsMap().begin(); \
            I != o.AsMap().end(); I++) \
        obj.SetAttr(I->first, I->second); \
    ObjectArrived(obj);\
}

namespace Atlas { namespace Objects {

Decoder::~Decoder()
{
}

void Decoder::ObjectArrived(const Object& o)
{
    if (!o.IsMap()) return;
    if (o.AsMap().find("parents") == o.AsMap().end())
        { UnknownObjectArrived(o); return; }
    if ((*o.AsMap().find("parents")).second.AsList().size() != 1)
        { UnknownObjectArrived(o); return; }

    string
        parent((*(*o.AsMap().find("parents")).second.AsList().begin()).AsString());

    ARR("root", Root)
    ARR("account", Entity::Account)
    ARR("admin", Entity::Admin)
    ARR("admin_entity", Entity::AdminEntity)
    ARR("game", Entity::Game)
    ARR("player", Entity::Player)
    ARR("root_entity", Entity::RootEntity)
    ARR("action", Operation::Action)
    ARR("appearance", Operation::Appearance)
    ARR("combine", Operation::Combine)
    ARR("create", Operation::Create)
    ARR("delete", Operation::Delete)
    ARR("disappearance", Operation::Disappearance)
    ARR("divide", Operation::Divide)
    ARR("error", Operation::Error)
    ARR("feel", Operation::Feel)
    ARR("get", Operation::Get)
    ARR("imaginary", Operation::Imaginary)
    ARR("info", Operation::Info)
    ARR("listen", Operation::Listen)
    ARR("login", Operation::Login)
    ARR("logout", Operation::Logout)
    ARR("look", Operation::Look)
    ARR("move", Operation::Move)
    ARR("perceive", Operation::Perceive)
    ARR("perception", Operation::Perception)
    ARR("root_operation", Operation::RootOperation)
    ARR("set", Operation::Set)
    ARR("sight", Operation::Sight)
    ARR("smell", Operation::Smell)
    ARR("sniff", Operation::Sniff)
    ARR("sound", Operation::Sound)
    ARR("talk", Operation::Talk)
    ARR("touch", Operation::Touch)

    UnknownObjectArrived(o);
}

} } // namespace Atlas::Objects
<commit_msg>Minor tweak to decoder that should improve performance<commit_after>// This file may be redistributed and modified only under the terms of
// the GNU Lesser General Public License (See COPYING for details).
// Copyright (C) 2000 Stefanus Du Toit

#include <string>
#include "Decoder.h"

using namespace Atlas::Message;
using namespace std;

#define ARR(a, b) if (parent == a) { \
    b obj; \
    for (Object::MapType::const_iterator I = o.AsMap().begin(); \
            I != o.AsMap().end(); I++) \
        obj.SetAttr(I->first, I->second); \
    ObjectArrived(obj);\
    return;\
}

namespace Atlas { namespace Objects {

Decoder::~Decoder()
{
}

void Decoder::ObjectArrived(const Object& o)
{
    if (!o.IsMap()) return;
    if (o.AsMap().find("parents") == o.AsMap().end())
        { UnknownObjectArrived(o); return; }
    if ((*o.AsMap().find("parents")).second.AsList().size() != 1)
        { UnknownObjectArrived(o); return; }

    string
        parent((*(*o.AsMap().find("parents")).second.AsList().begin()).AsString());

    ARR("root", Root)
    ARR("account", Entity::Account)
    ARR("admin", Entity::Admin)
    ARR("admin_entity", Entity::AdminEntity)
    ARR("game", Entity::Game)
    ARR("player", Entity::Player)
    ARR("root_entity", Entity::RootEntity)
    ARR("action", Operation::Action)
    ARR("appearance", Operation::Appearance)
    ARR("combine", Operation::Combine)
    ARR("create", Operation::Create)
    ARR("delete", Operation::Delete)
    ARR("disappearance", Operation::Disappearance)
    ARR("divide", Operation::Divide)
    ARR("error", Operation::Error)
    ARR("feel", Operation::Feel)
    ARR("get", Operation::Get)
    ARR("imaginary", Operation::Imaginary)
    ARR("info", Operation::Info)
    ARR("listen", Operation::Listen)
    ARR("login", Operation::Login)
    ARR("logout", Operation::Logout)
    ARR("look", Operation::Look)
    ARR("move", Operation::Move)
    ARR("perceive", Operation::Perceive)
    ARR("perception", Operation::Perception)
    ARR("root_operation", Operation::RootOperation)
    ARR("set", Operation::Set)
    ARR("sight", Operation::Sight)
    ARR("smell", Operation::Smell)
    ARR("sniff", Operation::Sniff)
    ARR("sound", Operation::Sound)
    ARR("talk", Operation::Talk)
    ARR("touch", Operation::Touch)

    UnknownObjectArrived(o);
}

} } // namespace Atlas::Objects
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 1/12/2020, 9:22:35 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>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using boost::rational;
using boost::multiprecision::cpp_int;
using namespace std;
using ll = long long;
// ----- 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} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----
int main()
{
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto i = 0; i < N; i++)
  {
    cin >> A[i];
  }
  combination C;
  sort(A.begin(), A.end());
  mint tmp0{0};
  for (auto i = 0; i < N; i++)
  {
    tmp0 += C(N - 1 - i, K - 1) * A[i];
  }
  reverse(A.begin(), A.end());
  mint tmp1{0};
  for (auto i = 0; i < N; i++)
  {
    tmp1 += C(N - 1 - i, K - 1) * A[i];
  }
  cerr << "tmp0 = " << tmp0 << endl;
  cerr << "tmp1 = " << tmp1 << endl;
  // cout << tmp1 - tmp0 << endl;
}
<commit_msg>tried E.cpp to 'E'<commit_after>#define DEBUG 1
/**
 * File    : E.cpp
 * Author  : Kazune Takahashi
 * Created : 1/12/2020, 9:22:35 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>
// ----- boost -----
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
// ----- using directives and manipulations -----
using boost::rational;
using boost::multiprecision::cpp_int;
using namespace std;
using ll = long long;
// ----- 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} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(const Mint &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(const Mint &a) { return *this += -a; }
  Mint &operator*=(const Mint &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(const Mint &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(const Mint &a) const { return Mint(*this) += a; }
  Mint operator-(const Mint &a) const { return Mint(*this) -= a; }
  Mint operator*(const Mint &a) const { return Mint(*this) *= a; }
  Mint operator/(const Mint &a) const { return Mint(*this) /= a; }
  bool operator<(const Mint &a) const { return x < a.x; }
  bool operator<=(const Mint &a) const { return x <= a.x; }
  bool operator>(const Mint &a) const { return x > a.x; }
  bool operator>=(const Mint &a) const { return x >= a.x; }
  bool operator==(const Mint &a) const { return x == a.x; }
  bool operator!=(const Mint &a) const { return !(*this == a); }
  const Mint power(ll N)
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, const Mint<MOD> &rhs)
{
  return rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator-(ll lhs, const Mint<MOD> &rhs)
{
  return -rhs + lhs;
}
template <ll MOD>
Mint<MOD> operator*(ll lhs, const Mint<MOD> &rhs)
{
  return rhs * lhs;
}
template <ll MOD>
Mint<MOD> operator/(ll lhs, const Mint<MOD> &rhs)
{
  return Mint<MOD>{lhs} / rhs;
}
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a)
{
  return stream >> a.x;
}
template <ll MOD>
ostream &operator<<(ostream &stream, const Mint<MOD> &a)
{
  return stream << a.x;
}
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i = 2LL; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i = 1LL; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1000000000000000LL};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}
// ----- main() -----
int main()
{
  ll N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (auto i = 0; i < N; i++)
  {
    cin >> A[i];
  }
  combination C;
  sort(A.begin(), A.end());
  mint tmp0{0};
  for (auto i = 0; i < N; i++)
  {
    tmp0 += C(N - 1 - i, K - 1) * (MOD + A[i]);
  }
  reverse(A.begin(), A.end());
  mint tmp1{0};
  for (auto i = 0; i < N; i++)
  {
    tmp1 += C(N - 1 - i, K - 1) * (MOD + A[i]);
  }
  cerr << "tmp0 = " << tmp0 << endl;
  cerr << "tmp1 = " << tmp1 << endl;
  // cout << tmp1 - tmp0 << endl;
}
<|endoftext|>


<commit_before>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/6/23 5:42:51
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::integer::gcd; // for C++14 or for cpp_int
using boost::integer::lcm; // for C++14 or for cpp_int
using boost::multiprecision::cpp_int;
using ll = long long;
using ld = long double;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1'000'000'007LL};
// constexpr ll MOD{998'244'353LL}; // be careful
constexpr ll MAX_SIZE{3'000'010LL};
// constexpr ll MAX_SIZE{30'000'010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
bool ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
    return true;
  }
  return false;
}
template <typename T>
bool ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
    return true;
  }
  return false;
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(Mint const &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(Mint const &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(Mint const &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(Mint const &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(Mint const &a) const { return Mint(*this) += a; }
  Mint operator-(Mint const &a) const { return Mint(*this) -= a; }
  Mint operator*(Mint const &a) const { return Mint(*this) *= a; }
  Mint operator/(Mint const &a) const { return Mint(*this) /= a; }
  bool operator<(Mint const &a) const { return x < a.x; }
  bool operator<=(Mint const &a) const { return x <= a.x; }
  bool operator>(Mint const &a) const { return x > a.x; }
  bool operator>=(Mint const &a) const { return x >= a.x; }
  bool operator==(Mint const &a) const { return x == a.x; }
  bool operator!=(Mint const &a) const { return !(*this == a); }
  Mint power(ll N) const
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, Mint<MOD> const &rhs) { return rhs + lhs; }
template <ll MOD>
Mint<MOD> operator-(ll lhs, Mint<MOD> const &rhs) { return -rhs + lhs; }
template <ll MOD>
Mint<MOD> operator*(ll lhs, Mint<MOD> const &rhs) { return rhs * lhs; }
template <ll MOD>
Mint<MOD> operator/(ll lhs, Mint<MOD> const &rhs) { return Mint<MOD>{lhs} / rhs; }
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a) { return stream >> a.x; }
template <ll MOD>
ostream &operator<<(ostream &stream, Mint<MOD> const &a) { return stream << a.x; }
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i{2LL}; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i{1LL}; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
// ----- for C++17 -----
template <typename T, typename enable_if<is_integral<T>::value>::type * = nullptr>
size_t popcount(T x) { return bitset<64>(x).count(); }
size_t popcount(string const &S) { return bitset<200010>{S}.count(); }
// ----- Infty -----
template <typename T>
constexpr T Infty() { return numeric_limits<T>::max(); }
template <typename T>
constexpr T mInfty() { return numeric_limits<T>::min(); }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1'000'000'000'000'010LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

// ----- ReadGraph -----
// Referring to ymatsux-san's source code: https://atcoder.jp/contests/abc138/submissions/7016619

struct Edge
{
  int src, dst, id, rev;
  // ll cost;
  Edge() {}
  Edge(int src, int dst, int id, int rev) : src{src}, dst{dst}, id{id}, rev{rev} {}

  void added_edge(vector<vector<Edge>> &V)
  {
    V[src].push_back(*this);
  }

  void added_rev(vector<vector<Edge>> &V)
  {
    V[dst].push_back(rev_edge());
  }

  Edge rev_edge()
  {
    Edge edge{*this};
    swap(edge.src, edge.dst);
    swap(edge.id, edge.rev);
    return edge;
  }
};

enum class GraphType
{
  Undirected,
  Directed,
  RevEdge,
};

tuple<vector<vector<Edge>>, vector<Edge>> ReadGraphWithEdges(int N, int M, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  vector<vector<Edge>> V(N);
  vector<Edge> E;
  for (auto i = 0; i < M; ++i)
  {
    int v, w;
    cin >> v >> w;
    if (is_one_indexed)
    {
      --v;
      --w;
    }
    switch (type)
    {
    case GraphType::Undirected:
    {
      Edge edge{v, w, static_cast<int>(E.size()), static_cast<int>(E.size())};
      edge.added_edge(V);
      edge.added_rev(V);
      E.push_back(edge);
      break;
    }
    case GraphType::Directed:
    {
      Edge edge{v, w, static_cast<int>(E.size()), static_cast<int>(E.size())};
      edge.added_edge(V);
      E.push_back(edge);
      break;
    }
    case GraphType::RevEdge:
    {
      Edge edge{v, w, static_cast<int>(E.size()), static_cast<int>(E.size()) + 1};
      edge.added_edge(V);
      edge.added_rev(V);
      E.push_back(edge);
      E.push_back(edge.rev_edge());
      break;
    }
    default:
      break;
    }
  }
  return make_tuple(V, E);
}

vector<vector<Edge>> ReadGraph(int N, int M, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  return get<0>(ReadGraphWithEdges(N, M, type, is_one_indexed));
}

tuple<vector<vector<Edge>>, vector<Edge>> ReadTreeWithEdges(int N, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  return ReadGraphWithEdges(N, N - 1, type, is_one_indexed);
}

vector<vector<Edge>> ReadTree(int N, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  return ReadGraph(N, N - 1, type, is_one_indexed);
}

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

chrono::system_clock::time_point time_start = chrono::system_clock::now();

void TimeStamp()
{
  cout << "Time: " << chrono::duration_cast<std::chrono::milliseconds>(chrono::system_clock::now() - time_start).count() << endl;
}

struct SubTree
{
  ll id, size;
};

class Solve
{
  ll N, id;
  vector<queue<SubTree>> Q;
  vector<ll> C;
  vector<vector<Edge>> V;
  vector<vector<ll>> S;
  vector<ll> color_to_count;

public:
  Solve(ll N) : N{N}, id{0}, Q(N), C(N), S(N), color_to_count(N, 0)
  {
    for (auto i{0}; i < N; ++i)
    {
      cin >> C[i];
      C[i]--;
      color_to_count[C[i]]++;
    }
#if DEBUG == 1
    cerr << "Here" << endl;
#endif
    V = ReadTree(N);
#if DEBUG == 1
    cerr << "Here" << endl;
#endif
  }

  void flush()
  {
    dfs();
    for (auto i{0}; i < N; ++i)
    {
      auto sum{accumulate(S[i].begin(), S[i].end(), 0)};
      S[i].push_back(N - sum - color_to_count[i]);
      ll ans{0};
      for (auto e : S[i])
      {
        ans += e * (e + 1) / 2;
      }
      cout << N * (N + 1) / 2 - ans << endl;
    }
  }

private:
  ll dfs(ll src = 0, ll parent = -1)
  {
    auto src_id{id++};
    auto &subtree_queue{Q[C[src]]};
    for (auto const &e : V[src])
    {
      if (e.dst == parent)
      {
        continue;
      }
      auto colorless_tree_size{dfs(e.dst, src)};
      while (!subtree_queue.empty() && subtree_queue.front().id > src_id)
      {
        colorless_tree_size -= subtree_queue.front().size;
        subtree_queue.pop();
      }
      S[C[src]].push_back(colorless_tree_size);
    }
    auto subtree_size{id - src_id};
    subtree_queue.push(SubTree{src_id, subtree_size});
    return subtree_size;
  }
};

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

int main()
{
  ll N;
  cin >> N;
  Solve solve(N);
  solve.flush();
}
<commit_msg>tried F.cpp to 'F'<commit_after>#define DEBUG 1
/**
 * File    : F.cpp
 * Author  : Kazune Takahashi
 * Created : 2020/6/23 5:42:51
 * Powered by Visual Studio Code
 */
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// ----- boost -----
#include <boost/integer/common_factor_rt.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
// ----- using directives and manipulations -----
using namespace std;
using boost::rational;
using boost::integer::gcd; // for C++14 or for cpp_int
using boost::integer::lcm; // for C++14 or for cpp_int
using boost::multiprecision::cpp_int;
using ll = long long;
using ld = long double;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// ----- constexpr for Mint and Combination -----
constexpr ll MOD{1'000'000'007LL};
// constexpr ll MOD{998'244'353LL}; // be careful
constexpr ll MAX_SIZE{3'000'010LL};
// constexpr ll MAX_SIZE{30'000'010LL}; // if 10^7 is needed
// ----- ch_max and ch_min -----
template <typename T>
bool ch_max(T &left, T right)
{
  if (left < right)
  {
    left = right;
    return true;
  }
  return false;
}
template <typename T>
bool ch_min(T &left, T right)
{
  if (left > right)
  {
    left = right;
    return true;
  }
  return false;
}
// ----- Mint -----
template <ll MOD = MOD>
class Mint
{
public:
  ll x;
  Mint() : x{0LL} {}
  Mint(ll x) : x{(x % MOD + MOD) % MOD} {}
  Mint operator-() const { return x ? MOD - x : 0; }
  Mint &operator+=(Mint const &a)
  {
    if ((x += a.x) >= MOD)
    {
      x -= MOD;
    }
    return *this;
  }
  Mint &operator-=(Mint const &a) { return *this += -a; }
  Mint &operator++() { return *this += 1; }
  Mint operator++(int)
  {
    Mint tmp{*this};
    ++*this;
    return tmp;
  }
  Mint &operator--() { return *this -= 1; }
  Mint operator--(int)
  {
    Mint tmp{*this};
    --*this;
    return tmp;
  }
  Mint &operator*=(Mint const &a)
  {
    (x *= a.x) %= MOD;
    return *this;
  }
  Mint &operator/=(Mint const &a)
  {
    Mint b{a};
    return *this *= b.power(MOD - 2);
  }
  Mint operator+(Mint const &a) const { return Mint(*this) += a; }
  Mint operator-(Mint const &a) const { return Mint(*this) -= a; }
  Mint operator*(Mint const &a) const { return Mint(*this) *= a; }
  Mint operator/(Mint const &a) const { return Mint(*this) /= a; }
  bool operator<(Mint const &a) const { return x < a.x; }
  bool operator<=(Mint const &a) const { return x <= a.x; }
  bool operator>(Mint const &a) const { return x > a.x; }
  bool operator>=(Mint const &a) const { return x >= a.x; }
  bool operator==(Mint const &a) const { return x == a.x; }
  bool operator!=(Mint const &a) const { return !(*this == a); }
  Mint power(ll N) const
  {
    if (N == 0)
    {
      return 1;
    }
    else if (N % 2 == 1)
    {
      return *this * power(N - 1);
    }
    else
    {
      Mint half = power(N / 2);
      return half * half;
    }
  }
};
template <ll MOD>
Mint<MOD> operator+(ll lhs, Mint<MOD> const &rhs) { return rhs + lhs; }
template <ll MOD>
Mint<MOD> operator-(ll lhs, Mint<MOD> const &rhs) { return -rhs + lhs; }
template <ll MOD>
Mint<MOD> operator*(ll lhs, Mint<MOD> const &rhs) { return rhs * lhs; }
template <ll MOD>
Mint<MOD> operator/(ll lhs, Mint<MOD> const &rhs) { return Mint<MOD>{lhs} / rhs; }
template <ll MOD>
istream &operator>>(istream &stream, Mint<MOD> &a) { return stream >> a.x; }
template <ll MOD>
ostream &operator<<(ostream &stream, Mint<MOD> const &a) { return stream << a.x; }
// ----- Combination -----
template <ll MOD = MOD, ll MAX_SIZE = MAX_SIZE>
class Combination
{
public:
  vector<Mint<MOD>> inv, fact, factinv;
  Combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE)
  {
    inv[1] = 1;
    for (auto i{2LL}; i < MAX_SIZE; i++)
    {
      inv[i] = (-inv[MOD % i]) * (MOD / i);
    }
    fact[0] = factinv[0] = 1;
    for (auto i{1LL}; i < MAX_SIZE; i++)
    {
      fact[i] = Mint<MOD>(i) * fact[i - 1];
      factinv[i] = inv[i] * factinv[i - 1];
    }
  }
  Mint<MOD> operator()(int n, int k)
  {
    if (n >= 0 && k >= 0 && n - k >= 0)
    {
      return fact[n] * factinv[k] * factinv[n - k];
    }
    return 0;
  }
  Mint<MOD> catalan(int x, int y)
  {
    return (*this)(x + y, y) - (*this)(x + y, y - 1);
  }
};
// ----- for C++14 -----
using mint = Mint<MOD>;
using combination = Combination<MOD, MAX_SIZE>;
// ----- for C++17 -----
template <typename T, typename enable_if<is_integral<T>::value>::type * = nullptr>
size_t popcount(T x) { return bitset<64>(x).count(); }
size_t popcount(string const &S) { return bitset<200010>{S}.count(); }
// ----- Infty -----
template <typename T>
constexpr T Infty() { return numeric_limits<T>::max(); }
template <typename T>
constexpr T mInfty() { return numeric_limits<T>::min(); }
// ----- frequently used constexpr -----
// constexpr double epsilon{1e-10};
// constexpr ll infty{1'000'000'000'000'010LL}; // or
// constexpr int infty{1'000'000'010};
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
// ----- Yes() and No() -----
void Yes()
{
  cout << "Yes" << endl;
  exit(0);
}
void No()
{
  cout << "No" << endl;
  exit(0);
}

// ----- ReadGraph -----
// Referring to ymatsux-san's source code: https://atcoder.jp/contests/abc138/submissions/7016619

struct Edge
{
  int src, dst, id, rev;
  // ll cost;
  Edge() {}
  Edge(int src, int dst, int id, int rev) : src{src}, dst{dst}, id{id}, rev{rev} {}

  void added_edge(vector<vector<Edge>> &V)
  {
    V[src].push_back(*this);
  }

  void added_rev(vector<vector<Edge>> &V)
  {
    V[dst].push_back(rev_edge());
  }

  Edge rev_edge()
  {
    Edge edge{*this};
    swap(edge.src, edge.dst);
    swap(edge.id, edge.rev);
    return edge;
  }
};

enum class GraphType
{
  Undirected,
  Directed,
  RevEdge,
};

tuple<vector<vector<Edge>>, vector<Edge>> ReadGraphWithEdges(int N, int M, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  vector<vector<Edge>> V(N);
  vector<Edge> E;
  for (auto i = 0; i < M; ++i)
  {
    int v, w;
    cin >> v >> w;
    if (is_one_indexed)
    {
      --v;
      --w;
    }
    switch (type)
    {
    case GraphType::Undirected:
    {
      Edge edge{v, w, static_cast<int>(E.size()), static_cast<int>(E.size())};
      edge.added_edge(V);
      edge.added_rev(V);
      E.push_back(edge);
      break;
    }
    case GraphType::Directed:
    {
      Edge edge{v, w, static_cast<int>(E.size()), static_cast<int>(E.size())};
      edge.added_edge(V);
      E.push_back(edge);
      break;
    }
    case GraphType::RevEdge:
    {
      Edge edge{v, w, static_cast<int>(E.size()), static_cast<int>(E.size()) + 1};
      edge.added_edge(V);
      edge.added_rev(V);
      E.push_back(edge);
      E.push_back(edge.rev_edge());
      break;
    }
    default:
      break;
    }
  }
  return make_tuple(V, E);
}

vector<vector<Edge>> ReadGraph(int N, int M, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  return get<0>(ReadGraphWithEdges(N, M, type, is_one_indexed));
}

tuple<vector<vector<Edge>>, vector<Edge>> ReadTreeWithEdges(int N, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  return ReadGraphWithEdges(N, N - 1, type, is_one_indexed);
}

vector<vector<Edge>> ReadTree(int N, GraphType type = GraphType::Undirected, bool is_one_indexed = true)
{
  return ReadGraph(N, N - 1, type, is_one_indexed);
}

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

chrono::system_clock::time_point time_start = chrono::system_clock::now();

void TimeStamp()
{
  cout << "Time: " << chrono::duration_cast<std::chrono::milliseconds>(chrono::system_clock::now() - time_start).count() << endl;
}

struct SubTree
{
  ll id, size;
};

class Solve
{
  ll N, id;
  vector<queue<SubTree>> Q;
  vector<ll> C;
  vector<vector<Edge>> V;
  vector<vector<ll>> S;
  vector<ll> color_to_count;

public:
  Solve(ll N) : N{N}, id{0}, Q(N), C(N), S(N), color_to_count(N, 0)
  {
    for (auto i{0}; i < N; ++i)
    {
      cin >> C[i];
      C[i]--;
      color_to_count[C[i]]++;
    }
    V = ReadTree(N);
  }

  void flush()
  {
    dfs();
    for (auto i{0}; i < N; ++i)
    {
      auto sum{accumulate(S[i].begin(), S[i].end(), 0)};
      S[i].push_back(N - sum - color_to_count[i]);
      ll ans{0};
      for (auto e : S[i])
      {
        ans += e * (e + 1) / 2;
      }
      cout << N * (N + 1) / 2 - ans << endl;
    }
  }

private:
  ll dfs(ll src = 0, ll parent = -1)
  {
#if DEBUG == 1
    cerr << "src = " << src << endl;
    cout << "src = " << src << endl;
#endif
    auto src_id{id++};
    auto &subtree_queue{Q[C[src]]};
    for (auto const &e : V[src])
    {
      if (e.dst == parent)
      {
        continue;
      }
      auto colorless_tree_size{dfs(e.dst, src)};
      while (!subtree_queue.empty() && subtree_queue.front().id > src_id)
      {
        colorless_tree_size -= subtree_queue.front().size;
        subtree_queue.pop();
      }
      S[C[src]].push_back(colorless_tree_size);
    }
    auto subtree_size{id - src_id};
    subtree_queue.push(SubTree{src_id, subtree_size});
    return subtree_size;
  }
};

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

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