結果

問題 No.295 hel__world
ユーザー antaanta
提出日時 2015-10-23 23:59:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 46 ms / 5,000 ms
コード長 6,071 bytes
コンパイル時間 1,169 ms
コンパイル使用メモリ 108,972 KB
実行使用メモリ 25,344 KB
最終ジャッジ日時 2023-10-11 04:51:09
合計ジャッジ時間 3,647 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,220 KB
testcase_01 AC 6 ms
11,296 KB
testcase_02 AC 6 ms
11,356 KB
testcase_03 AC 6 ms
11,316 KB
testcase_04 AC 7 ms
11,292 KB
testcase_05 AC 6 ms
11,348 KB
testcase_06 AC 6 ms
11,228 KB
testcase_07 AC 6 ms
11,344 KB
testcase_08 AC 6 ms
11,212 KB
testcase_09 AC 6 ms
11,300 KB
testcase_10 AC 6 ms
11,300 KB
testcase_11 AC 6 ms
11,252 KB
testcase_12 AC 5 ms
11,216 KB
testcase_13 AC 6 ms
11,252 KB
testcase_14 AC 6 ms
11,348 KB
testcase_15 AC 6 ms
11,356 KB
testcase_16 AC 6 ms
11,212 KB
testcase_17 AC 6 ms
11,548 KB
testcase_18 AC 6 ms
11,344 KB
testcase_19 AC 6 ms
11,356 KB
testcase_20 AC 6 ms
11,356 KB
testcase_21 AC 6 ms
11,220 KB
testcase_22 AC 7 ms
11,260 KB
testcase_23 AC 6 ms
11,296 KB
testcase_24 AC 6 ms
11,292 KB
testcase_25 AC 7 ms
11,344 KB
testcase_26 AC 6 ms
11,256 KB
testcase_27 AC 6 ms
11,252 KB
testcase_28 AC 6 ms
11,252 KB
testcase_29 AC 6 ms
11,344 KB
testcase_30 AC 6 ms
11,320 KB
testcase_31 AC 6 ms
11,340 KB
testcase_32 AC 46 ms
25,260 KB
testcase_33 AC 46 ms
25,344 KB
testcase_34 AC 46 ms
25,096 KB
testcase_35 AC 6 ms
11,468 KB
testcase_36 AC 6 ms
11,464 KB
testcase_37 AC 6 ms
11,352 KB
testcase_38 AC 6 ms
11,496 KB
testcase_39 AC 6 ms
11,300 KB
testcase_40 AC 7 ms
11,292 KB
testcase_41 AC 6 ms
11,352 KB
testcase_42 AC 6 ms
11,208 KB
testcase_43 AC 6 ms
11,232 KB
testcase_44 AC 6 ms
11,232 KB
testcase_45 AC 6 ms
11,228 KB
testcase_46 AC 6 ms
11,312 KB
testcase_47 AC 6 ms
11,352 KB
testcase_48 AC 6 ms
11,500 KB
testcase_49 AC 6 ms
11,216 KB
testcase_50 AC 6 ms
11,220 KB
testcase_51 AC 6 ms
11,228 KB
testcase_52 AC 6 ms
11,236 KB
testcase_53 AC 12 ms
12,548 KB
testcase_54 AC 13 ms
12,400 KB
testcase_55 AC 13 ms
12,392 KB
testcase_56 AC 14 ms
12,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }


struct SaturationInt {
	typedef unsigned long long T; typedef double Float;
	static T Max;
	T x;
	static inline T sat(T x) { return x > Max ? Max : x; }
	SaturationInt(): x(0) { }
	SaturationInt(T x_): x(sat(x_)) { }
	T get() const { return x; }

	SaturationInt operator+(const SaturationInt &that) const {
		return SaturationInt(x + that.x);
	}
	SaturationInt operator*(const SaturationInt &that) const {
		return SaturationInt((Float)x * that.x > Max ? Max : x * that.x);
	}
	SaturationInt &operator+=(const SaturationInt &that) { return *this = *this + that; }
	SaturationInt &operator*=(const SaturationInt &that) { return *this = *this * that; }

	SaturationInt operator^(long long k) const {
		T r = 1, a = x;
		while(k) {
			if(k & 1) r *= a;
			if(k >>= 1) {
				if((Float)a * a * r > Max) return SaturationInt(Max);
				a *= a;
			}
		}
		return SaturationInt(r);
	}

	bool operator<(const SaturationInt &that) const {
		return x < that.x;
	}
};
SaturationInt::T SaturationInt::Max = (1ULL << 63) - 1;

typedef SaturationInt sint;

const int NCR_TABLE_N = 1000;
sint nCrTable[NCR_TABLE_N+1][NCR_TABLE_N+1];

