hdu 2510 符号三角形 dfs+打表
2014-08-12 09:02
204 查看
题意:
符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。
题解:
我们将+认为1,-认为是0。那么题意中的操作就变成了异或操作。
我们从一个符号开向多个符号推导,我们发现对于新增的行,只要第一个符号确定,那么这行之后的符号就都会被确定(因为之后的符号就是上一层的符号和左边符号的异或)。所以我们每次增加行的时候只需要枚举下第一个符号即可,之后统计1的个数和总数比较就能判断是否符合条件。我们用dfs来推导。这种方式耗时很长(耗时6S多),所以不能直接用,不过可以用来求的各个值的结果。
由于结果是静态的,所以我们可以直接用dfs得到结果,然后打表输出。
代码:
符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。
题解:
我们将+认为1,-认为是0。那么题意中的操作就变成了异或操作。
我们从一个符号开向多个符号推导,我们发现对于新增的行,只要第一个符号确定,那么这行之后的符号就都会被确定(因为之后的符号就是上一层的符号和左边符号的异或)。所以我们每次增加行的时候只需要枚举下第一个符号即可,之后统计1的个数和总数比较就能判断是否符合条件。我们用dfs来推导。这种方式耗时很长(耗时6S多),所以不能直接用,不过可以用来求的各个值的结果。
由于结果是静态的,所以我们可以直接用dfs得到结果,然后打表输出。
代码:
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <iostream> #include <vector> #include <map> using namespace std; const int maxn=30; int ans[maxn]={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; while(scanf("%d",&n)!=EOF) { if(n==0)break; printf("%d %d\n",n,ans[n-1]); } return 0; } /* //正常dfs,耗时6S多。 #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <iostream> #include <vector> #include <map> using namespace std; const int maxn=30; int f[maxn][maxn]; int res[maxn]; void dfs(int t,int s) { int i,j,ans; for(i=0;i<2;i++) { f[t][0]=i; ans=i; for(j=1;j<t;j++) { f[t][j]=f[t][j-1]^f[t-1][j-1]; ans+=f[t][j]; } if((1+t)*t/2==(ans+s)*2)res[t]++; if(t==24)continue; dfs(t+1,s+ans); } } void init() { memset(f,0,sizeof(f)); memset(res,0,sizeof(res)); dfs(1,0); } int main() { init(); for(int i=1;i<=24;i++) { if(i==1)printf("{%d",res[i]); else if(i==24)printf(",%d}\n",res[i]); else printf(",%d",res[i]); } } */
相关文章推荐
- hdu 2510 符号三角形 (DFS+打表)
- hdu 2510 符号三角形【DFS+打表】
- HDU 2510 符号三角形 dfs打表
- hdu 2510 符号三角形 (状压dp打表存入文件,O(1))
- 符号三角形(hdu 2510 搜索+打表)
- HDU 2510 符号三角形 暴力打表
- HDU 2510 符号三角形 深搜打表
- HDU 2510 符号三角形 【DFS】
- HDU 2510 符号三角形(dfs)
- HDU 2510--符号三角形【DFS】
- hdu-2510-符号三角形
- hdu 2510 符号三角形 很好的搜索题 要经常看
- HDU 2510 符号三角形
- hdu2510 dfs打表
- HDU 2510 - 符号三角形
- hdu 2510 符号三角形
- 符号三角形 HDU - 2510
- 符号三角形_hdu_2510(深搜).java
- hdu 2510 符号三角形(dfs+打表)
- 杭电 2510 符号三角形(DFS与南阳的一样)