結果

問題 No.1634 Sorting Integers (Multiple of K) Hard
ユーザー chocoruskchocorusk
提出日時 2021-07-30 21:34:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,694 ms / 3,000 ms
コード長 1,922 bytes
コンパイル時間 3,516 ms
コンパイル使用メモリ 194,328 KB
実行使用メモリ 89,044 KB
最終ジャッジ日時 2023-10-14 03:44:16
合計ジャッジ時間 34,435 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 67 ms
14,000 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 209 ms
4,348 KB
testcase_06 AC 194 ms
4,356 KB
testcase_07 AC 215 ms
4,352 KB
testcase_08 AC 541 ms
55,288 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 930 ms
88,812 KB
testcase_12 AC 2,694 ms
79,788 KB
testcase_13 AC 931 ms
88,788 KB
testcase_14 AC 2,251 ms
88,828 KB
testcase_15 AC 2,452 ms
88,396 KB
testcase_16 AC 932 ms
88,724 KB
testcase_17 AC 355 ms
21,724 KB
testcase_18 AC 229 ms
26,776 KB
testcase_19 AC 726 ms
43,080 KB
testcase_20 AC 298 ms
40,096 KB
testcase_21 AC 29 ms
8,388 KB
testcase_22 AC 1,893 ms
27,888 KB
testcase_23 AC 2,297 ms
47,784 KB
testcase_24 AC 2,347 ms
67,232 KB
testcase_25 AC 2,269 ms
73,512 KB
testcase_26 AC 1,979 ms
82,200 KB
testcase_27 AC 1,437 ms
85,304 KB
testcase_28 AC 1,444 ms
88,724 KB
testcase_29 AC 797 ms
88,908 KB
testcase_30 AC 859 ms
89,044 KB
testcase_31 AC 1,003 ms
88,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
int n, k;
int c[10];
int n1,n2;
map<pair<int, ll>, int> mp, mp2;
ll ans;
void dfs(ll v, ll r, int t){
    if(t==n1){
        mp[make_pair(r, v)]++;
        // if(r==0){
        //     for(int i=0; i<3; i++) cout<<v[i];
        //     cout<<endl;
        // }
        return;
    }
    for(int i=1; i<=9; i++){
        ll d=((v>>((i-1)*4))&15);
        if(c[i]>d){
            v+=(1ll<<((i-1)*4));
            dfs(v, (r*10+i)%k, t+1);
            v-=(1ll<<((i-1)*4));
        }
    }
}
ll q;
ll c0;
void dfs2(ll v, ll r, int t){
    if(t==n2){
        ll x=(k-q*r%k);
        if(x>=k) x-=k;
        ll w=c0-v;
        auto itr=mp.find(make_pair(x, w));
        if(itr!=mp.end()){
            ans+=itr->second;
        }
        return;
    }
    for(int i=1; i<=9; i++){
        ll d=((v>>((i-1)*4))&15);
        if(c[i]>d){
            v+=(1ll<<((i-1)*4));
            dfs2(v, (r*10+i)%k, t+1);
            v-=(1ll<<((i-1)*4));
        }
    }
}
int main()
{
	cin>>n>>k;
	for(int i=1; i<=9; i++){
		cin>>c[i];
        c0^=((ll)c[i]<<((i-1)*4));
	}
    n1=min(7, n);
    dfs(0, 0, 0);
    if(n1==n){
        for(auto p:mp) if(p.first.first==0) ans+=p.second;
        cout<<ans<<endl;
        return 0;
    }
    n2=n-n1;
    q=1;
    for(int i=0; i<n1; i++) q=q*10%k;
    dfs2(0, 0, 0);
    cout<<ans<<endl;
    return 0;
}
0