結果

問題 No.1231 Make a Multiple of Ten
ユーザー marurunn11marurunn11
提出日時 2020-09-18 21:40:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 13,836 bytes
コンパイル時間 4,011 ms
コンパイル使用メモリ 220,084 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 18:11:53
合計ジャッジ時間 5,811 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 36 ms
4,376 KB
testcase_05 AC 31 ms
4,376 KB
testcase_06 AC 13 ms
4,380 KB
testcase_07 AC 37 ms
4,380 KB
testcase_08 AC 12 ms
4,376 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 34 ms
4,376 KB
testcase_11 AC 40 ms
4,376 KB
testcase_12 AC 73 ms
4,376 KB
testcase_13 AC 76 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 76 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"

#ifdef _MSC_VER
#include <intrin.h>  //gcc上ではこれがあると動かない。__popcnt, umul128 等用のincludeファイル。
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
//#pragma intrinsic(_umul128)
#endif

//#include <atcoder/all>
//using namespace atcoder;

//#include <boost/multiprecision/cpp_int.hpp>
//#include <boost/multiprecision/cpp_dec_float.hpp>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int long long
#define LL128 boost::multiprecision::int128_t
#define LL boost::multiprecision::cpp_int
#define LD100 boost::multiprecision::cpp_dec_float_100

#define rep(i, n) for(long long i = 0; i < (n); i++)
#define sqrt(d) pow((long double) (d), 0.50)
#define PII pair<int, int>
#define MP make_pair
#define PB push_back
#define ALL(v) v.begin(), v.end()

const int INF = std::numeric_limits<int>::max() / 2 - 100000000;
const long long INF2 = std::numeric_limits<long long>::max() / 2 - 100000000;
const long double pi = acos(-1);

constexpr int MOD = 1000000007; //1e9 + 7
//constexpr int MOD = 1000000009; //1e9 + 9
//constexpr int MOD = 998244353;




long long my_gcd(long long a, long long b) {
	if (b == 0) return a;
	return my_gcd(b, a % b);
}




// ax + by = gcd(a, b) を解く。返り値は、gcd(a, b)。
long long my_gcd_ext(long long a, long long b, long long& x, long long& y) {
	if (b == 0) {
		x = 1; y = 0;
		return a;
	}

	long long tempo = my_gcd_ext(b, a % b, y, x);

	//bx' + ry' = gcd(a, b) → (qb + r)x + by = gcd(a, b) に戻さないといけない。// (r = a % b)
	//b(x' - qy') + (bq + r)y' = gcd(a, b) と同値変形できるから、
	// x = y', y = x' - qy'
	y -= (a / b) * x;

	return tempo;
}




//M を法として、a の逆元を返す。但し gcd(a, M) = 1。
long long my_invmod(long long a, long long M) {
	long long x = 0, y = 0;
	long long memo = my_gcd_ext(a, M, x, y);
	assert(memo == 1LL);
	x %= M;
	if (x < 0) x += M;
	return x;
}




//繰り返し2乗法
//N^aの、Mで割った余りを求める。
ll my_pow(ll N, ll a, ll M) {
	ll tempo;
	if (a == 0) {
		return 1;
	}
	else {
		if (a % 2 == 0) {
			tempo = my_pow(N, a / 2, M);
			return (tempo * tempo) % M;
		}
		else {
			tempo = my_pow(N, a - 1, M);
			return (tempo * N) % M;
		}
	}
}

ll my_pow(ll N, ll a) {
	ll tempo;
	if (a == 0) {
		return 1;
	}
	else {
		if (a % 2 == 0) {
			tempo = my_pow(N, a / 2);
			return (tempo * tempo);
		}
		else {
			tempo = my_pow(N, a - 1);
			return (tempo * N);
		}
	}
}




//N_C_a を M で割った余り
ll my_combination(ll N, ll a, ll M) {
	if (N < a) return 0;

	ll answer = 1;

	rep(i, a) {
		answer *= (N - i);
		answer %= M;
	}

	rep(i, a) {
		answer *= my_pow(i + 1, M - 2, M);
		answer %= M;
	}

	return answer;
}


//N_C_i を M で割った余りを、v.at(i) に代入する。
void my_combination_table(ll N, ll M, vector<ll>& v) {
	v.assign(N + 1, 1);

	for (ll i = 1; i <= N; i++) {
		v.at(i) = v.at(i - 1) * (N - (i - 1LL));
		v.at(i) %= M;

		v.at(i) *= my_pow(i, M - 2, M);
		v.at(i) %= M;
	}
}


