結果

問題 No.1631 Sorting Integers (Multiple of K) Easy
ユーザー chocorusk
提出日時 2021-07-30 21:41:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,163 ms / 3,000 ms
コード長 1,918 bytes
コンパイル時間 3,948 ms
コンパイル使用メモリ 187,584 KB
最終ジャッジ日時 2025-01-23 11:26:17
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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;
ll ans;
void dfs(ll v, ll r, int t){
if(t==n1){
mp[make_pair(r, v)]++;
return;
}
ll r1=(r*10+1)%k;
for(int i=1; i<=9; i++){
ll d=((v>>((i-1)<<2))&15);
if(c[i]>d){
v+=(1ll<<((i-1)<<2));
dfs(v, r1, t+1);
v-=(1ll<<((i-1)<<2));
}
r1++;
if(r1>=k) r1-=k;
}
}
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;
}
ll r1=(r*10+1)%k;
for(int i=1; i<=9; i++){
ll d=((v>>((i-1)<<2))&15);
if(c[i]>d){
v+=(1ll<<((i-1)<<2));
dfs2(v, r1, t+1);
v-=(1ll<<((i-1)<<2));
}
r1++;
if(r1>=k) r1-=k;
}
}
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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0