sint nCr(long long x, long long y) {
	if(nCrTable[0][0].x == 0) {
		for(int i = 0; i <= NCR_TABLE_N; i ++) {
			nCrTable[i][0] = 1;
			for(int j = 1; j <= i; j ++)
				nCrTable[i][j] = nCrTable[i-1][j-1] + nCrTable[i-1][j];
		}
	}
	if(x < y) return sint();
	if(y > x / 2) y = x - y;
	if(y == 0) return sint(1);
	if(y == 1) return sint(x);
	if(y == 2) {
		long long a[2] = {x, x - 1};
		a[x % 2] /= 2;
		return sint(a[0]) * sint(a[1]);
	}
	if(y == 3) {
		long long a[3] = {x, x - 1, x - 2};
		a[x % 2] /= 2;
		a[x % 3] /= 3;
		return sint(a[0]) * sint(a[1]) * sint(a[2]);
	}
	if(y == 4) {
		long long a[4] = {x, x - 1, x - 2, x - 3};
		a[(x % 2 == x % 4 ? 2 : 0) + x % 2] /= 2;
		a[x % 3] /= 3;
		a[x % 4] /= 4;
		return sint(a[0]) * sint(a[1]) * sint(a[2]) * sint(a[3]);
	}
	if(y == 5) {
		long long a[5] = {x, x - 1, x - 2, x - 3, x - 4};
		a[(x % 2 == x % 4 ? 2 : 0) + x % 2] /= 2;
		a[x % 3] /= 3;
		a[x % 4] /= 4;
		a[x % 5] /= 5;
		return sint(a[0]) * sint(a[1]) * sint(a[2]) * sint(a[3]) * sint(a[4]);
	}
	if(y == 6) {
		long long a[6] = {x, x - 1, x - 2, x - 3, x - 4, x - 5};
		a[(x % 2 == x % 4 ? 2 : 0) + x % 2] /= 2;
		a[(x % 3 == x % 6 ? 3 : 0) + x % 3] /= 3;
		a[x % 4] /= 4;
		a[x % 5] /= 5;
		a[x % 6] /= 6;
		return sint(a[0]) * sint(a[1]) * sint(a[2]) * sint(a[3]) * sint(a[4]) * sint(a[5]);
	}
	if(y == 7) {
		long long a[7] = {x, x - 1, x - 2, x - 3, x - 4, x - 5, x - 6};
		a[(x % 2 == x % 4 ? 2 : 0) + x % 2] /= 2;
		a[(x % 3 == x % 6 ? 3 : 0) + x % 3] /= 3;
		a[x % 4] /= 4;
		a[x % 5] /= 5;
		a[x % 6] /= 6;
		a[x % 7] /= 7;
		return sint(a[0]) * sint(a[1]) * sint(a[2]) * sint(a[3]) * sint(a[4]) * sint(a[5]) * sint(a[6]);
	}
	if(x > NCR_TABLE_N) {
		return sint(sint::Max);
	} else {
		return nCrTable[(int)x][(int)y];
	}
}

template<typename T>
vector<pair<T,int> > runLength(const T a[], int n) {
	vector<pair<T,int> > r;
	if(n <= 0) return r;
	int cnt = 0;
	for(int i = 0; i <= n; i ++) {
		if(i == n || (i != 0 && a[i] != a[i-1])) {
			r.push_back(make_pair(a[i-1], cnt));
			cnt = 0;
		}
		cnt ++;
	}
	return r;
}

sint solve(long long x, int y, int n) {
	sint ans = 1;
	ans *= nCr(y + x / n, y) ^ (n - x % n);
	ans *= nCr(y + x / n + 1, y) ^ (x % n);
	return ans;
}

int main() {
	const int Alphas = 26;
	char *T = new char[1000001];
	vector<long long> S(Alphas);
	for(int i = 0; i < Alphas; ++ i)
		scanf("%lld", &S[i]);
	scanf("%s", T);
	auto runs = runLength(T, strlen(T));
	vector<vi> lengths(Alphas);
	for(auto p : runs) {
		int a = p.first - 'a';
		S[a] -= p.second;
		lengths[a].push_back(p.second);
	}
	sint ans = 1;
	rep(a, Alphas) {
		long long x = S[a];
		if(x < 0) {
			ans *= 0;
			continue;
		}
		vi &v = lengths[a];
		sort(all(v), greater<int>());
		if(v.empty())
			continue;
		auto w = runLength(&v[0], v.size());
		if(w.size() == 1) {
			ans *= solve(x, w[0].first, w[0].second);
			continue;
		}
		vector<int> sum(w.size() + 1, 0);
		for(int i = (int)w.size() - 1; i >= 0; -- i)
			sum[i] = sum[i+1] + w[i].first * w[i].second;
		map<long long, sint> dp, ndp;
		dp[x] = 1;
		rep(k, w.size()) {
			int y = w[k].first, n = w[k].second;
			double ratio = y * n * 1. / sum[k];
			ndp.clear();
			for(auto p : dp) {
				long long remx = p.first;
				sint t = p.second;
				long long a = (long long)(remx * ratio);
				for(long long b = max(0LL, a - 20); b <= min(remx, a + 20); ++ b) {
					amax(ndp[remx - b], t * solve(b, y, n));
				}
			}
			sint maxi;
			dp.clear();
			for(auto it = ndp.rbegin(); it != ndp.rend(); ++ it) {
				if(maxi < it->second) {
					maxi = it->second;
					dp.insert(*it);
				}
			}
			if(maxi.get() >= 1ULL << 62)
				break;
		}
		sint maxi;
		for(auto p : dp)
			amax(maxi, p.second);
		ans *= maxi;
	}
	if(ans.get() >= 1ULL << 62) {
		puts("hel");
	} else {
		cout << ans.get() << endl;
	}
	return 0;
}
0