PE 77, used dynamic programming
This commit is contained in:
60
projecteuler/077/main.cpp
Normal file
60
projecteuler/077/main.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int MAXN = 1000;
|
||||||
|
|
||||||
|
vector<int> genPrimes(int n){
|
||||||
|
vector<bool> primeBinary(n+1, true);
|
||||||
|
|
||||||
|
for(int i = 2; i <= n; ++i){
|
||||||
|
if(primeBinary[i]){
|
||||||
|
for(int j = 2 * i; j <= n; j += i){
|
||||||
|
primeBinary[j] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<int> result;
|
||||||
|
|
||||||
|
for(int i = 2; i <= n; ++i){
|
||||||
|
if(primeBinary[i]){
|
||||||
|
result.push_back(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
cout << "Hello this is Patrick" << endl;
|
||||||
|
auto start = chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
|
auto primes = genPrimes(MAXN);
|
||||||
|
set<int> primeSet(primes.begin(), primes.end());
|
||||||
|
|
||||||
|
vector<unsigned long long> solutions(MAXN + 1, 0);
|
||||||
|
solutions[0] = 1;
|
||||||
|
|
||||||
|
for(int i = 2; i <= MAXN; ++i){
|
||||||
|
if(primeSet.count(i)){
|
||||||
|
for(int pos = 0; pos <= MAXN - i; ++pos){
|
||||||
|
solutions[pos + i] += solutions[pos];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < solutions.size(); ++i){
|
||||||
|
// cout << solutions[i] << " ";
|
||||||
|
if(solutions[i] > 5000){
|
||||||
|
cout << i << endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
|
||||||
|
cout << (float)duration.count()/1000 << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user