結果

問題 No.535 自然数の収納方法
ユーザー pekempeypekempey
提出日時 2017-06-24 00:49:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,870 bytes
コンパイル時間 825 ms
コンパイル使用メモリ 75,972 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 06:50:26
合計ジャッジ時間 1,829 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 7 ms
6,944 KB
testcase_08 AC 16 ms
6,940 KB
testcase_09 AC 19 ms
6,940 KB
testcase_10 AC 22 ms
6,944 KB
testcase_11 AC 6 ms
6,944 KB
testcase_12 AC 11 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 9 ms
6,944 KB
testcase_15 AC 23 ms
6,944 KB
testcase_16 AC 16 ms
6,944 KB
testcase_17 AC 24 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 25 ms
6,944 KB
testcase_20 AC 24 ms
6,940 KB
testcase_21 AC 24 ms
6,944 KB
testcase_22 AC 24 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <fstream>

constexpr int MOD = 1e9 + 7;

struct modint {
	int n;
	modint(int n = 0) : n(n) {}
};

modint operator+(modint a, modint b) { return modint((a.n += b.n) >= MOD ? a.n - MOD : a.n); }
modint operator-(modint a, modint b) { return modint((a.n -= b.n) < 0 ? a.n + MOD : a.n); }
modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % MOD); }
modint &operator+=(modint &a, modint b) { return a = a + b; }
modint &operator-=(modint &a, modint b) { return a = a - b; }
modint &operator*=(modint &a, modint b) { return a = a * b; }

int main() {
	int n;
	std::cin >> n;

	static modint dp0[2002];
	static modint dp1[2002];

	memset(dp0, 0, sizeof(dp0));

	modint ans;

	// A[N]<=A[1]         -> A'[1]<=A'[2]
	// A[1]<=A[2]         -> A'[2]<=A'[3]
	// A[2]<=A[3]+1       -> A'[3]<=A'[4]+1
	// A[3]<=A[4]+2       -> A'[4]<=A'[5]+2
	// A[4]<=A[5]+3       -> A'[5]<=A'[6]+3
	// ...
	// A[N-1]<=A[N]+N-2   -> A'[N]<=A'[1]+N-2

	// A'[N]=1
	{
		memset(dp0, 0, sizeof(dp0));
		dp0[1] = 1;

		for (int i = 2; i <= n; i++) {
			memset(dp1, 0, sizeof(dp1));
			for (int j = 1; j <= n; j++) {
				dp0[j + 1] += dp0[j];
			}
			for (int j = 1; j <= n; j++) {
				dp1[j] = dp0[std::min(n, j + std::max(0, i - 3))];
			}
			std::swap(dp0, dp1);
		}

		for (int i = 1; i <= n - 1; i++) {
			ans += dp0[i];
		}
	}

	// A'[N]>=2
	{
		memset(dp0, 0, sizeof(dp0));
		for (int i = 2; i <= n; i++) {
			dp0[i] = 1;
		}

		for (int i = 2; i <= n; i++) {
			memset(dp1, 0, sizeof(dp1));
			for (int j = 1; j <= n; j++) {
				dp0[j + 1] += dp0[j];
			}
			for (int j = 1; j <= n; j++) {
				dp1[j] = dp0[std::min(n, j + std::max(0, i - 3))];
			}
			std::swap(dp0, dp1);
		}

		for (int i = 1; i <= n; i++) {
			ans += dp0[i];
		}
	}

	std::cout << ans.n << std::endl;
}
0