符号三角形
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 664 Accepted Submission(s): 320
Problem Description
符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。 n=7时的1个符号三角形如下:
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -
+
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -
+
Input
每行1个正整数n <=24,n=0退出.
Output
n和符号三角形的个数.
Sample Input
15 16 19 20 0
Sample Output
15 1896 16 5160 19 32757 20 59984
Source
Recommend
lcy
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <stdlib.h>
#include<cctype>
#define rep(i,s,e) for(int i = (s);i<=(e);++i)
#define maxn 100100
#define INF 1000000000
using namespace std;
/*
//题目的重点是异或操作的应用
//我们记-为1+为0(为了便于异或操作)
//题目的代码是参考别人的。。。不是很好懂
//通过自己画一下可以知道:n=1 => p[1][1]
//n=2 =>p[1][2] + p[1][1](由n=1时已推出) => p[2][2]
//以此类推所以for循环中的p[1][n]刚好相当于逐步把第一行的符号放进去(而for循环本身也控制了放进哪个符号)
//然后自上而下推出。。。。。。
int cnt;
int p[30][30];//p[i][j]用来保存第i行第j列的符号
int Count[30];
void dfs(int n)
{
if(n>24)
{
return ;
}
else
{
for(int i = 0;i<2;++i)//一共两种操作符+和-
{
p[1][n] = i;//第一行最后一个数放i(0为+ ,1为-)
cnt += i ;//记录减号的个数
for(int j = 2;j<=n;++j)
{
//每次往第一行加一个符号所以开始算的地方肯定是第二行的最后一个数
//然后是第三行的最后一个数(上一个操作相当于往第二行建一个符号!!!!!!)
p[j][n-j+1] = p[j-1][n-j+1]^p[j-1][n-j+2];//很巧妙的用法
cnt += p[j][n-j+1];
}
if(n*(n+1)/2==cnt*2)
Count[n]++;
dfs(n+1);
//恢复原状因为还有另一层循环没做(这一层只考虑了最后一个元素是+还要做-)
for(int j = 2;j<=n;++j)
{
cnt -= p[j][n-j+1];
}
cnt -= i;
}
}
}
int main()
{
memset(Count,0,sizeof(Count));
//memset(p,0,sizeof(p));
cnt = 0;
dfs(1);
for(int i = 1;i<=24;++i)
printf("%d\n",Count[i]);
}*/
//时间卡得太紧。。看到的都是用打表过的。。。
int result[24]={0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757,59984,0,0,431095,822229};
int main()
{
int n;
cin>>n;
while(n!=0)
{
cout<<n<<" "<<result[n-1]<<endl;
cin>>n;
}
return 0;
}