[TopCoder14580] EllysRPS
题意
有\(m\)个人都要跟你进行\(n\)场石头剪刀布的游戏。你决定使用相同的出拳序列来迎战这\(m\)个人。
然后你又想和这\(m\)个人都打成平局。求方案数。
\(n\le20,m\le100\)
sol
总状态数显然是\(3^{20}\)对吧。
考虑\(Meet\ in\ the\ middle\)。先\(3^{10}\)爆搜出前半段的所有方案,用\(map\)存下来。再\(3^{10}\)搜出后半段的状态,直接去\(map\)里面去查。
使用\(multiset\)会\(T\)。
code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
#define ll long long
#define vi vector<int>
int win[3][3]={
{0,-1,1},
{1,0,-1},
{-1,1,0}
};
int n,m,a[105][25],b[25];
map<vi,int>M;
ll ans;
void record()
{
vi tmp;tmp.clear();
for (int i=1;i<=m;++i)
{
int x=0;
for (int j=1;j<=n/2;++j) x+=win[b[j]][a[i][j]];
tmp.push_back(x);
}
++M[tmp];
}
void dfs1(int u)
{
if (u==n/2+1) {record();return;}
for (int i=0;i<=2;++i)
b[u]=i,dfs1(u+1);
}
void query()
{
vi tmp;tmp.clear();
for (int i=1;i<=m;++i)
{
int x=0;
for (int j=n/2+1;j<=n;++j) x+=win[b[j]][a[i][j]];
tmp.push_back(-x);
}
ans+=M[tmp];
}
void dfs2(int u)
{
if (u==n/2) {query();return;}
for (int i=0;i<=2;++i)
b[u]=i,dfs2(u-1);
}
ll yyb_solve()
{
dfs1(1);dfs2(n);
return ans;
}
/*
int main()
{
m=42;n=14;
for (int i=1;i<=m;++i)
for (int j=1;j<=n;++j)
{
char ch=getchar();
while (ch!='R'&&ch!='P'&&ch!='S') ch=getchar();
if (ch=='R') a[i][j]=0;
if (ch=='P') a[i][j]=1;
if (ch=='S') a[i][j]=2;
}
printf("%lld\n",yyb_solve());return 0;
}
*/
class EllysRPS{
public:
ll getCount(vector<string>s)
{
m=s.size();n=s[0].size();
for (int i=1;i<=m;++i)
for (int j=1;j<=n;++j)
{
if (s[i-1][j-1]=='R') a[i][j]=0;
if (s[i-1][j-1]=='P') a[i][j]=1;
if (s[i-1][j-1]=='S') a[i][j]=2;
}
return yyb_solve();
}
};