//(N + i)_C_N を M で割った余りを、v.at(i) に代入する。(v のサイズに依存)
void my_combination_table2(ll N, ll M, vector<ll>& v) {
	v.at(0) = 1;

	for (ll i = 1; i < (ll)v.size(); i++) {
		v.at(i) = v.at(i - 1) * (N + i);
		v.at(i) %= M;

		v.at(i) *= my_pow(i, M - 2, M);
		v.at(i) %= M;
	}
}




//階乗。x ! まで計算する。結果は dp に保存する。20 ! = 2.43e18 まで long long に入る。
ll factorial(ll x, vector<ll>& dp) {
	if ((ll)dp.size() <= x) {
		int n = dp.size();
		rep(i, x + 1 - n) {
			dp.push_back(0);
		}
	}

	if (x == 0) return dp.at(x) = 1;
	if (dp.at(x) != -1 && dp.at(x) != 0) return dp.at(x);
	return dp.at(x) = x * factorial(x - 1, dp);
}




//階乗の M で割った余り。x ! まで計算する。結果は dp に保存する。
ll factorial2(ll x, ll M, vector<ll>& dp) {
	if ((ll)dp.size() <= x) {
		int n = dp.size();
		rep(i, x + 1 - n) {
			dp.push_back(0);
		}
	}

	if (x == 0) return dp.at(x) = 1;
	if (dp.at(x) != -1 && dp.at(x) != 0) return dp.at(x);
	ll tempo = (x * factorial2(x - 1, M, dp));
	tempo %= M;
	return dp.at(x) = tempo;
}




//階乗の mod M での逆元 (M: prime)。x ! まで計算する。結果は dp に保存する。
ll factorial_inverse(ll x, ll M, vector<ll>& dp) {
	if ((ll)dp.size() <= x) {
		int n = dp.size();
		rep(i, x + 1 - n) {
			dp.push_back(0);
		}
	}

	if (x == 0) return dp.at(x) = 1;
	if (dp.at(x) != -1 && dp.at(x) != 0) return dp.at(x);
	return dp.at(x) = (my_pow(x, M - 2, M) * factorial_inverse(x - 1, M, dp)) % M;
}




//N_C_a を M で割った余り。何度も呼ぶ用。
ll my_combination2(ll N, ll a, ll M, vector<ll>& dp_factorial, vector<ll>& dp_factorial_inverse) {
	if ((ll)dp_factorial.size() <= N) {
		factorial2(N, M, dp_factorial);
	}
	if ((ll)dp_factorial_inverse.size() <= N) {
		factorial_inverse(N, M, dp_factorial_inverse);
	}

	if (N < a) return 0;

	ll answer = 1;
	answer *= dp_factorial.at(N);
	answer %= M;
	answer *= dp_factorial_inverse.at(N - a);
	answer %= M;
	answer *= dp_factorial_inverse.at(a);
	answer %= M;

	return answer;
}




// base を底としたときの、n の i桁目を、v.at(i) に入れる。(桁数は n に応じて自動で設定する。)
void ll_to_vector(signed base, long long n, vector<signed>& v) {
	long long tempo = n;
	long long tempo2 = n;

	signed n_digit = 1;
	while (tempo2 >= base) {
		tempo2 /= base;
		n_digit++;
	}

	v.assign(n_digit, 0);   // v のサイズを適切に調整する場合。

	// n_digit = v.size();  // v のサイズをそのままにする場合。

	for (signed i = 0; i < n_digit; i++) {
		long long denominator = my_pow(base, n_digit - 1 - i);

		v.at(i) = tempo / denominator;
		tempo -= v.at(i) * denominator;
	}
}




int char_to_int(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4;
	case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9;
	default: return 0;
	}
}




//エラトステネスの篩で、prime で ないところに false を入れる。O(n loglog n)
void prime_judge(vector<bool>& prime_or_not) {
	prime_or_not.assign(prime_or_not.size(), true);

	prime_or_not.at(0) = false;
	prime_or_not.at(1) = false;

	long long n = prime_or_not.size() - 1;

	for (long long i = 2; 2 * i <= n; i++) {
		prime_or_not.at(2 * i) = false;
	}


	for (long long i = 3; i * i <= n; i += 2) {
		//ここからは奇数のみ探索。i の倍数に false を入れる。
		if (prime_or_not.at(i)) {
			long long j = i * i;  // i^2 未満の i の倍数には、すでに false が入っているはず。
			while (j < n + 1) {
				prime_or_not.at(j) = false;
				j += 2 * i;
			}
		}
	}
};




