TopCoder

User's AC Ratio

100.0% (1/1)

Submission's AC Ratio

100.0% (1/1)

Tags

Description


For many sets of consecutive integers from 1 through N (1 <= N <= 39),
one can partition the set into two sets whose sums are identical.


For example, if N=3, one can partition the set {1, 2, 3} in one way so
that the sums of both subsets are identical:


  • {3} and {1,2}


This counts as a single partitioning (i.e., reversing the order counts
as the same partitioning and thus does not increase the count of
partitions).


If N=7, there are four ways to partition the set {1, 2, 3, ... 7} so that
each partition has the same sum:


  • {1,6,7} and {2,3,4,5}
  • {2,5,7} and {1,3,4,6}
  • {3,4,7} and {1,2,5,6}
  • {1,2,4,7} and {3,5,6}


Given N, your program should print the number of ways a set containing
the integers from 1 through N can be partitioned into two sets whose
sums are identical. Print 0 if there are no such ways.


Your program must calculate the answer, not look it up from a table. TA will check your code after the deadline.

Input Format

The input file contains several test cases, each on a line with a single integer representing N on each line, as above.

Output Format

The output file contains a single line for each test case with a single integer that tells how many same-sum partitions can be made from the set {1, 2, ..., N}. The output file should contain 0 if there are no ways to make a same-sum partition.

Sample Input 1

7

Sample Output 1

4

Hints

Dynamic Programming


以下僅供參考, 有錯不負責

DP的解法是有一定規則可以參考的, 不過這需要靠經驗的累積, 在這邊提供一些我的想法.

當你猜測題目解法是DP時, 先假設用一維可以解決, 有了維度以後就要開始找遞迴關係, 以這題為例, 假設用一維的DP就可以解決, 那麼可以假設一個A(i), 至於A(i)是什麼涵義以及其遞迴關係就需要靠自己去找, 比如說我可以假設A(i)表示N=i時此問題的解, 也可以假設A(i)表示從1到N的數中要湊出i有幾種方法.


當一維想不出解法時, 可以視著把維度提高, 也就是假設一個二維的涵數, 以這題為例, 一個參考的假設方法是令B(i, j)=由前i個數中, 要湊出和為j的方法, 如果能夠在合理的時間內把所有需要的B(i, j)算出來, 則B(N, N(N+1)/4)即是所要的解答


當找出遞迴關係以後, 當然也不是笨笨的直接遞迴call下去, 如此的話下場應該不是Time Limit Exceed就是Runtim Error

Problem Source

Migrated from old NTUJ.

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 1000 65536 20