結果
問題 |
No.1634 Sorting Integers (Multiple of K) Hard
|
ユーザー |
|
提出日時 | 2021-07-30 22:22:45 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 765 bytes |
コンパイル時間 | 769 ms |
コンパイル使用メモリ | 79,744 KB |
実行使用メモリ | 8,704 KB |
最終ジャッジ日時 | 2024-09-16 00:51:56 |
合計ジャッジ時間 | 5,306 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | TLE * 1 -- * 27 |
コンパイルメッセージ
main.cpp:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type] 38 | main() | ^~~~
ソースコード
#include<iostream> #include<map> using namespace std; int N,K,c[14]; long V=1; map<pair<int,int>,long>mp; void dfs1(int k,int now,int rest) { if(rest==0) { mp[make_pair(k,now)]++; } else { for(int i=0;i<N;i++)if(!(k>>i&1)) { dfs1(k|1<<i,(now*10L+c[i])%K,rest-1); } } } long ans=0; long P10; void dfs2(int k,int now,int rest) { if(rest==0) { pair<int,int>p=make_pair((1<<N)-1-k,(K-now*P10%K)%K); if(mp.find(p)!=mp.end())ans+=mp[p]; } else { for(int i=0;i<N;i++)if(!(k>>i&1)) { dfs2(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; } dfs1(0,0,N/2); P10=1; for(int i=0;i<N/2;i++)P10*=10; P10%=K; dfs2(0,0,N-N/2); cout<<ans/V<<endl; }