开始自己用的DFS居然超时,改用DP法,呵呵,秒杀!
/* ID: qq104801 LANG: C++ TASK: subset */ #include <iostream> #include <fstream> #include <string> #include <vector> #include <cstdio> using namespace std; int n; size_t d[800]={0}; void test() { freopen("subset.in","r",stdin); freopen("subset.out","w",stdout); cin>>n; int sum=n*(n+1)/2; if(sum & 0x1u) { cout<<0<<endl; return; } sum>>=1; d[0]=1; for(int i=1;i<=n;++i) for(int j=sum;j>=i;--j) d[j]+=d[j-i]; cout<<d[sum]/2<<endl; } int main () { test(); return 0; }
testdata:
USER: cn tom [qq104801] TASK: subset LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.005 secs, 3372 KB] Test 2: TEST OK [0.005 secs, 3372 KB] Test 3: TEST OK [0.008 secs, 3372 KB] Test 4: TEST OK [0.005 secs, 3372 KB] Test 5: TEST OK [0.005 secs, 3372 KB] Test 6: TEST OK [0.005 secs, 3372 KB] Test 7: TEST OK [0.005 secs, 3372 KB] All tests OK. Your program ('subset') produced all correct answers! This is your submission #3 for this problem. Congratulations! Here are the test data inputs: ------- test 1 ---- 7 ------- test 2 ---- 15 ------- test 3 ---- 24 ------- test 4 ---- 31 ------- test 5 ---- 36 ------- test 6 ---- 39 ------- test 7 ---- 37 Keep up the good work! Thanks for your submission!