HDU 1258 Sum It Up (dfs)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[20];
int ans[20];
int num[200];
struct Mark
{
    int shu,rt;
};
Mark mark[20];
int ok;
int n,t;
int cnt;
int cmp(int x,int y)
{
    return x>y;
}
void dfs(int rt,int i,int j,int sum)
{
       if(sum>t) return ;
       int k,l;
       mark[rt].shu=a[i];
       mark[rt].rt=j;
       //printf("%d...\n",sum);
       if(sum==t)
       {
           ok=1; //printf("%d\n",rt);

           int coun=0;
           /*if(t==5&&rt==3)
           {
               printf("%d %d %d %d\n",rt,i,j,sum);
           }*/
           for(k=1;k<=rt;k++)
           {
              for(l=1;l<=mark[k].rt;l++)
              {
                  ans[coun++]=mark[k].shu;
              }
           }
           for(k=0;k<coun;k++)
           {
               if(k!=0) printf("+");
               printf("%d",ans[k]);
           }
           printf("\n");
           return ;
       }
       if(i<cnt-1)
       {
           for(k=num[a[i+1]];k>=0;k--)
           {
               dfs(rt+1,i+1,k,sum+a[i+1]*k);
           }
       }
}
int main()
{
    int i,j,k;
    int temp;
    //int cnt;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        if(t==0&&n==0) break;
        ok=0; cnt=0;
        memset(ans,0,sizeof(ans));
        memset(num,0,sizeof(num));
        memset(mark,0,sizeof(mark));

        for(i=0;i<n;i++)
        {
            scanf("%d",&temp);
            if(num[temp]==0)
            {
                a[cnt++]=temp;
            }
            num[temp]++;
        }
        //printf("%d..\n",cnt);
        sort(a,a+cnt,cmp);
        /*for(i=0;i<cnt;i++)
        {
            printf("%d %d..\n",a[i],num[a[i]]);
        }*/
        printf("Sums of %d:\n",t);

            for(i=num[a[0]];i>=0;i--)
            {
                dfs(1,0,i,a[0]*i);
            }

        if(ok==0)
            printf("NONE\n");
    }
    return 0;
}
时间: 2024-08-05 03:17:11

HDU 1258 Sum It Up (dfs)的相关文章

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

hdu 1258 Sum It Up(dfs+去重)

题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = [4,3,2,2,1,1]. 有四种不同的方法使得它们相加的结果等于total(即等于4),分别为:4,3+1,2+2, 2+1+1. 在同一种拼凑方式中,每个数字不能被重复使用,但是在list中可能存在许多相等的数字. 输入: 输入包含许多测试用例,每个用例仅占一行.每个用例包含t(total)

HDU 1258 Sum It Up(dfs 巧妙去重)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7758    Accepted Submission(s): 4067 Problem Description Given a specified total t a

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

HDU 2209 翻纸牌游戏(dfs)

翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2180    Accepted Submission(s): 787 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张

HDU 1258 Sum It Up (POJ 1564)

以前做过,碰巧看到了.我去复制了一下.很奇怪--交POJ的程序交HDU 就WA. 然后重写,交HDU的程序AC后再去交 POJ 居然TLE.简直-- 简单DFS,判重就好了. HDU : #include<cstdio> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<stack> #

hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002

题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1.  定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2.  对于树A与树B,如果A与B的节点数相同,且无论i为何值,f[A, i]与f[B, i]都相等,则A与B为两棵相似的树. 3.  对于一棵树A,在以节点1为根节点的情况下,如果不存在与其它树与A相似,则A是一棵特殊的树. 输入: 包含多组输入样例. 每组输入样例中首先输入一个整数n,表示一棵含有n个节点的树. 接下来n

hdu 5423 Rikka with Tree(dfs)

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of

HDU 1312 Red and Black (dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17773    Accepted Submission(s): 10826 Problem Description There is a rectangula