Usaco Chapter 1 Section 5

Source: Internet
Author: User
Tags cmath

1. Number triangle, poj1163, not to mention.

Code:

# Include <cstdio>
# Include <iostream>
# Include <cstring>
# Include <fstream>
# Include <cstdlib>
# Include <memory>
# Include <algorithm>
# Define read freopen ("numtri. In", "r", stdin)
# Define write freopen ("numtri. Out", "W", stdout)
# Define RD ifstream cout ("numtri. Out ");
# Define wt ifstream CIN ("numtri. In ");
# Define max (A, B) A> B? A: B
Int data [1001] [1001];
Int main (){
Read;
Write;
Int I, j, N;
Scanf ("% d", & N );
For (I = 0; I <n; I ++)
For (j = 0; j <= I; j ++)
Scanf ("% d", & Data [I] [J]);
For (I = n-2; I> = 0; I --)
For (j = 0; j <= I; j ++)
Data [I] [J] + = max (data [I + 1] [J], data [I + 1] [J + 1]);
Printf ("% d \ n", data [0] [0]);
Return 0 ;}

 

2. It is obviously impossible to create a prime number table for this question, so we need to get a return number. Two methods:

(1) 10000000 judge in turn whether it is a return, of course, you can skip a lot in the middle, such as an even number, for example, the length except 11 is an even number.

(2) generate an odd number of input objects with an odd Length plus 11.

First, I wrote it in the first way. After writing it, I added a variety of optimizations and pruning. The Code is the same as the pus, but it still times out. The second method is much better. Although the code is ugly, the efficiency is quite high. Slowest 0.03

Code:

# Include <cstdlib>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <string>
# Include <fstream>
# Define read freopen ("pprime. In", "r", stdin)
# Define write freopen ("pprime. Out", "W", stdout)
# Define RD ifstream cout ("pprime. Out ");
# Define wt ifstream CIN ("pprime. In ");
Int CNT, pa [1000001];
Void get_palin (){
Int I, J, K, L, G;
CNT = 0;
Pa [CNT ++] = 5;
Pa [CNT ++] = 7;
Pa [CNT ++] = 11;
For (I = 1; I <10; I + = 2)
For (j = 0; j <10; j ++)
Pa [CNT ++] = 100 * I + I + 10 * j;
For (I = 1; I <10; I + = 2)
For (j = 0; j <10; j ++)
For (k = 0; k <10; k ++)
Pa [CNT ++] = 10000 * I + I + 1000 * j + 10 * j + 100 * K;
For (I = 1; I <10; I + = 2)
For (j = 0; j <10; j ++)
For (k = 0; k <10; k ++)
For (L = 0; L <10; l ++)
Pa [CNT ++] = 1000000 * I + I + 100000 * j + 10 * j + 10000 * k + 100 * k + 1000 * l;
}
Int is_prim (int ){
For (INT I = 2; I <= SQRT (a); I ++)
If (a % I = 0) return 0;
Return 1;
}
Int main (){
Read;
Write;
Int I, n, m;
Scanf ("% d", & N, & M );
Get_palin ();
For (I = 0; I <CNT; I ++ ){
If (PA [I] <n | pa [I]> M |! Is_prim (PA [I]) continue;
Printf ("% d \ n", pa [I]);
}
Return 0;
}

 

3. Like the previous question, it is also directly generated and can be implemented recursively. I have written it as a.

Code:

# Include <cstdlib>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <string>
# Include <fstream>
# Define read freopen ("sprime. In", "r", stdin)
# Define write freopen ("sprime. Out", "W", stdout)
# Define RD ifstream cout ("sprime. Out ");
# Define wt ifstream CIN ("sprime. In ");
Int ans [10] [1, 100001], CNT [11];
Int is_prim (int ){
For (INT I = 2; I <= SQRT (a); I ++)
If (a % I = 0) return 0;
Return 1;
}
Int main (){
Read;
Write;
Int N, I, J, K;
Scanf ("% d", & N );
Memset (CNT, 0, sizeof (CNT ));
CNT [1] = 4;
Ans [1] [0] = 2, ANS [1] [1] = 3, ANS [1] [2] = 5, ANS [1] [3] = 7;
For (I = 2; I <9; I ++)
For (k = 0; k <CNT [I-1]; k ++)
For (j = 1; j <10; j + = 2 ){
Int T = ans [I-1] [k] * 10 + J;
If (is_prim (t) ans [I] [CNT [I] ++] = T;
}
For (I = 0; I <CNT [N]; I ++)
Printf ("% d \ n", ANS [N] [I]);
Return 0 ;}

 

4. Queen eight, DFS + backtracking.

Code:

# Include <cstdio>
# Include <iostream>
# Include <cstring>
# Include <fstream>
# Include <cstdlib>
# Include <memory>
# Include <algorithm>
# Define read freopen ("checker. In", "r", stdin)
# Define write freopen ("checker. Out", "W", stdout)
# Define RD ifstream cout ("checker. Out ");
# Define wt ifstream CIN ("checker. In ");
Int N, C [15], D1 [30], D2 [30], ANS [15], CNT;
Void print (){
Int I;
For (I = 1; I <n; I ++)
Printf ("% d", ANS [I]);
Printf ("% d \ n", ANS [I]);
}
Void DFS (int r ){
If (r = n + 1 ){
CNT ++;
If (CNT <4)
Print ();
Return;
}
For (INT I = 1; I <= N; I ++ ){
If (C [I] | D1 [R + I] | D2 [R-I + N]) continue;
C [I] = 1;
D1 [R + I] = 1;
D2 [R-I + N] = 1;
Ans [R] = I;
DFS (R + 1 );
C [I] = 0;
D1 [R + I] = 0;
D2 [R-I + N] = 0;
}
}
Int main (){
Read;
Write;
Scanf ("% d", & N );
Memset (C, 0, sizeof (c ));
Memset (D1, 0, sizeof (D1 ));
Memset (D2, 0, sizeof (D2 ));
CNT = 0;
DFS (1 );
Printf ("% d \ n", CNT );
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.