Number Triangles:
/*
DP 题
dp[j] = max(dp[j - 1] + tgl[i-1][j], dp[j] + tgl[i-1][j]);
*/
1 #define _CRT_SECURE_NO_WARNINGS 2 #define HOME 3 #define _CRTDBG_MAP_ALLOC 4 #include <crtdbg.h> 5 6 /* 7 ID: Jming 8 PROG: numtri 9 LANG: C++ 10 */ 11 #include <iostream> 12 #include <fstream> 13 #include <sstream> 14 #include <cstdlib> 15 #include <cstdio> 16 #include <cstddef> 17 #include <iterator> 18 #include <algorithm> 19 #include <string> 20 #include <locale> 21 #include <cmath> 22 #include <vector> 23 #include <cstring> 24 #include <map> 25 #include <utility> 26 #include <queue> 27 #include <stack> 28 #include <set> 29 #include <functional> 30 using namespace std; 31 typedef pair<int, int> PII; 32 typedef long long int64; 33 const int INF = 0x3f3f3f3f; 34 const int modPrime = 3046721; 35 const double eps = 1e-9; 36 const int MaxN = 510000; 37 const int MaxM = 10010; 38 39 40 int N; 41 int dp[MaxN]; 42 vector<vector<int> > tgl; 43 44 45 void Solve() 46 { 47 for (int i = 1; i <= N; ++i) 48 { 49 for (int j = i; j >= 1; --j) 50 { 51 dp[j] = max(dp[j - 1] + tgl[i-1][j], dp[j] + tgl[i-1][j]); 52 } 53 } 54 cout << *max_element(dp, dp + MaxN) << endl; 55 } 56 57 int main() 58 { 59 #ifdef HOME 60 freopen("in", "r", stdin); 61 //freopen("out", "w", stdout); 62 #endif 63 64 freopen("numtri.in", "r", stdin); 65 freopen("numtri.out", "w", stdout); 66 67 fill(dp, dp + MaxN, 0); 68 scanf("%d", &N); 69 for (int i = 1; i <= N; ++i) 70 { 71 vector<int> row; 72 row.push_back(0); 73 int tmp; 74 for (int j = 0; j < i; ++j) 75 { 76 scanf("%d", &tmp); 77 row.push_back(tmp); 78 } 79 tgl.push_back(row); 80 } 81 82 Solve(); 83 84 #ifdef HOME 85 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 86 _CrtDumpMemoryLeaks(); 87 #endif 88 return 0; 89 }
Prime Palindromes:
/*
生成回文数 + 判断是否为质数
*/
解法一:
1 /* 2 ID: Jming 3 PROG: pprime 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 100000010; 32 const int MaxM = 10010; 33 34 35 vector<int> legal; 36 37 38 // 判断是否为质数 39 bool isPrime(int x) 40 { 41 for (int i = 2; i*i <= x; ++i) 42 { 43 if (x%i == 0) 44 { 45 return false; 46 } 47 } 48 return true; 49 } 50 51 // 保存答案 52 void getAns(int x) 53 { 54 if (isPrime(x)) 55 { 56 legal.push_back(x); 57 } 58 } 59 60 // 生成回文数 61 void getPdm() 62 { 63 int palindrome; 64 65 for (int i1 = 1; i1 <= 9; i1 += 2) 66 { 67 // 1 68 palindrome = i1; 69 getAns(palindrome); 70 // 2 71 palindrome = i1 * 10 + i1; 72 getAns(palindrome); 73 } 74 75 for (int i1 = 1; i1 <= 9; i1 += 2) 76 { 77 for (int i2 = 0; i2 <= 9; ++i2) 78 { 79 // 3 80 palindrome = i1 * 100 + i2 * 10 + i1; 81 getAns(palindrome); 82 // 4 83 palindrome = i1 * 1000 + i2 * 100 + i2 * 10 + i1; 84 getAns(palindrome); 85 } 86 } 87 88 for (int i1 = 1; i1 <= 9; i1 += 2) 89 { 90 for (int i2 = 0; i2 <= 9; ++i2) 91 { 92 for (int i3 = 0; i3 <= 9; ++i3) 93 { 94 // 5 95 palindrome = 96 i1 * 10000 + i2 * 1000 + i3 * 100 + 97 i2 * 10 + i1; 98 getAns(palindrome); 99 // 6 100 palindrome = 101 i1 * 100000 + i2 * 10000 + i3 * 1000 + 102 i3 * 100 + i2 * 10 + i1; 103 getAns(palindrome); 104 } 105 } 106 } 107 108 for (int i1 = 1; i1 <= 9; i1 += 2) 109 { 110 for (int i2 = 0; i2 <= 9; ++i2) 111 { 112 for (int i3 = 0; i3 <= 9; ++i3) 113 { 114 for (int i4 = 0; i4 <= 9; ++i4) 115 { 116 // 7 117 palindrome = 118 i1 * 1000000 + i2 * 100000 + i3 * 10000 + i4 * 1000 + 119 i3 * 100 + i2 * 10 + i1; 120 getAns(palindrome); 121 // 8 122 palindrome = 123 i1 * 10000000 + i2 * 1000000 + i3 * 100000 + i4 * 10000 + 124 i4 * 1000 + i3 * 100 + i2 * 10 + i1; 125 getAns(palindrome); 126 } 127 } 128 } 129 } 130 } 131 132 133 134 int main() 135 { 136 #ifdef HOME 137 freopen("in", "r", stdin); 138 //freopen("out", "w", stdout); 139 #endif 140 141 freopen("pprime.in", "r", stdin); 142 freopen("pprime.out", "w", stdout); 143 144 145 146 getPdm(); 147 sort(legal.begin(), legal.end()); 148 int a, b; 149 scanf("%d %d", &a, &b); 150 int i = 0; 151 while (legal[i] < a) ++i; 152 while ((i<legal.size()) && legal[i] <= b) 153 { 154 printf("%d\n", legal[i]); 155 ++i; 156 } 157 158 159 160 #ifdef HOME 161 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 162 _CrtDumpMemoryLeaks(); 163 #endif 164 return 0; 165 }
解法二(优化):
1 /* 2 生成回文数 + 判断是否为质数 3 备注: 4 sprintf:整数转换为字符串 5 atoi:字符串转换为int 6 */ 7 /* 8 ID: Jming 9 PROG: pprime 10 LANG: C++ 11 */ 12 #include <iostream> 13 #include <fstream> 14 #include <sstream> 15 #include <cstdlib> 16 #include <cstdio> 17 #include <cstddef> 18 #include <iterator> 19 #include <algorithm> 20 #include <string> 21 #include <locale> 22 #include <cmath> 23 #include <vector> 24 #include <cstring> 25 #include <map> 26 #include <utility> 27 #include <queue> 28 #include <stack> 29 #include <set> 30 #include <functional> 31 using namespace std; 32 typedef pair<int, int> PII; 33 typedef long long int64; 34 const int INF = 0x3f3f3f3f; 35 const int modPrime = 3046721; 36 const double eps = 1e-9; 37 const int MaxN = 10010; 38 const int MaxM = 10010; 39 40 41 int A, B; 42 43 // 判断是否为质数 44 bool isPrime(int x) 45 { 46 if (x < 2) 47 { 48 return false; 49 } 50 for (int i = 2; i*i <= x; ++i) 51 { 52 if (x%i == 0) 53 { 54 return false; 55 } 56 } 57 return true; 58 } 59 60 void gen(int x, int isOdd) 61 { 62 char buf[10]; 63 64 sprintf(buf, "%d", x); 65 66 char *p, *q; 67 68 p = buf + strlen(buf); 69 70 q = p - isOdd; 71 72 while (q > buf) 73 { 74 *(p++) = *(--q); 75 } 76 77 *p = '\0'; 78 79 int n = atoi(buf); 80 81 if ((n >= A) && (n <= B) && (isPrime(n))) 82 { 83 printf("%d\n", n); 84 } 85 } 86 87 88 89 void getPdm(int start, int end) 90 { 91 for (int i = start; i <= end; ++i) 92 { 93 // 生成位数是奇数的回文数 94 gen(i, 1); 95 } 96 for (int i = start; i <= end; ++i) 97 { 98 // 生成位数是偶数的回文数 99 gen(i, 0); 100 } 101 } 102 103 void Solve() 104 { 105 getPdm(1, 9); 106 getPdm(10, 99); 107 getPdm(100, 999); 108 getPdm(1000, 9999); 109 } 110 111 int main() 112 { 113 #ifdef HOME 114 freopen("in", "r", stdin); 115 //freopen("out", "w", stdout); 116 #endif 117 118 freopen("pprime.in", "r", stdin); 119 freopen("pprime.out", "w", stdout); 120 121 scanf("%d %d", &A, &B); 122 Solve(); 123 124 125 #ifdef HOME 126 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 127 _CrtDumpMemoryLeaks(); 128 #endif 129 return 0; 130 }
Superprime Rib:
/*
此题可利用 “当前生成位数为n的数是否为质数” 进行剪枝
递归,详见代码
*/
1 /* 2 ID: Jming 3 PROG: sprime 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 10010; 32 const int MaxM = 10010; 33 34 int N; 35 36 bool isPrime(int x) 37 { 38 if (x < 2) 39 { 40 return false; 41 } 42 for (int i = 2; i*i <= x; ++i) 43 { 44 if (x%i == 0) 45 { 46 return false; 47 } 48 } 49 return true; 50 } 51 52 53 void Solve(int val, int n) 54 { 55 if (n == N) 56 { 57 printf("%d\n", val); 58 return; 59 } 60 for (int i = 1; i <= 9; ++i) 61 { 62 int tmp = val * 10 + i; 63 // 剪枝 64 if (isPrime(tmp)) 65 { 66 Solve(tmp, n + 1); 67 } 68 } 69 } 70 71 int main() 72 { 73 #ifdef HOME 74 //freopen("in", "r", stdin); 75 //freopen("out", "w", stdout); 76 #endif 77 78 freopen("sprime.in", "r", stdin); 79 freopen("sprime.out", "w", stdout); 80 81 while (~scanf("%d", &N)) 82 { 83 Solve(0, 0); 84 } 85 86 #ifdef HOME 87 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 88 _CrtDumpMemoryLeaks(); 89 #endif 90 return 0; 91 }