1800: [Ahoi2009]fly 飞行棋
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1822 Solved: 1434
[ Submit][ Status][ Discuss]
Description
给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依圆周顺序排列。 请找出这些点中有没有可以围成矩形的,并希望在最短时间内找出所有不重复矩形。
Input
第一行为正整数N,表示点的个数,接下来N行分别为这N个点所分割的各个圆弧长度
Output
所构成不重复矩形的个数
Sample Input
8
1
2
2
3
1
1
3
3
1
2
2
3
1
1
3
3
Sample Output
3
HINT
N<= 20
Source
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
const int INF = 2147483647;
const int maxn = 30;
const int maxs = 10000;
const LL seed = 163;
const LL hyj = 1e9 + 7;
LL ha[maxs];
int tots,s[maxs][10];
int n,ans,a[maxn],sum[maxn];
int mark[maxn];
inline LL getint()
{
LL ret = 0,f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
ret = ret * 10 + c - '0',c = getchar();
return ret * f;
}
inline void judge()
{
int j = 1,cur = 1;
memset(sum,0,sizeof(sum));
for (int i = 1; i <= n; i++)
{
if (mark[i]) cur++;
else sum[cur] += a[j++];
}
sum[1] += sum[5]; sum[5] = 0;
if (sum[1] == sum[3] && sum[2] == sum[4] && sum[1] && sum[2])
{
tots++;
cur = 0; j = 0;
for (int i = 1; i <= n; i++)
{
if (!mark[i]) j++;
else s[tots][++cur] = j;
}
if (s[tots][cur] == n - 4) s[tots][cur] = 0;
sort(s[tots] + 1,s[tots] + 5);
}
}
inline void dfs(int x,int cnt)
{
if (cnt == 4) {judge(); return;}
if (x > n) return;
for (int i = x; i <= n; i++)
{
mark[i] = 1;
dfs(i + 1,cnt + 1);
mark[i] = 0;
}
}
int main()
{
#ifdef dyf
freopen("hyj1.txt","r",stdin);
#endif
n = getint();
for (int i = 1; i <= n; i++) a[i] = getint();
n += 4; dfs(1,0);
for (int i = 1; i <= tots; i++)
for (int j = 1; j <= 4; j++)
ha[i] = (ha[i] * seed + s[i][j]) % hyj;
sort(ha + 1,ha + tots + 1);
for (int i = 1; i <= tots; i++)
if (ha[i] != ha[i - 1])
ans++;
printf("%d",ans);
return 0;
}