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
| #include <cstdio> #include <algorithm> typedef long long LL; using namespace std; const int maxn = 100010; LL a[maxn]; int And[maxn], Or[maxn], Xor[maxn]; int main() { int t, tt = 0; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); double ansAnd = 0.0, ansOr = 0.0, ansXor = 0.0, tmp = LL(n+1)*n/2.0; for (int i = 0; i < 31; ++i) { fill(And, And + n + 1, 0); fill(Or, Or + n + 1, 0); fill(Xor, Xor + n + 1, 0); LL sumAnd = 0, sumOr = 0, sumXor = 0; for (int j = 1; j <= n; ++j) { if ((1LL << i) & a[j]) { And[j] = And[j - 1] + 1; Or[j] = j; Xor[j] = j - Xor[j - 1]; } else { And[j] = 0; Or[j] = Or[j - 1]; Xor[j] = Xor[j - 1]; } sumAnd += And[j]; sumOr += Or[j]; sumXor += Xor[j]; } ansAnd += (1LL << i) * sumAnd / tmp; ansOr += (1LL << i) * sumOr / tmp; ansXor += (1LL << i) * sumXor / tmp; } printf("Case #%d: %.6lf %.6lf %.6lf\n", ++tt, ansAnd, ansOr, ansXor); } return 0; }
|