1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5,P = 1e9 + 7; inline void Plus(int &x,const int &y) { x += y;if(x >= P) x -= P;} int n,k,a[N],fac[N],ifac[N]; inline int qpow(int a,int b) { int res = 1;while(b) { if(b&1) res = 1ll * res * a % P;a = 1ll * a * a % P;b >>= 1;} return res;} inline void init(int n) { fac[0] = 1; for(int i = 1;i <= n;i++) fac[i] = 1ll * fac[i - 1] * i % P; ifac[n] = qpow(fac[n],P - 2); for(int i = n - 1;i >= 0;i--) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % P; } inline int C(int n,int m) { if(n < 0 || m < 0 || n < m) return 0; return 1ll * fac[n] * ifac[m] % P * ifac[n - m] % P;} inline int Strling(int n,int m) { int res = 0; for(int i = 0;i <= m;i++) if(i & 1) Plus(res,P - 1ll * C(m,i) * qpow(m - i,n) % P); else Plus(res,1ll * C(m,i) * qpow(m - i,n) % P); return 1ll * res * ifac[m] % P; } long long ans; int Cnt = 1; void Solve(vector<int> A,int k,int dep) { assert(k <= A.size()); int cnt0 = 0,cnt1 = 0; for(auto i : A) if((i >> dep) & 1) ++cnt1; else ++cnt0; ans += min(k - (cnt0 > 0),cnt1) * (1ll << dep); if(dep == 0) { if(k > cnt1) Cnt = 1ll * Cnt * Strling(cnt0,k - cnt1) % P; else Cnt = 1ll * Cnt * Strling(1 + cnt1,k) % P; return; } if(k > cnt1) { vector<int> B; for(auto i : A) if((~i >> dep) & 1) B.push_back(i); else ans += i & ((1 << dep) - 1); Solve(B,k - cnt1,dep - 1); } else { vector<int> B;int andsum = INT_MAX; for(auto i : A) if((i >> dep) & 1) B.push_back(i); else andsum &= i; if(cnt0) B.push_back(andsum); Solve(B,k,dep - 1); } } int main() { cin >> n >> k; for(int i = 1;i <= n;i++) cin >> a[i]; init(n); vector<int> all(a + 1,a + n + 1); Solve(all,k,30); Cnt = 1ll * Cnt * fac[k] % P; printf("%lld %d\n",ans,Cnt); return 0; }
|