zoukankan      html  css  js  c++  java
  • hdu2510

    hdu2510 符号三角形
    传送门

    题意

    符号三角形每一行的字符都是'+'或者'-' ,第一行长度为(n(nleq 24)),之后每一行字符比上一行少(1),构成规则是上一行中,连续两个同号为'+',连续两个异号为'-'。给出(n),计算有多少个第一行有(n)个字符的符号三角形,满足'+'和'-'的数量相同。
    例如(n=7)时的一种合法构造为:
    ++-+-++
    +----+
    -+++-
    -++-
    -+-
    - -
    +

    题解

    dfs打表
    打表代码

    #include<bits/stdc++.h>
    #define LL long long
    #define PII pair<int,int>
    #define eps 1e-6
    #define lowbit(x) x&(-x)
    using namespace std;
    
    const int maxn=30;
    int n,a[maxn][maxn],ans,cnt0,cnt1;
    
    void dfs(int id){
    	if(id==n){
    		if(cnt0==cnt1) ans++;
    		return;
    	}
    	for(int i=0;i<n-id;i++){
    		if(a[id-1][i]==a[id-1][i+1]){
    			a[id][i]=1;
    			cnt1++;
    		}
    		else{
    			a[id][i]=0;
    			cnt0++;
    		}
    	}
    	dfs(id+1);
    }
    
    int main(){
    	freopen("data.out","w",stdout);
    	for(n=1;n<=24;n++){
    		int m=1<<n;
    		ans=0;
    		for(int i=0;i<m;i++){
    			cnt0=0;
    			cnt1=0;
    			for(int j=0;j<n;j++){
    				if(i&(1<<j)){
    					a[0][j]=1;
    					cnt1++;
    				}
    				else{
    					a[0][j]=0;
    					cnt0++;
    				}
    			}
    			dfs(1);
    		}
    		printf("%d,",ans);
    	}
    }
    

    代码

    #include<bits/stdc++.h>
    #define LL long long
    #define PII pair<int,int>
    #define eps 1e-6
    #define lowbit(x) x&(-x)
    using namespace std;
    
    int ans[25]={0,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 n;
    
    int main(){
    	while(scanf("%d",&n)!=EOF && n){
    		printf("%d %d
    ",n,ans[n]);
    	}
    }
    
  • 相关阅读:
    对象池使用时要注意几点
    Flash3D学习计划(一)——3D渲染的一般管线流程
    714. Best Time to Buy and Sell Stock with Transaction Fee
    712. Minimum ASCII Delete Sum for Two Strings
    647. Palindromic Substrings(马拉车算法)
    413. Arithmetic Slices
    877. Stone Game
    338. Counting Bits
    303. Range Sum Query
    198. House Robber
  • 原文地址:https://www.cnblogs.com/fxq1304/p/14527108.html
Copyright © 2011-2022 走看看