将答案的公式结合起来化简可得
,
这样不会爆long long,只需要处理出最终趋于稳定的数组的形式,可知a+b=a&b+a|b,操作无数次,总和是不变的
AC代码:
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; LL gcd(LL x, LL y) { return y == 0 ? x : gcd(y, x % y); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); LL n; cin >> n; vector<LL> a(n + 1); LL maxx = 0; LL sum = 0; vector<LL> pos(16, 0); for (int i = 1; i <= n; i ++ ) { cin >> a[i]; sum += a[i]; } if (sum == 0) { cout << "0/1\n"; return 0; } for (int i = 1; i <= n; i ++ ) { for (int j = 0; j <= 15; j ++ ) { if(1 << j & a[i]) { pos[j]++; } } } vector<LL> x; for (int i = 1; i <= n; i ++ ) { LL t = 0; for (int j = 15; j >= 0; j -- ) { if(pos[j]) { t += (1 << j); pos[j]--; } } if(t) x.push_back(t); else break; } int len = x.size(); LL ans = -sum * sum; for (int i = 0; i < n; i++) { if (i < len) ans += n * x[i] * x[i]; else { break; } } LL c = gcd(ans, n * n); cout << ans / c << "/" << n * n / c << '\n'; return 0; }
01-20
07-24
1037
07-31
161
07-24
151
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交