結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー 沙耶花沙耶花
提出日時 2021-07-30 21:03:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,598 ms / 3,000 ms
コード長 1,028 bytes
コンパイル時間 4,516 ms
コンパイル使用メモリ 265,688 KB
実行使用メモリ 132,328 KB
最終ジャッジ日時 2023-10-14 01:56:16
合計ジャッジ時間 38,967 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 3 ms
4,352 KB
testcase_03 AC 7 ms
4,988 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 114 ms
4,352 KB
testcase_06 AC 122 ms
4,388 KB
testcase_07 AC 114 ms
4,348 KB
testcase_08 AC 763 ms
82,076 KB
testcase_09 AC 3 ms
4,352 KB
testcase_10 AC 2 ms
4,356 KB
testcase_11 AC 1,970 ms
132,328 KB
testcase_12 AC 2,485 ms
99,328 KB
testcase_13 AC 1,930 ms
132,184 KB
testcase_14 AC 2,584 ms
131,724 KB
testcase_15 AC 2,598 ms
130,364 KB
testcase_16 AC 1,783 ms
132,148 KB
testcase_17 AC 374 ms
31,080 KB
testcase_18 AC 389 ms
39,032 KB
testcase_19 AC 949 ms
55,732 KB
testcase_20 AC 495 ms
37,484 KB
testcase_21 AC 38 ms
8,692 KB
testcase_22 AC 1,271 ms
30,912 KB
testcase_23 AC 1,576 ms
50,776 KB
testcase_24 AC 1,973 ms
91,868 KB
testcase_25 AC 2,281 ms
104,308 KB
testcase_26 AC 2,113 ms
120,988 KB
testcase_27 AC 1,879 ms
126,292 KB
testcase_28 AC 1,511 ms
132,240 KB
testcase_29 AC 1,153 ms
132,296 KB
testcase_30 AC 1,086 ms
132,228 KB
testcase_31 AC 1,376 ms
132,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000005

long long ans = 0LL;
int K;
int n;
vector<int> c;
map<int,int> cnt[16384];
void dfs0(long long cur,int b){
	if(__builtin_popcount(b)==n/2){
		cnt[b][cur%K] ++;
		return;
	}
	rep(i,n){
		if((b>>i)&1)continue;
		if(i!=0 && ((b>>(i-1))&1)==0 && c[i]==c[i-1])continue;
		int nb = b | (1<<i);
		dfs0(cur*10 + c[i],nb);
	}
	
}

void dfs1(long long cur,int b){
	if(__builtin_popcount(b)==(n+1)/2){
		rep(i,n/2)cur *= 10;
		cur %= K;
		cur = (K-cur)%K;
		b = ((1<<n)-1) ^ b;
		ans += cnt[b][cur];
		return;
	}
	for(int i=n-1;i>=0;i--){
		if((b>>i)&1)continue;
		if(i!=n-1 && ((b>>(i+1))&1)==0 && c[i]==c[i+1])continue;
		int nb = b | (1<<i);
		dfs1(cur*10 + c[i],nb);
	}
	
}

int main(){
	
	cin>>n>>K;
	
	rep(i,9){
		int t;
		cin>>t;
		rep(j,t)c.push_back(i+1);
	}
	
	dfs0(0,0);
	dfs1(0,0);
	cout<<ans<<endl;
	return 0;
}
0