// n + 1 の サイズの vector を返す。res.at(i) には、i の 1 以外で最小の約数を入れる。res.at(i) == i なら i は素数。
// 2e8 なら、3.2 秒程度で終わる。たぶん、prime_judge より 3倍弱遅い。
vector<long long> sieve(long long n) {
	n++; // n まで判定する。配列サイズは +1。

	vector<long long> res(n, 0);
	for (long long i = 1; i < n; i++) {
		if (i % 2 == 0) res.at(i) = 2;  // 偶数をあらかじめ処理。
		else res.at(i) = i;
	}

	for (long long i = 3; i * i < n; i += 2) {
		//ここからは奇数のみ探索。i の倍数に i を入れる。
		if (res.at(i) == i) {
			long long j = i * i;  // i^2 未満の i の倍数には、すでに最小の約数が入っているはず。
			while (j < n) {
				if (res.at(j) == j) res.at(j) = i;
				j += 2 * i;
			}
		}
	}

	return res;
};




//O (sqrt(n)) で素数判定する用。
bool is_prime(long long N) {
	if (N == 1 || N == 0) return false;
	if (N == 2 || N == 3) return true;

	if (N % 2 == 0) return false;
	if (N % 3 == 0) return false;

	for (long long i = 1; (6 * i + 1) * (6 * i + 1) <= N; ++i) {
		if (N % (6 * i + 1) == 0) return false;
	}
	for (long long i = 0; (6 * i + 5) * (6 * i + 5) <= N; ++i) {
		if (N % (6 * i + 5) == 0) return false;
	}
	return true;
}




//素因数分解を O(sqrt(N)) で行うための関数。
map<ll, ll> divide_to_prime(int target) {
	map<ll, ll> res;

	//sqrt(target) まで調べる。
	ll upper_lim = ceil(sqrt(target));
	vector<bool> prime_or_not(upper_lim + 3, true);
	if (upper_lim < 20) prime_or_not.assign(25, true);

	prime_or_not.at(0) = false; prime_or_not.at(1) = false;
	prime_judge(prime_or_not);

	ll tempo = target;
	for (int i = 1; i * i <= target; i++) {
		if (prime_or_not.at(i)) {
			while (tempo % i == 0) {
				tempo /= i;
				res[i]++;
			}
		}

		if (tempo == 1) break;  //別に必要はない。
	}

	if (tempo != 1) res[tempo]++; //sqrt(target) より大きな素因数は高々1つしかない。
	return res;
}




//関数 sieve で得た、vector min_factor を持ってるときに、素因数分解を高速で行うための関数。
map<long long, long long> divide_to_prime2(long long target, vector<long long>& min_factor) {
	map<long long, long long> res;
	if (min_factor.empty() || (long long)min_factor.size() - 1 < target) min_factor = sieve(target);

	while (target > 1) {
		res[min_factor[target]]++;
		target /= min_factor[target];
	}

	return res;
}




//約数全列挙を O(sqrt(N)) で行うための関数。
vector<long long> count_dividers(long long target) {

	vector <long long> dividers, tempo;
	long long i = 1;
	while (i * i < target + 1) {
		if (target % i == 0) {
			dividers.push_back(i);
			if (i < target / i) tempo.push_back(target / i);  // if節がないと、平方数の時、sqrt(target) がダブルカウントされる。
		}
		i++;
	}

	for (long long j = 0; j < (long long)tempo.size(); j++) {
		dividers.push_back(tempo.at(tempo.size() - 1 - j));
	}

	return dividers;
}




//関数 sieve で得た、vector min_factor を持ってるときに、約数全列挙を高速で行うための関数。
vector<long long> count_dividers2(long long target, vector<long long>& min_factor) {

	vector <long long> dividers = { 1 };
	map<long long, long long> memo = divide_to_prime2(target, min_factor);

	for (auto&& iter = memo.begin(); iter != memo.end(); iter++) {
		vector <long long> tempo = dividers;
		for (long long k = 0; k < (long long)tempo.size(); k++) {
			long long times = 1;
			for (long long j = 1; j <= (iter->second); j++) {
				times *= iter->first;
				dividers.push_back(tempo[k] * times);
			}
		}
	}

	sort(dividers.begin(), dividers.end());  //sortしないと小さい順に並ばないが、必要ないなら消しても良い。
	return dividers;
}




