結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー karinohitokarinohito
提出日時 2021-07-31 03:07:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 320 ms / 3,000 ms
コード長 1,914 bytes
コンパイル時間 1,982 ms
コンパイル使用メモリ 195,464 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 11:09:39
合計ジャッジ時間 8,965 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 122 ms
4,352 KB
testcase_06 AC 105 ms
4,348 KB
testcase_07 AC 123 ms
4,348 KB
testcase_08 AC 194 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 317 ms
4,348 KB
testcase_12 AC 306 ms
4,348 KB
testcase_13 AC 317 ms
4,352 KB
testcase_14 AC 318 ms
4,352 KB
testcase_15 AC 317 ms
4,348 KB
testcase_16 AC 315 ms
4,348 KB
testcase_17 AC 71 ms
4,352 KB
testcase_18 AC 89 ms
4,352 KB
testcase_19 AC 151 ms
4,352 KB
testcase_20 AC 58 ms
4,348 KB
testcase_21 AC 10 ms
4,348 KB
testcase_22 AC 197 ms
4,348 KB
testcase_23 AC 245 ms
4,352 KB
testcase_24 AC 292 ms
4,352 KB
testcase_25 AC 304 ms
4,348 KB
testcase_26 AC 314 ms
4,348 KB
testcase_27 AC 319 ms
4,352 KB
testcase_28 AC 318 ms
4,348 KB
testcase_29 AC 316 ms
4,348 KB
testcase_30 AC 314 ms
4,352 KB
testcase_31 AC 320 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using Graph = vector<vector<ll>>;

vector<vector<int>> BOX;
//max C size 通りの全列挙
int k = 0;
vector<int> now_vector;
void next_combination(int size, int max, int k) {

	if (k == size) {
		BOX.push_back(now_vector);
	}
	else {
		
		if (k == 0) {
			for (int i = 0; i < max; i++) {
				now_vector[k] = i;
				k++;
				next_combination(size, max, k);
				k--;
			}
		}
		else {
			int LAST = now_vector[k - 1];
			for (int i = LAST + 1; i < max; i++) {
				now_vector[k] = i;
				k++;
				next_combination(size, max, k);
				k--;
			}
		}
	}
	return;
}



int main() {
	int N, K;
	cin >> N >> K;

	vector<int> C(9);
	vector<int> D;
	for(int i=0;i<9;i++) {
		cin >> C[i];
		for(int j=C[i]-1;j>=0;j--)D.push_back(i + 1);
	}
	int n = N / 2;
	now_vector.assign(n, -1);
	next_combination(n, N, 0);
	ll an = 0;
	unordered_set<string> Poo;
	vector<vector<int>> KK(14,vector<int> (10));
	ll as=1;
	for(int i=0;i<N;i++) {
		for(int j=0;j<9;j++){
			KK[i][j]=(as*(j+1))%K;
		}
		as*=10;
		as%=K;
	}
	for(int i=BOX.size()-1;i>=0;i--) {

		string AU;
		unordered_set<int> S;
		for(int k=0;k<n;k++) {
			S.insert(BOX[i][k]);
			AU.push_back(D[BOX[i][k]]);
		}
		if (Poo.count(AU))continue;
		Poo.insert(AU);
		string BU;
		for(int k=0;k<N;k++){
			if (!S.count(k))BU.push_back(D[k]);
		}
		ll x = 0;
		unordered_map<int, int> M1;
		do {
			x = 0;
			for(int u=0;u<n;u++){
				x += KK[u][AU[u]-1];
			}
			x %= K;
			M1[x]++;
		} while (next_permutation(all(AU)));

		do {
			x = 0;
			for(int u=0;u<N-n;u++) {
				x+=KK[u+n][BU[u]-1];
			}
			x %= K;
			if (x == 0)x += K;
			if(M1.count(K-x))an += M1[K - x];

		} while (next_permutation(all(BU)));


	}
	cout << an << endl;
}
0