結果

問題 No.1935 Water Simulation
ユーザー miscalcmiscalc
提出日時 2022-05-13 21:57:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,932 bytes
コンパイル時間 4,659 ms
コンパイル使用メモリ 270,108 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 10:00:30
合計ジャッジ時間 5,618 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//*
#include <atcoder/all>
using namespace atcoder;
//using mint = modint998244353;
using mint = modint1000000007;
//*/
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tlll = tuple<ll, ll, ll>;
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll safemod(ll A, ll M) {return (A % M + M) % M;}
ll divfloor(ll A, ll B) {if (B < 0) {return divfloor(-A, -B);} return (A - safemod(A, B)) / B;}
ll divceil(ll A, ll B) {if (B < 0) {return divceil(-A, -B);} return divfloor(A + B - 1, B);}
ll mypowl(ll A, int B) {ll res = 1; for (int i = 0; i < B; i++) {res *= A;} return res;}
template<class T> void unique(vector<T> &V) {V.erase(unique(V.begin(), V.end()), V.end());}
template<class T> void sortunique(vector<T> &V) {sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end());}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)
template<class T> void printvec(const vector<T> &V) {int _n = V.size(); for (int i = 0; i < _n; i++) cout << V[i] << (i == _n - 1 ? "" : " ");cout << '\n';}
template<class T> void printvect(const vector<T> &V) {for (auto v : V) cout << v << '\n';}
template<class T> void printvec2(const vector<vector<T>> &V) {for (auto &v : V) printvec(v);}

void f(ll A, ll B, ll &a, ll &b)
{
  ll x = min(a, B - b);
  a -= x;
  b += x;
}

void nxt(ll t, ll A, ll B, ll C, ll D, ll &a, ll &b, ll &c, ll &d)
{
  if (t == 0)
    f(A, B, a, b);
  else if (t == 1)
    f(B, C, b, c);
  else if (t == 2)
    f(C, D, c, d);
  else if (t == 3)
    f(D, A, d, a);
}

/*
a_{i + 1} = next_index(a_i) の漸化式が成り立っているとき
conversion(a_1) add … add conversion(a_K) を求める
t mul a = a add … add a (t 回)
計算量は各クエリ O(周期)
add には結合法則と単位元 e を要求
*/
template <typename Index, typename Data, Index (*next_index)(Index), Data (*conversion)(Index), Data (*add)(Data, Data), Data (*e)(), Data (*mul)(ll, Data)>
struct Period
{
  vector<Index> his;
  vector<Data> dat;
  int lambda, mu;
  Data head, tail;

  Period(Index start)
  {
    Index a = start, b = start;
    do
    {
      his.emplace_back(a);
      a = next_index(a);
      b = next_index(next_index(b));
    } while (a != b);
    mu = his.size();
    Index c = start;
    while (a != c)
    {
      his.emplace_back(a);
      a = next_index(a);
      c = next_index(c);
    }
    lambda = (int)his.size() - mu;

    dat.resize(his.size());
    for (int i = 0; i < (int)his.size(); i++)
      dat[i] = conversion(his[i]);
    head = range_add(0, lambda);
    tail = range_add(lambda, lambda + mu);
  }

  Data range_add(int l, int r) // 半開区間
  {
    Data res = e();
    for (int i = l; i < r; i++)
      res = add(res, dat[i]);
    return res;
  }

  Data query(ll K)
  {
    if (K <= lambda + mu)
      return range_add(0, K);

    Data mid = range_add(lambda, lambda + (K - lambda) % mu);
    return add(add(head, mul((K - lambda) / mu, tail)), mid);
  }
};

ll A, B, C, D;
using tl5 = tuple<ll, ll, ll, ll, ll>;
tl5 next_index(tl5 tabcd)
{
  auto [t, a, b, c, d] = tabcd;
  nxt(t, A, B, C, D, a, b, c, d);
  t++, t %= 4;
  return {t, a, b, c, d};
}
tl5 conversion(tl5 a) { return a; }
tl5 e() { return {-1, -1, -1, -1, -1}; }
tl5 add(tl5 a, tl5 b) { return b == e() ? a : b; }
tl5 mul(ll t, tl5 a) { return a; }

int main()
{
  ll N;
  cin >> A >> B >> C >> D >> N;
  /*
  ll a = A, b = 0, c = 0, d = 0;
  for (ll i = 0; i < N; i++)
  {
    nxt(i % 4, A, B, C, D, a, b, c, d);
    cout << a << " " << b << " " << c << " " << d << endl;
  }
  //*/
  Period<tl5, tl5, next_index, conversion, add, e, mul> pe({0, A, 0, 0, 0});
  auto [t, a2, b2, c2, d2] = pe.query(N + 1);
  cout << a2 << " " << b2 << " " << c2 << " " << d2 << endl;
}
0