結果
問題 | No.1634 Sorting Integers (Multiple of K) Hard |
ユーザー |
|
提出日時 | 2021-07-30 22:34:17 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,711 ms / 3,000 ms |
コード長 | 1,692 bytes |
コンパイル時間 | 786 ms |
コンパイル使用メモリ | 79,008 KB |
実行使用メモリ | 104,960 KB |
最終ジャッジ日時 | 2024-09-16 01:17:10 |
合計ジャッジ時間 | 55,416 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 4 ms
6,944 KB |
testcase_04 | AC | 1,185 ms
104,832 KB |
testcase_05 | AC | 1,183 ms
104,960 KB |
testcase_06 | AC | 1,299 ms
104,832 KB |
testcase_07 | AC | 1,181 ms
104,832 KB |
testcase_08 | AC | 2,137 ms
104,832 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 3 ms
6,944 KB |
testcase_11 | AC | 2,253 ms
104,832 KB |
testcase_12 | AC | 2,711 ms
104,832 KB |
testcase_13 | AC | 2,236 ms
104,832 KB |
testcase_14 | AC | 2,489 ms
104,832 KB |
testcase_15 | AC | 2,521 ms
104,960 KB |
testcase_16 | AC | 2,226 ms
104,832 KB |
testcase_17 | AC | 2,454 ms
104,832 KB |
testcase_18 | AC | 2,210 ms
104,832 KB |
testcase_19 | AC | 2,518 ms
104,960 KB |
testcase_20 | AC | 991 ms
61,440 KB |
testcase_21 | AC | 973 ms
61,440 KB |
testcase_22 | AC | 2,406 ms
104,960 KB |
testcase_23 | AC | 2,630 ms
104,960 KB |
testcase_24 | AC | 2,681 ms
104,960 KB |
testcase_25 | AC | 2,573 ms
104,832 KB |
testcase_26 | AC | 2,487 ms
104,832 KB |
testcase_27 | AC | 2,267 ms
104,832 KB |
testcase_28 | AC | 2,194 ms
104,832 KB |
testcase_29 | AC | 1,957 ms
104,960 KB |
testcase_30 | AC | 1,655 ms
104,832 KB |
testcase_31 | AC | 2,124 ms
104,832 KB |
コンパイルメッセージ
main.cpp:22:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 22 | main() | ^~~~
ソースコード
#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/2;i++)P10=P10*10%K; int comb=(1<<N-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; }