結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー 沙耶花沙耶花
提出日時 2021-07-30 21:03:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,346 ms / 3,000 ms
コード長 1,028 bytes
コンパイル時間 6,736 ms
コンパイル使用メモリ 256,152 KB
最終ジャッジ日時 2025-01-23 10:47:23
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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