牛牛排队 解题报告
对于这个题来说,首先我们要判断n的奇偶性:
- 如果n是奇数,那么绝对值就只能是[0,2,4...,n-1],其中0只出现1次,其余的偶数各出现两次。
- 如果n是偶数,那么绝对值就只能是[1,3,5...,n-1], 其中每个奇数出现两次。
如果不满足上述两种情况的,结果为0,否则结果为pow(2, int(n/2))
代码如下:const int MAXN = 1e6+5; int cnt[MAXN]; typedef long long LL; const LL MOD = 1e9+7; LL Pow(LL a, LL b){ LL ans = 1; while(b){ if(b & 1) ans = ans*a%MOD; b>>=1; a = a*a%MOD; } return ans; } class Solution { public: /** * * @param n int整型 * @param a int整型vector * @return int整型 */ int solve(int n, vector<int>& a) { // write code here for(int i=0; i<n; i++) cnt[a[i]]++; int ok = 0; if(n & 1){ if(cnt[0] != 1) ok = 1; for(int i=2; i<n; i+=2){ if(cnt[i] != 2){ ok = 1; break; } } } else{ for(int i=1; i<n; i+=2){ if(cnt[i] != 2){ ok = 1; break; } } } if(ok) return 0; else return Pow(2, n/2); } };
基恩士成长空间 440人发布
查看19道真题和解析