結果
| 問題 |
No.1631 Sorting Integers (Multiple of K) Easy
|
| コンテスト | |
| ユーザー |
momoyuu
|
| 提出日時 | 2024-10-06 20:01:59 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,223 ms / 3,000 ms |
| コード長 | 1,865 bytes |
| コンパイル時間 | 1,814 ms |
| コンパイル使用メモリ | 135,548 KB |
| 実行使用メモリ | 76,672 KB |
| 最終ジャッジ日時 | 2024-10-06 20:02:12 |
| 合計ジャッジ時間 | 11,838 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 28 |
ソースコード
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
#include<map>
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n,k;
cin>>n>>k;
vector<int> c(10,0);
for(int i = 1;i<=9;i++) cin>>c[i];
if(n==1){
int ans = 0;
for(int i = 1;i<=9;i++) if(i%k==0) ans += c[i];
cout<<ans<<endl;
return 0;
}
vector<ll> a,b;
ll now = 1;
for(int i = 0;i<n;i++){
if(i%2==0) a.push_back(now);
else b.push_back(now);
now *= 10;
}
map<vector<int>,map<ll,ll>> m1,m2;
vector<int> use(10,0);
ll ans = 0;
auto calc = [&](vector<int>&now) {
vector<int> all;
for(int i = 1;i<=9;i++) for(int j = 0;j<now[i];j++) all.push_back(i);
sort(all.begin(),all.end());
do{
ll sum = 0;
for(int i = 0;i<a.size();i++){
sum += a[i] * all[i];
sum %= k;
}
m1[now][sum]++;
}while(next_permutation(all.begin(),all.end()));
};
auto dfs = [&](auto dfs,int ni,int res) -> void {
if(res==0){
calc(use);
return;
}
for(int i = ni;i<=9;i++){
if(c[i]==0) continue;
c[i]--;
use[i]++;
dfs(dfs,i,res-1);
use[i]--;
c[i]++;
}
};
dfs(dfs,1,a.size());
swap(a,b);
swap(m1,m2);
dfs(dfs,1,a.size());
for(auto itr:m1){
auto use = itr.first;
vector<int> want(10,0);
for(int i = 1;i<=9;i++) want[i] = c[i] - use[i];
auto now = m2[want];
for(auto nn:itr.second){
ll w = k - nn.first;
w %= k;
if(w<0) w += k;
ans += nn.second * now[w];
}
}
cout<<ans<<endl;
}
momoyuu