zoukankan      html  css  js  c++  java
  • BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

    【题目分析】

        劳逸结合好了。

        杨辉三角+暴搜。

    【代码】

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstdlib>
    
    #include <map>
    #include <set>
    #include <queue>
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    #define maxn 500005
    #define inf 0x3f3f3f3f
    #define F(i,j,k) for (int i=j;i<=k;++i)
    #define D(i,j,k) for (int i=j;i>=k;--i)
    
    void Finout()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        #endif
    }
    
    int Getint()
    {
        int x=0,f=1; char ch=getchar();
        while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
        while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
        return x*f;
    }
    
    int n,sum,a[15][15];
    int list[15],hav[15];
    
    bool dfs(int step,int s)
    {
    	if (s>sum) return false;
    	if (step==n+1&&s==sum) return true;
    	if (step==n+1) return false;
    	F(i,1,n)
    	{
    		if (!hav[i])
    		{
    			list[step]=i;
    			hav[i]=1;
    			if (dfs(step+1,s+a[n][step]*i)) return true;
    			hav[i]=0;
    		}
    	}
    	return false;
    }
    
    int main()
    {
        Finout();
        n=Getint(); sum=Getint(); 
        F(i,1,12){a[i][1]=1;F(j,2,i)a[i][j]=a[i-1][j]+a[i-1][j-1];}
    //    F(i,1,12)
    //    {
    //    	F(j,1,i)
    //    		cout<<a[i][j]<<" ";
    //    	cout<<endl;
    //	}
    	if (dfs(1,0)) F(i,1,n-1) cout<<list[i]<<" ";
    	cout<<list[n]<<endl;
    }
    

      

  • 相关阅读:
    12_RHEL7.1普通用户添加sudo权限
    11_RHEL安装Maya2015
    10_RHEL安装搜狗输入法
    Fedora21源配置与显卡安装
    Centos7下Intel与AMD双显卡驱动的安装
    09_linux下安装Nvidia显卡驱动
    firewall-cmd --reload 防火墙
    CSS 单行 多行文本溢出显示省略号
    css3 渐变色
    Error: Cannot find module 'gulp-sass'
  • 原文地址:https://www.cnblogs.com/SfailSth/p/6287504.html
Copyright © 2011-2022 走看看