void BFS_labyrinth(queue<pair<int, int>>& que, vector<vector<int>>& dist, int& area) {
	int H = dist.size();
	int W = dist.at(0).size();

	while (!que.empty()) {
		int h, w;
		pair<int, int> tempo = que.front(); que.pop();

		h = tempo.first;
		w = tempo.second;
		//cout << temp_i << " " << temp_j << endl;

		for (int dh = -1; dh <= 1; dh++) {
			for (int dw = -1; dw <= 1; dw++) {
				if (h + dh < 0 || H <= h + dh) continue;  //範囲外
				if (w + dw < 0 || W <= w + dw) continue;  //範囲外
				if (dh == 0 && dw == 0) continue; //動いていない
				if (dh * dw != 0) continue; //右上など。八近傍の場合は消す。
				if (dist.at(h + dh).at(w + dw) != -1) continue; //行けない領域に、既に INF などが代入されている場合はこの条件だけで ok

				dist.at(h + dh).at(w + dw) = dist.at(h).at(w) + 1;
				que.push(make_pair(h + dh, w + dw));
			}
		}

		//何か所も領域がある場合だけ必要
		if (que.empty()) {
			rep(i, H) {
				rep(j, W) {
					if (dist.at(i).at(j) == -1) {
						que.push(make_pair(i, j));
						dist.at(i).at(j) = 0;
						area++;
						break;
					}
				}
				if (!que.empty()) break;
			}
		}
	}
}




void BFS01_labyrinth(deque<pair<int, int>>& que, vector<vector<int>>& dist, vector<vector<int>>& cost) {
	int H = dist.size();
	int W = dist.at(0).size();

	while (!que.empty()) {
		int h, w;
		pair<int, int> tempo = que.front(); que.pop_front();

		h = tempo.first;
		w = tempo.second;
		//cout << temp_i << " " << temp_j << endl;

		for (int dh = -1; dh <= 1; dh++) {
			for (int dw = -1; dw <= 1; dw++) {
				if (h + dh < 0 || H <= h + dh) continue;  //範囲外
				if (w + dw < 0 || W <= w + dw) continue;  //範囲外
				if (dh == 0 && dw == 0) continue; //動いていない
				if (dh * dw != 0) continue; //右上など。八近傍の場合は消す。
				if (dist.at(h + dh).at(w + dw) != -1) continue; //行けない領域に、既に INF などが代入されている場合はこの条件だけで ok

				dist.at(h + dh).at(w + dw) = dist.at(h).at(w) + cost.at(h + dh).at(w + dw);

				if (cost.at(h + dh).at(w + dw) == 0) {//コストが低い場合
					que.push_front(make_pair(h + dh, w + dw));
				}
				else {//コストが高い場合
					que.push_back(make_pair(h + dh, w + dw));
				}

			}
		}
	}
}




int dfs(const vector<int>& cnt, int v, int sum, int tempo) {
	if (v == 10) {
		if (sum == 0) return tempo;
		else return 0;
	}

	int memo = 0;
	for (int i = 0; i <= cnt.at(v); i++) {
		int sum2 = sum + i * v;
		sum2 %= 10;
		memo = max(memo, dfs(cnt, v + 1, sum2, tempo + i));
	}
	return memo;

}




signed main() {
	
	int N; cin >> N;
	vector<int> cnt(10, 0);
	int sum = 0;

	rep(i, N) {
		int A; cin >> A;
		cnt.at(A % 10)++;

		sum += A % 10;
		sum %= 10;
	}

	if (sum == 0) {
		cout << N << endl;
		return 0;
	}
	
	if (cnt.at(sum) != 0) {
		cout << N - 1 << endl;
		return 0;
	}

	int res = 0;
	rep(i, N) {
		if (i == 0) {
			res += cnt.at(i);
			cnt.at(i) = 0;
		}
		else {
			res += (cnt.at(i) / 10) * 10;
			cnt.at(i) %= 10;
		}
	}

	int res2 = dfs(cnt, 1, 0, 0);
	cout << res + res2 << endl;

}
0