結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー publflpublfl
提出日時 2021-07-30 21:29:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,497 ms / 3,000 ms
コード長 1,472 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 71,064 KB
実行使用メモリ 88,748 KB
最終ジャッジ日時 2023-10-14 03:35:52
合計ジャッジ時間 49,311 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 9 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 1,636 ms
88,440 KB
testcase_06 AC 1,665 ms
88,448 KB
testcase_07 AC 1,612 ms
88,480 KB
testcase_08 AC 1,305 ms
55,284 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2,410 ms
88,692 KB
testcase_12 AC 2,455 ms
88,748 KB
testcase_13 AC 2,431 ms
88,636 KB
testcase_14 AC 2,497 ms
88,684 KB
testcase_15 AC 2,477 ms
88,632 KB
testcase_16 AC 2,409 ms
88,684 KB
testcase_17 AC 414 ms
21,532 KB
testcase_18 AC 538 ms
26,384 KB
testcase_19 AC 1,013 ms
43,448 KB
testcase_20 AC 659 ms
39,872 KB
testcase_21 AC 60 ms
7,884 KB
testcase_22 AC 2,017 ms
88,476 KB
testcase_23 AC 2,110 ms
88,516 KB
testcase_24 AC 2,251 ms
88,556 KB
testcase_25 AC 2,340 ms
88,620 KB
testcase_26 AC 2,350 ms
88,604 KB
testcase_27 AC 2,391 ms
88,616 KB
testcase_28 AC 2,402 ms
88,624 KB
testcase_29 AC 2,418 ms
88,640 KB
testcase_30 AC 2,359 ms
88,732 KB
testcase_31 AC 2,327 ms
88,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <map>

int limit,a,b;
std::vector<int> V,V1,V2;
std::map<long long int,int> M,check1,check2;

long long int P[110];
long long int ans = 0;
void func(int k)
{
	if(k>=a)
	{
		if(V1.size()!=limit) return;
		
		long long int temp = 0;
		for(int i=0;i<limit;i++) temp += P[i]*V1[i];
		if(check1[temp]>0) return;
		
		M.clear();
		check2.clear();
		std::vector<int> W1 = V1, W2 = V2;
		std::sort(W1.begin(),W1.end());
		std::sort(W2.begin(),W2.end());
		do{
			long long int sum = 0;
			for(int i=0;i<limit;i++) sum += P[i]*W1[i];
			if(check1[sum]>0) continue;
			check1[sum] = 1;
			M[sum%b]++;
		}while(std::next_permutation(W1.begin(),W1.end()));
		
		do{
			long long int sum = 0;
			for(int i=limit;i<a;i++) sum += P[i]*W2[i-limit];
			if(check2[sum]>0) continue;
			
			check2[sum] = 1;
			ans += M[(b-sum%b)%b];
		}while(std::next_permutation(W2.begin(),W2.end()));
		return;
	}
	
	V1.push_back(V[k]);
	func(k+1);
	V1.pop_back();
	
	V2.push_back(V[k]);
	func(k+1);
	V2.pop_back();
	
}

int c[110];

int main()
{
	scanf("%d%d",&a,&b);
	for(int i=1;i<=9;i++) scanf("%d",&c[i]);
	if(a==1)
	{
		for(int i=1;i<=9;i++)
		{
			if(c[i]==1)
			{
				if(i%b==0) printf("1");
				else printf("0");
				return 0;
			}
		}
	}
	
	for(int i=1;i<=9;i++) while(c[i]--) V.push_back(i);
	P[0] = 1;
	for(int i=1;i<a;i++) P[i] = (10*P[i-1]);
	if(a%2==0) limit = a/2;
	else limit = (a+1)/2;
	
	func(0);
	printf("%lld",ans);
}
0