Skip to content

Instantly share code, notes, and snippets.

@skyzh
Created May 16, 2015 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyzh/705fc70e7feb719a9b68 to your computer and use it in GitHub Desktop.
Save skyzh/705fc70e7feb719a9b68 to your computer and use it in GitHub Desktop.
Unknown Problem
// Input: N, Y ---> Output Several Numbers' Sum == Y
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX_NUMBER = 100;
const int MAX_SUM = 100;
using Index = unsigned int;
Index f[MAX_NUMBER][MAX_SUM];
vector<int> NList[MAX_NUMBER*MAX_SUM];
int NSum[MAX_NUMBER*MAX_SUM];
int ListUsed;
int s[MAX_NUMBER];
int N, Y;
int GetIndex(Index& Pos)
{
if (Pos != 0)return Pos;
Pos = ListUsed;
NSum[ListUsed] = 0;
ListUsed++;
return Pos;
}
vector<int>Path;
void PrintPath(int i, int Y)
{
if (Y - s[i] == 0)
{
for (const auto p : Path)
{
cout << s[p] << " ";
}
cout << endl;
return;
}
if (Y < 0)return;
int __index = GetIndex(f[i][Y]);
for (const auto p : NList[__index])
{
Path.push_back(p);
PrintPath(p, Y - s[i]);
Path.pop_back();
}
}
int main()
{
cin >> N >> Y;
ListUsed = 1;
for (int i = 0; i < N; i++)cin >> s[i];
memset(f, sizeof(f), 0);
int __index = GetIndex(f[0][s[0]]);
NSum[__index] = 1;
for (int i = 1; i < N; i++)
{
int __Index = GetIndex(f[i][s[i]]);
NSum[__Index] = 1;
for (int j = 0; j < i; j++)
{
for (int k = 1; k <= Y; k++)
{
int __PrevIndex = GetIndex(f[j][k]);
if (NSum[__PrevIndex] <= 0)continue;
int __CurIndex = GetIndex(f[i][k + s[i]]);
NSum[__CurIndex] += NSum[__PrevIndex];
NList[__CurIndex].push_back(j);
}
}
}
int ans = 0;
for (int i = 0; i < N; i++)
{
int __index = GetIndex(f[i][Y]);
Path.push_back(i);
if (NSum[__index] != 0)
{
ans += NSum[__index];
PrintPath(i, Y);
}
Path.pop_back();
}
cout << ans << endl;
system("PAUSE");
return 0;
}
@MsRotate
Copy link

This programm for Windows?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment