Hdu 2510, symbol triangle, good search questions should be read frequently

Source: Internet
Author: User

Symbol triangle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 661 Accepted Submission (s): 317

 

Problem Description
The second line of the symbolic triangle contains n symbols consisting of "+" and "-". In the future, each line has one fewer symbol than the uplink, and the two with the same number are "+", the two differences are "-". Calculate the number of different signed triangles so that they contain the same number of "+" and. The following is a signed triangle with n = 7:
++-+-++
+--+
-++-
-++-
-+-
--
+

 


Input
One positive integer n <= 24, n = 0 in each row exits.

 


Output
N and the number of signed triangles.

 


Sample Input
15
16
19
20
0


Sample Output
15 1896
16 5160
19 32757
20 59984


Source
Ecjtu2008 Autumn Contest
 


Recommend
Lcy
Plane ticket:
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2510
 
 
At the beginning, I wrote a few groups of data to find the rule, but I was too lazy to search for tables.
Later, Baidu used the tabulation method.
However, when Baidu came back, the brute-force search was actually stuck, and many details were not written, so it won't be processed. Later, it was done by referring to other people's code.
You need to work hard. Come on, cool.
 
Consider-'as 1,' + 'as 0. This satisfies the question.

We know that 1 ^ 1 = 0; 1 ^ 0 = 1, 0 ^ 1 = 1, 0 ^ 0 = 0;

Then we can perform an exclusive or operation.

 

Brute-force code on

[Cpp] # include <stdio. h>
# Include <string. h>
Int ans [30];
Int a [30] [30]; // The intermediate process used to save the state of the first layer of each n
Int count; // number of records 1
Void DFS (int n)
{
Int I, j;
If (n> 24) return;
For (I = 0; I <= 1; I ++)
{
A [1] [n] = I; // only the first line of brute force is required.
Count + = I;
For (j = 2; j <= n; j ++)
{
A [j] [n-j + 1] = a [J-1] [n-j + 1] ^ a [J-1] [n-j + 2];
Count + = a [j] [n-j + 1];
}
If (count * 2 = n * (n + 1)/2)
Ans [n] ++;
DFS (n + 1 );
Count-= I;
For (j = 2; j <= n; j ++)
{
A [j] [n-j + 1] = a [J-1] [n-j + 1] ^ a [J-1] [n-j + 2];
Count-= a [j] [n-j + 1];
}
 
}
}
Int main ()
{
Int I, j, k, n;
Memset (ans, 0, sizeof (ans ));
Count = 0;
DFS (1 );
While (scanf ("% d", & n )! = EOF)
{
If (! N) break;
Printf ("% d \ n", ans [n]);
 
}
Return 0;
}

# Include <stdio. h>
# Include <string. h>
Int ans [30];
Int a [30] [30]; // The intermediate process used to save the state of the first layer of each n
Int count; // number of records 1
Void DFS (int n)
{
Int I, j;
If (n> 24) return;
For (I = 0; I <= 1; I ++)
{
A [1] [n] = I; // only the first line of brute force is required.
Count + = I;
For (j = 2; j <= n; j ++)
{
A [j] [n-j + 1] = a [J-1] [n-j + 1] ^ a [J-1] [n-j + 2];
Count + = a [j] [n-j + 1];
}
If (count * 2 = n * (n + 1)/2)
Ans [n] ++;
DFS (n + 1 );
Count-= I;
For (j = 2; j <= n; j ++)
{
A [j] [n-j + 1] = a [J-1] [n-j + 1] ^ a [J-1] [n-j + 2];
Count-= a [j] [n-j + 1];
}

}
}
Int main ()
{
Int I, j, k, n;
Memset (ans, 0, sizeof (ans ));
Count = 0;
DFS (1 );
While (scanf ("% d", & n )! = EOF)
{
If (! N) break;
Printf ("% d \ n", ans [n]);

}
Return 0;
}

 

 

Code submission:

[Cpp] # include <iostream>
Using namespace std;
Int result [24] = {171,410 };
Int main ()
{
Int n;
Cin> n;
While (n! = 0)
{
Cout <n <"" <result [n-1] <endl;
Cin> n;
}
Return 0;
}

# Include <iostream>
Using namespace std;
Int result [24] = {171,410 };
Int main ()
{
Int n;
Cin> n;
While (n! = 0)
{
Cout <n <"" <result [n-1] <endl;
Cin> n;
}
Return 0;
}
 

 

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.