結果
問題 | No.831 都市めぐり |
ユーザー |
![]() |
提出日時 | 2019-05-24 22:54:48 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 24 ms / 2,000 ms |
コード長 | 3,354 bytes |
コンパイル時間 | 1,085 ms |
コンパイル使用メモリ | 119,828 KB |
最終ジャッジ日時 | 2025-01-07 04:01:17 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 18 |
ソースコード
#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;}