結果

問題 No.831 都市めぐり
ユーザー 👑 emthrmemthrm
提出日時 2019-05-24 22:54:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 3,354 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 123,624 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 13:16:01
合計ジャッジ時間 2,116 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007; // 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
/*-------------------------------------------------*/
int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  // int n; cin >> n;
  // vector<int> city;
  // FOR(i, 1, n + 1) city.emplace_back(i);
  // vector<int> root(n);
  // long long ans = LINF;
  // do {
  //   long long money = 0;
  //   REP(i, n - 1) {
  //     money += (city[i] * city[i + 1]) + (city[i + 1] - city[i]);
  //   }
  //   money += (city[n - 1] * city[0]) + (city[0] - city[n - 1]);
  //   if (money < ans) {
  //     ans = money;
  //     root = city;
  //   }
  // } while (next_permutation(ALL(city)));
  // cout << ans << '\n';
  // REP(i, n) cout << root[i] << ' ';
  long long n; cin >> n;
  if (n == 1) {
    cout << 0 << '\n';
    return 0;
  }
  if (n == 2) {
    cout << 4 << '\n';
    return 0;
  }
  long long ans = 0, mae = 1, ushiro = n - 1;
  if (n & 1) {
    REP(i, (n + 1) / 2) {
      ans += (mae * ushiro) + (ushiro - mae);
      mae += 2;
      mae = min(mae, n);
      if (i < (n - 1) / 2) {
        ans += (mae * ushiro) + (mae - ushiro);
        ushiro -= 2;
        ushiro = max(ushiro, 1LL);
      }
    }
  } else if (n % 4 == 0) {
    int cnt = 0;
    while (mae < ushiro) {
      ans += (mae * ushiro) + (ushiro - mae);
      mae += 2;
      ++cnt;
      if (mae == ushiro) {
        ans += (ushiro * (mae - 1)) + (mae - 1 - ushiro);
        ++cnt;
        ++mae;
        --ushiro;
        break;
      }
      ans += (mae * ushiro) + (mae - ushiro);
      ushiro -= 2;
      ++cnt;
    }
    while (cnt < n) {
      ans += (mae * ushiro) + (mae - ushiro);
      ushiro -= 2;
      ushiro = max(ushiro, 1LL);
      ++cnt;
      if (cnt == n) break;
      ans += (mae * ushiro) + (ushiro - mae);
      mae += 2;
      mae = min(mae, n);
      ++cnt;
    }
  } else {
    int cnt = 0;
    while (mae < ushiro) {
      ans += (mae * ushiro) + (ushiro - mae);
      mae += 2;
      ans += (mae * ushiro) + (mae - ushiro);
      ushiro -= 2;
      cnt += 2;
      if (mae == ushiro) {
        ans += (mae * (ushiro + 1) + (ushiro + 1 - mae));
        ++cnt;
        ++mae;
        --ushiro;
        break;
      }
    }
    while (cnt < n) {
      ans += (mae * ushiro) + (ushiro - mae);
      mae += 2;
      mae = min(mae, n);
      ++cnt;
      if (cnt == n) break;
      ans += (mae * ushiro) + (mae - ushiro);
      ushiro -= 2;
      ushiro = max(ushiro, 1LL);
      ++cnt;
    }
  }
  cout << ans << '\n';
  return 0;
}
0