結果

問題 No.824 Many Shifts Hard
ユーザー omochana2omochana2
提出日時 2019-04-26 23:39:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 501 ms / 2,000 ms
コード長 3,835 bytes
コンパイル時間 1,543 ms
コンパイル使用メモリ 165,528 KB
実行使用メモリ 239,104 KB
最終ジャッジ日時 2024-05-04 01:51:29
合計ジャッジ時間 7,477 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
236,544 KB
testcase_01 AC 113 ms
236,544 KB
testcase_02 AC 144 ms
236,288 KB
testcase_03 AC 194 ms
236,980 KB
testcase_04 AC 182 ms
237,568 KB
testcase_05 AC 379 ms
238,208 KB
testcase_06 AC 115 ms
237,696 KB
testcase_07 AC 170 ms
238,336 KB
testcase_08 AC 315 ms
237,440 KB
testcase_09 AC 308 ms
238,544 KB
testcase_10 AC 126 ms
237,160 KB
testcase_11 AC 158 ms
238,208 KB
testcase_12 AC 211 ms
237,568 KB
testcase_13 AC 111 ms
236,416 KB
testcase_14 AC 420 ms
236,544 KB
testcase_15 AC 112 ms
236,544 KB
testcase_16 AC 123 ms
236,416 KB
testcase_17 AC 356 ms
236,544 KB
testcase_18 AC 112 ms
236,416 KB
testcase_19 AC 111 ms
236,672 KB
testcase_20 AC 501 ms
238,972 KB
testcase_21 AC 115 ms
239,104 KB
testcase_22 AC 498 ms
238,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cerr << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cerr<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 300010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
mt19937 rng; //use it by rng() % mod, shuffle(all(vec), rng)
///////////////////////////////////////////////////////////////////////////////////////////////////

ll fac[MAX_N], inv[MAX_N], fiv[MAX_N]; //fiv:inv(fac(i))

ll mod_pow(ll a, ll n) {
	if(n == 0) return 1;
	ll res = mod_pow(a, n / 2);
	if(n % 2 == 0) return res * res % mod;
	else return a * res % mod * res % mod;
}

ll invf(ll a) {
	return mod_pow(a, mod - 2);
}

void C_init(int n) {
	fac[0] = fac[1] = 1; inv[1] = 1;
	fiv[0] = fiv[1] = 1;
	rep(i, 2, n + 1) {
		fac[i] = fac[i - 1] * i % mod;
		inv[i] = mod - inv[mod % i] * (mod / i) % mod;
		fiv[i] = fiv[i - 1] * inv[i] % mod;
	}
}

ll C(int a, int b) { //assume a >= b
	if(a < b || a < 0 || b < 0) return 0;
	return fac[a] * fiv[b] % mod * fiv[a - b] % mod;
}

int N, K;

ll ans(int at, vector<int> vec) {
	if(at == K) {
		int res = 0;
		rep(i, 1, N + 1) {
			if(vec[i]) res += i;
		}
		return res;
	}
	ll sum = 0;
	rep(i, 1, N + 1) {
		vector<int> tmp = vec;
		tmp[i - 1] += tmp[i];
		tmp[i] = 0;
		sum += ans(at + 1, tmp);
	}
	return sum;
}

ll dp[310][310][310];

ll loop(int a, int b, int k) {
	if(k == 0) {
		if(a == 0 && b == 1) return 1;
		else return 0;
	}
	if(dp[a][b][k] != -1) return dp[a][b][k];
	ll res = 0;
	if(a != 0) {
		if(a != b - 1) ADD(res, loop(a, b - 1, k - 1) * inv[N] % mod);
		ADD(res, loop(a - 1, b, k - 1) * inv[N] % mod);
		ADD(res, loop(a, b, k - 1) * inv[N] % mod * (N - 2) % mod);
	}
	else {
		if(b > 1) ADD(res, loop(a, b - 1, k - 1) * inv[N] % mod);
		ADD(res, loop(a, b, k - 1) * inv[N] % mod * (N - 1) % mod);
	}
	return dp[a][b][k] = res;
}

void solve() {
	cin >> N >> K;
	memset(dp, -1, sizeof(dp));
	C_init(N);
	ll res = 0;
	ll base = mod_pow((N - 1) * inv[N] % mod, K); 
	rep(i, 1, N + 1) {
		ll p = base;
		rep(j, 0, K + 1) {
			if(i + j + 1 > N) continue;
			ADD(p, loop(j + 1, j + 2, K));
		}
		ADD(res, p * i % mod);
	}
	cout << res * mod_pow(N, K) % mod << "\n";
}

uint32_t rd() {
	uint32_t res;
#ifdef __MINGW32__
	asm volatile("rdrand %0" :"=a"(res) ::"cc");
#else
	res = std::random_device()();
#endif
	return res;
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
    cerr << fixed;
	cerr.precision(6);
	rng.seed(rd());
#ifdef LOCAL
	//freopen("in.txt", "wt", stdout); //for tester
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}
0