(周六赛1)Sum it up

题意:

Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0

Sample Output
Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25

注意每个数只能用一次,且不能出现重复的等式

题解 :dfs深搜

 1 #include<stdio.h>
 2 int a[20],ans[20];
 3 int sum,n,flag;
 4 void dfs(int x,int count,int m)
 5 {
 6     int last,i;
 7     if(m>sum)
 8     return;
 9     if(m==sum)
10     {
11         flag=1;
12         for(i=1;i<count;i++)
13         {
14             if(i==count-1)
15                 printf("%d\n",ans[i]);
16             else
17                 printf("%d+",ans[i]);
18         }
19     }
20     last=-1;
21     for(i=x;i<=n;i++)
22     {
23         if(last!=a[i])
24         {
25             ans[count]=a[i];
26             last=a[i];
27             dfs(i+1,count+1,m+a[i]);
28         }
29     }
30     return;
31 }
32
33 int main()
34 {
35     int i;
36     while(~scanf("%d %d",&sum,&n))
37     {
38         if(sum==0&&n==0)
39             break;
40         for(i=1;i<=n;i++)
41             scanf("%d",&a[i]);
42         flag=0;
43         printf("Sums of %d:\n",sum);
44         dfs(1,1,0);
45         if(!flag)
46         puts("NONE");
47     }
48     return 0;
49 }
时间: 2024-10-09 15:49:41

(周六赛1)Sum it up的相关文章

2017 ACM-ICPC 亚洲区(西安赛区)网络赛C. Sum【脑洞题】

限制:1000ms 32768K Define the function S(x) for xx is a positive integer. S(x) equals to the sum of all digit of the decimal expression of x. Please find a positive integer k that S(k?x)%233=0. Input Format First line an integer T, indicates the number

(周六赛) Vertical Histogram

题意:统计字母的个数,以“*”号输出 1 #include<stdio.h> 2 #include<algorithm> 3 #include<string.h> 4 int main() 5 { 6 char str[4][100]; 7 char map[100][100]; 8 int i,j; 9 int a[100]; 10 while(gets(str[0])) 11 { 12 memset(a,0,sizeof(a)); 13 memset(map,' '

【推导】计蒜客17116 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 C. Sum

题意:S(x)被定义为x的十进制表示下的数位之和.给你x,让你找一个正整数k,使得S(kx)能被233整除.k不超过2千位. 由于x不超过1000000,不论x是多少,10000000重复233次一定是合法的. #include<cstdio> using namespace std; int T,x; int main(){ scanf("%d",&T); for(;T;--T){ scanf("%d",&x); for(int i=1

2014上海网络赛 HDU 5053 the Sum of Cube

水 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include<math.h> 4 #include<iostream> 5 #define LL long long 6 using namespace std; 7 8 int main() 9 { 10 int t; 11 int a,b; 12 int cas; 13 LL sum; 14 while(~scanf("%d",&

hdu 5053 the Sum of Cube---2014acm上海赛区网络赛

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5053 the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 140    Accepted Submission(s): 80 Problem Description A range is given, the b

黑龙江省赛The minimum square sum

题目 The minimum square sum Time Limit 1000ms Memory Limit 65536K description Given a prime p (p<108),you are to find min{x2+y2},where x and y belongs to positive integer, so that x2+y2=0 (mod p). input Every line is a p. No more than 10001 test cases.

hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法

The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 23    Accepted Submission(s): 4 Problem Description You have an array ,the length of  is Let Input There are multiple test cases.

ZOJ 3813 Alternating Sum (牡丹江网络赛E题)

ZOJ 3813 Alternating Sum 题目链接 赛后补题中,这题真心恶心爆了 先推下公式,发现是隔一个位置,长度从最长每次减2,这样累加起来的和,然后就可以利用线段树维护,记录4个值,奇数和,偶数和,奇数答案和,偶数答案和,这样pushup的时候,对应要乘系数其实就是加上左边奇(偶)和乘上右边长度,线段树处理完,还有个问题就是查询可能横跨很多个区间,这样一来就要把区间进行分段,分成3段,然后和上面一样的方法合并即可,注意中间一段很长,不能一一去合并,可以推成等差数列,利用前n项和去搞

hdu 5053 the Sum of Cube(上海网络赛)

the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 405    Accepted Submission(s): 224 Problem Description A range is given, the begin and the end are both integers. You should sum