結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー kotatsugamekotatsugame
提出日時 2021-07-30 22:32:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,692 bytes
コンパイル時間 736 ms
コンパイル使用メモリ 77,788 KB
実行使用メモリ 105,028 KB
最終ジャッジ日時 2023-10-14 05:52:16
合計ジャッジ時間 57,247 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 1,286 ms
104,852 KB
testcase_05 AC 1,286 ms
104,724 KB
testcase_06 AC 1,407 ms
104,852 KB
testcase_07 AC 1,294 ms
104,900 KB
testcase_08 AC 2,206 ms
104,896 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 2,329 ms
105,028 KB
testcase_12 AC 2,774 ms
104,732 KB
testcase_13 AC 2,314 ms
104,780 KB
testcase_14 AC 2,554 ms
104,852 KB
testcase_15 AC 2,572 ms
104,816 KB
testcase_16 AC 2,302 ms
104,732 KB
testcase_17 AC 2,507 ms
104,864 KB
testcase_18 AC 2,273 ms
104,724 KB
testcase_19 AC 2,600 ms
104,720 KB
testcase_20 AC 670 ms
61,544 KB
testcase_21 AC 683 ms
61,492 KB
testcase_22 AC 2,489 ms
104,892 KB
testcase_23 AC 2,701 ms
104,892 KB
testcase_24 AC 2,736 ms
104,920 KB
testcase_25 AC 2,625 ms
104,868 KB
testcase_26 AC 2,558 ms
104,912 KB
testcase_27 AC 2,318 ms
104,852 KB
testcase_28 AC 2,247 ms
104,840 KB
testcase_29 AC 2,041 ms
104,740 KB
testcase_30 AC 1,765 ms
104,912 KB
testcase_31 AC 2,188 ms
104,896 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:22:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   22 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int N,K,c[14];
long V=1;
vector<int>A[1<<14];
void dfs(int k,int now,int rest)
{
	if(rest==0)
	{
		A[k].push_back(now);
	}
	else
	{
		for(int i=0;i<N;i++)if(!(k>>i&1))
		{
			dfs(k|1<<i,(now*10L+c[i])%K,rest-1);
		}
	}
}
main()
{
	cin>>N>>K;
	int sz=0;
	for(int i=1;i<=9;i++)
	{
		int C;cin>>C;
		for(;C;V*=C--)c[sz++]=i;
	}
	long ans=0;
	dfs(0,0,N/2);
	if(N%2==0)
	{
		long P10=1;
		for(int i=0;i<N/2;i++)P10=P10*10%K;
		int comb=(1<<N/2)-1;
		while(comb<1<<N)
		{
			vector<int>Ac=A[comb];
			for(int&a:Ac)a=(K-P10*a%K)%K;
			sort(Ac.begin(),Ac.end());
			int u=(1<<N)-1-comb;
			sort(A[u].begin(),A[u].end());
			int ic=0,iu=0;
			while(ic<Ac.size()&&iu<A[u].size())
			{
				if(Ac[ic]<A[u][iu])ic++;
				else if(Ac[ic]>A[u][iu])iu++;
				else
				{
					int l=0;
					int p=A[u][iu];
					while(ic<Ac.size()&&p==Ac[ic])ic++,l++;
					while(iu<A[u].size()&&p==A[u][iu])iu++,ans+=l;
				}
			}
			int x=comb&-comb,y=comb+x;
			comb=(comb&~y)/x>>1|y;
		}
	}
	else
	{
		dfs(0,0,N-N/2);
		long P10=1;
		for(int i=0;i<N-N/2;i++)P10=P10*10%K;
		int comb=(1<<N/2)-1;
		while(comb<1<<N)
		{
			for(int&a:A[comb])a=(K-P10*a%K)%K;
			sort(A[comb].begin(),A[comb].end());
			int u=(1<<N)-1-comb;
			sort(A[u].begin(),A[u].end());
			int ic=0,iu=0;
			while(ic<A[comb].size()&&iu<A[u].size())
			{
				if(A[comb][ic]<A[u][iu])ic++;
				else if(A[comb][ic]>A[u][iu])iu++;
				else
				{
					int l=0;
					int p=A[u][iu];
					while(ic<A[comb].size()&&p==A[comb][ic])ic++,l++;
					while(iu<A[u].size()&&p==A[u][iu])iu++,ans+=l;
				}
			}
			int x=comb&-comb,y=comb+x;
			comb=(comb&~y)/x>>1|y;
		}
	}
	cout<<ans/V<<endl;
}
0