题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5229
ZCC loves strings
解题思路:
结论:对于串a和b,游戏中先手必胜当且仅当 |a|+|b| 为奇数或 a=b . 我们按 |a|+|b| 的大小从小到大考虑所有的情况。 当 |a|+|b|=0 时,显然先手必败,符合结论。 假设已经证明了 |a|+|b|=k(k<p) 的所有情况满足结论,现在考虑 |a|+|b|=p 的情况。 若 p 是奇数,先手只需要选择长度较短的不为空的串,并使用A操作,就可以转移到 |a|+|b| 为偶数并且两个串不相等或者两个串均为空的情况,这种情况先手必败,故此时先手必胜。 若 p 是偶数,如果两个串相等,显然先手只需要选择使用B操作就能获得胜利了。否则,无论先手如何操作,都只能转移到 |a|+|b| 为奇数的先手必胜的情况。故此时先手必败。 因此,按顺序考虑每一个串,求得在其之前出现的串中,长度奇偶性与其不同的串共有x个,与其完全相同的串有y个,则对答案有 x+y 的贡献。累加即可。
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
typedef __int64 ll;
ll x,y;//x为奇数,y为偶数
string s[20005];
int gcd(ll x,ll y)
{
if(y == 0)
return x;
else
return gcd(y,x%y);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int i;
ll ans,sum,j = 0,n;
x = 0;y = 0;
scanf("%I64d",&n);
for(i = 0;i < n; i++)
{
cin>>s[i];
if(s[i].size()%2==0)
y++;
else
x++;
}
ans = x*y;
sum = n*(n-1)/2;
sort(s,s+n);
for(i = 0; i < n-1; i++)
{
if(s[i] == s[i+1])
{
j++;
ans+=j;
}
else
j = 0;
}
if(ans == 0)
printf("0/1\n");
else
{
ll tmp=gcd(ans,sum);
printf("%lld/%lld\n",ans/tmp,sum/tmp);
}
}
return 0;
}