結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー re_re0101re_re0101
提出日時 2021-07-30 22:54:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 437 ms / 3,000 ms
コード長 2,984 bytes
コンパイル時間 4,204 ms
コンパイル使用メモリ 239,980 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 07:27:55
合計ジャッジ時間 13,423 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 4 ms
4,352 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 200 ms
4,352 KB
testcase_06 AC 200 ms
4,348 KB
testcase_07 AC 201 ms
4,352 KB
testcase_08 AC 234 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 422 ms
4,348 KB
testcase_12 AC 374 ms
4,348 KB
testcase_13 AC 422 ms
4,352 KB
testcase_14 AC 424 ms
4,352 KB
testcase_15 AC 416 ms
4,352 KB
testcase_16 AC 422 ms
4,348 KB
testcase_17 AC 89 ms
4,348 KB
testcase_18 AC 105 ms
4,348 KB
testcase_19 AC 182 ms
4,352 KB
testcase_20 AC 100 ms
4,352 KB
testcase_21 AC 15 ms
4,348 KB
testcase_22 AC 265 ms
4,352 KB
testcase_23 AC 312 ms
4,348 KB
testcase_24 AC 369 ms
4,348 KB
testcase_25 AC 387 ms
4,348 KB
testcase_26 AC 412 ms
4,348 KB
testcase_27 AC 437 ms
4,352 KB
testcase_28 AC 416 ms
4,348 KB
testcase_29 AC 418 ms
4,352 KB
testcase_30 AC 417 ms
4,348 KB
testcase_31 AC 418 ms
4,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:43:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   43 | main(){
      | ^~~~

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define int long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define bit(n,k) (((ll)n>>(ll)k)&1) /*nのk bit目*/
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define eb emplace_back
#define endl '\n'
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;


template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
const long long MOD=1000000007;

ll modpow(ll a, ll n,ll mod=MOD) {

    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

ll functional(int x){
    if(x==0)return 1;
    else return x*functional(x-1);
}

int cntv[10];
int vec[10];

main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(12);
    /*--------------------------------*/

    int n,k;cin>>n>>k;
    int ans=0;
  	int ma=0;
    for(int i=1;i<=9;i++){
        cin>>cntv[i];
      	if(cntv[i])chmax(ma,i);
    }
    if(n==1){
        if(ma%k==0)cout<<1<<endl;
        else cout<<0<<endl;
        return 0;
    }
    unordered_map<int,int>exist;
    unordered_map<int,int>exist2;
    vector<int>vec(10); 
    auto dfs=[&](int cur,int cnt,auto &dfs)->void{
        if(cnt>n/2)return;
        if(cur==10){
            if(cnt<n/2)return;
            exist.clear();
            exist2.clear();
            vector<int>v;
            string s;
            for(int i=1;i<=9;i++){
                for(int j=0;j<vec[i];j++)v.pb(i);
            }
            vector<int>v2;
            string s2;
            vector<int>cnt2(10);
            for(int i=1;i<10;i++)cnt2[i]=cntv[i];
            for(int i=0;i<n/2;i++){
                cnt2[v[i]]--;
            }
            for(int i=1;i<=9;i++){
                for(int j=0;j<cnt2[i];j++)v2.push_back(i);
            }
            for(int i=0;i<v.size();i++)s+=char(v[i]+'0');
            for(int i=0;i<v2.size();i++)s2+=char(v2[i]+'0');
            do{            
                exist[stoi(s)%k]++;
            }while(next_permutation(all(s)));

            int modp=modpow(10,n/2,k);
            do{
                int cur2=stoi(s2)%k;
                cur2*=modp;
                cur2%=k;
                exist2[cur2]++;
            }while(next_permutation(all(s2)));
            if(exist.size()>exist2.size())swap(exist,exist2);
            for(auto p:exist){
                if(p.first==0)continue;
                ans+=p.second*exist2[k-p.first];
            }
            ans+=exist[0]*exist2[0];
            return;    
        }
        for(int i=0;i<=cntv[cur];i++){
            vec[cur]=i;
            dfs(cur+1,cnt+i,dfs);
        }
        return;
    };
    dfs(1,0,dfs);
    cout<<ans<<endl;
}
0