Set and-0-1 backpack

Source: Internet
Author: User

Set

Description

For a continuous integer from 1 to n, it can be divided into two subsets, and the number of each set are equal. For example, if n = 3, for {1, 2, 3}, it can be divided into two subsets. All their numbers are equal to: {3} and {1, 2 }, this is the only distribution (the switch set location is considered to be the same division scheme, so the total number of Division schemes will not be increased ).

For example, if n = 7, the set {1, 2, 3, 4, 5, 6, 7} has the following four partitioning methods:

(1), {, 7} and {,} {Note 1 + 6 + 7 = 2 + 3 + 4 + 5}

(2), {, 7} and}

(3), {3, 4, 7} and {1, 2, 5, 6}

(4), {,} and {, 6}

Given N, your program should output the total number of Division schemes. If such a division scheme does not exist, it will output 0. The program cannot store the result and output it directly.

Input

Only one row and only one integer n

Output

Total number of output partitioning schemes. If not, 0 is output.

Sample Input

 7

Sample output

 4
Code
   1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4  long long f[10000];
5 int n;
6 int main(){
7 FILE *in,*out;
8 in=fopen("input6.txt","r");
9 out=fopen("output.txt","w");
10 fscanf(in,"%d",&n);
11 if(((n*n+n)/2)%2==1)
12 {
13 fprintf(out,"0\n");
14 printf("0\n");
15 }
16 else
17 {
18 int i,j;
19 memset(f,0,sizeof(f));
20 f[1]=1;
21 for(i=2;i<=n;i++)
22 for(j=(n*n+n)/4;j>=1;j--)
23 {
24 if(j>i)
25 f[j]=f[j]+f[j-i];
26 if(j==i) f[j]++;
27 }
28
29 printf("%d\n",f[(n*n+n)/4]/2);
30 fprintf(out,"%d\n",f[(n*n+n)/4]/2);
31 }
32 system("pause");
33 fclose(in);
34 fclose(out);
35 return 0;
36 }

Transfer equation: F [I] [J] = f [I-1] [J] + F [I-1] [J-I],
Program idea: F [I] [J] indicates the number of J methods, which is equal to the number of I-1 represents the number of J methods, coupled with the number of I-1, number of J-I methods
Note that:

1. The program running result may be out of the Integer Range. Therefore, define f [] as the long type;
2. In order to save space, the first and second cycles need to be written backwards;
3. The final f [J] represents 1 ~ N indicates the total number of J methods. Therefore, the final output of F [(n * n + N)/4] is divided by 2;
4. The program first checks whether the sum of all numbers is an even number, and the maximum value of the second loop is half of the sum.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.