• 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;
    }
    

      

  • 相关阅读:
    jQuery工具函数
    jqXHR对象
    跨域获取
    Ajax :六个全局事件
    表单序列化
    Ajax : $. get()和$.post() $.getScript $.getJSON
    Ajax : load()
    列队动画
    关于MindManager显示不同级别的控制
    Mybatis 查询传多个参数(3中方法)
  • 原文地址:https://www.cnblogs.com/SfailSth/p/6287504.html
Copyright © 2020-2023  润新知