結果

問題 No.824 Many Shifts Hard
ユーザー omochana2omochana2
提出日時 2019-04-26 23:33:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,825 bytes
コンパイル時間 2,002 ms
コンパイル使用メモリ 174,728 KB
実行使用メモリ 239,952 KB
最終ジャッジ日時 2024-05-04 01:37:46
合計ジャッジ時間 7,543 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
237,544 KB
testcase_01 AC 70 ms
237,388 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 97 ms
238,740 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 91 ms
238,284 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 72 ms
237,084 KB
testcase_19 AC 70 ms
237,484 KB
testcase_20 WA -
testcase_21 AC 71 ms
239,320 KB
testcase_22 WA -
権限があれば一括ダウンロードができます

ソースコード

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 {
		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