-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset_v1.cpp
82 lines (75 loc) · 1.33 KB
/
subset_v1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
ID: wxx54331
PROG: subset
LANG: C++
*/
/*
这题用bfs做是不行的,因为数据量太大了,内存消耗过多。
当N较大时不要运行这个程序。。会死机。。这是个失败的程序。。
*/
#include<iostream>
#include<fstream>
#include<deque>
#include<vector>
using namespace std;
struct State {
vector<int> num;
int sum;
};
int bfs(int N);
int getSum(const State &state, int num);
int main()
{
ifstream fin("subset.in");
ofstream fout("subset.out");
int N;
fin >> N;
fout << bfs(N)/2 << endl;
fin.close();
fout.close();
return 0;
}
int bfs(int N)
{
int totalSum = (1+N)*N/2;
if(totalSum%2 != 0)
return 0;
int halfSum = totalSum/2;
deque< State > q;
State lastState, currentState;
lastState.sum = currentState.sum = 0;
int count = 0;
do
{
if(!q.empty())
{
lastState = q.front();
q.pop_front();
}
int num;
if(lastState.num.size() == 0)
num = 1;
else
num = lastState.num.back()+1;
for(; num <= N; ++num)
{
currentState = lastState;
int sum = getSum(currentState,num);
if(sum > halfSum)
continue;
else if(sum == halfSum)
count++;
else
{
currentState.sum = sum;
currentState.num.push_back(num);
q.push_back(currentState);
}
}
}while(!q.empty());
return count;
}
int getSum(const State &state, int num)
{
return state.sum+num;
}