京东笔试第二题
第二题
大佬求助,为啥我这个解法是错误的呢?0%通过lü'b
数据范围: n <= 1e5;
思路: 统计odd、even。然后求组合数。
#include
#include
#include
using namespace std;
typedef long long LL;
const int N = 1e5 + 10, mod = 1e9 + 7;
int fact[N],infact[N];
int n;
int a[N];
//快速幂求逆元
int qmi(int a, int k, int p)
{
int res = 1;
while(k){
if(k & 1) res = (LL) res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int main()
{
cin >> n;
int even = 0, odd = 0;
for(int i = 0; i < n; i ++)
{
cin >> a[i];
if(a[i] % 2 == 0) even ++;
else odd ++;
}
LL ans = 0;
fact[0] = infact[0] = 1;
for(int i = 1; i < N; i ++){
fact[i] = (LL)fact[i - 1] * i % mod;
infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
}
if(odd >= 2){
for(int i = 2; i <= odd ; i += 2){
ans += (LL)fact[odd] * infact[i] % mod * infact[odd - i] % mod;
}
}
if(even >= 2){
for(int i = 2; i <= even; i ++){
ans += (LL)fact[even] * infact[i] % mod * infact[even - i] % mod;
}
}
cout << ans << endl;
return 0;
}#京东笔试##京东2023秋招笔试好难啊#

