結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー re_re0101re_re0101
提出日時 2021-07-15 19:13:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 286 ms / 3,000 ms
コード長 4,908 bytes
コンパイル時間 4,097 ms
コンパイル使用メモリ 244,812 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 02:19:40
合計ジャッジ時間 7,689 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

//#pragma GCC optimize ("O3")
//#pragma GCC target("avx512f")
//#pragma GCC optimize("unroll-loops")
// #ifndef ONLINE_JUDGE
// #define _GLIBCXX_DEBUG
// #endif
#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()
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define PI 3.14159265359
const double eps = 1e-12;
const long long INF= (long long)1e18+20;
const int inf= 1010101010;
typedef double D;      // 座標値の型。doubleかlong doubleを想定
typedef complex<D> Point;  // Point
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl>vvl;
typedef vector<vvl>vvvl;
typedef vector<vvvl>vvvvl;
typedef vector<vvvvl>vvvvvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
template<class T> using minpq=priority_queue<T,vector<T>,greater<T>>;
const ll MOD=1000000007LL;
// const ll MOD=998244353LL;
// using mint=modint998244353;
using mint=modint1000000007;
// using mint=modint;
const ll mod=MOD;
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vl dx={0,0,1,-1,1,1,-1,-1};
vl dy={1,-1,0,0,-1,1,-1,1};

template<class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

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;}



typedef vector<mint>vm;
typedef vector<vm>vvm;
typedef vector<vvm>vvvm;
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;
}

vector<int>divisor(int n){
  vector<int>res;
  for(int i=1;i*i<=n;i++){
    if(n%i==0){
      res.push_back(i);
      if(i != n/i) res.push_back(n/i);
    }
  }
  return res;
}

//素因数分解O(√n)
map<ll,ll>prime_factor(ll n){
  map<ll,ll>res;
  for(ll i=2;i*i<=n;i++){
    while(n%i==0){
      res[i]++;
      n/=i;
    }
  }
  if(n!=1)res[n]=1;
  return res;
}

struct edge{
    ll to;ll cost;
};

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);
    //重複してしまうので
    //1の数が何個、2の数が何個、としてdfs    
    auto dfs=[&](int cur,int cnt,auto &dfs)->void{
        if(cnt>n/2)return;
        if(cur==10){
            if(cnt<n/2)return;
            //長さが1だと例外になりそう
            //処理
            // for(int i=0;i<k;i++)exist[i]=0;
            // for(int i=0;i<k;i++)exist2[i]=0; 
            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