http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3155
枚举交换次数算期望太麻烦,不妨换个思路:对于任意一对数字,它们之间发生交换的概率和不交换的概率是相等的,那这对数字提供的期望值就为1/2。总共有C(n,2)对数字,所以最终的期望值就为n*(n-1)/4
完整代码:
/*0.015s*/
#include<cstdio>
int main()
{
long long x;
int T, cas = 0;
scanf("%d", &T);
while (T--)
{
scanf("%lld", &x);
x = x * (x - 1) >> 1;
printf("Case %d: ", ++cas);
if (x & 1) printf("%lld/2\n", x);
else printf("%lld\n", x >> 1);
}
}