結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー 沙耶花沙耶花
提出日時 2021-07-30 21:03:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,601 ms / 3,000 ms
コード長 1,028 bytes
コンパイル時間 5,119 ms
コンパイル使用メモリ 266,304 KB
実行使用メモリ 36,608 KB
最終ジャッジ日時 2023-10-14 01:55:08
合計ジャッジ時間 16,747 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 5 ms
4,420 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 3 ms
4,352 KB
testcase_14 AC 834 ms
17,576 KB
testcase_15 AC 1,574 ms
36,308 KB
testcase_16 AC 1,558 ms
36,608 KB
testcase_17 AC 1,597 ms
36,328 KB
testcase_18 AC 1,548 ms
36,312 KB
testcase_19 AC 1,601 ms
36,384 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 7 ms
4,720 KB
testcase_24 AC 3 ms
4,356 KB
testcase_25 AC 3 ms
4,352 KB
testcase_26 AC 114 ms
4,352 KB
testcase_27 AC 402 ms
14,832 KB
testcase_28 AC 269 ms
9,788 KB
testcase_29 AC 29 ms
5,416 KB
testcase_30 AC 211 ms
10,716 KB
testcase_31 AC 218 ms
12,984 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