This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* https://github.com/trhgquan | |
*/ | |
#include <stdio.h> | |
int PRIME[2000004]; | |
void generatePrimes() { | |
for (int i = 2; i <= 2000004; ++i) | |
PRIME[i] = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* https://www.spoj.com/PTIT/problems/P166PROH/ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include <stdio.h> | |
int PRIME[4000]; | |
// Because maximum n is 3000, so prime list limit is bigger than 3000. | |
void generatePrimes() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* CDRSANJ - https://www.spoj.com/problems/CDRSANJ/ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include <stdio.h> | |
int f(int x) { | |
if (x == 0 || x == 1 || x == 2) | |
return x; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* https://www.spoj.com/problems/NUB04/ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include <iostream> | |
#define lli long long int | |
bool isPrime(lli a) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int max(int a, int b) { | |
return (a > b) ? a : b; | |
} | |
int MaximumNonAdj(vector<int> adj) { | |
int ret = INT_MIN; | |
adj.insert(adj.begin(), 0); | |
vector<int> F(adj.size(), 0); | |
F[1] = max(adj[1], 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def maximumNonAdj(arr): | |
arr.insert(0, 0) | |
brr = [0] * len(arr) | |
brr[1] = arr[1] | |
m = -100000000 | |
for i in range(1, len(arr)): | |
brr[i] = max(arr[i] + brr[i - 2], brr[i - 1]) | |
if (brr[i] > m): m = brr[i] | |
return m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* longestArrays.cpp - Dynamic Programming approach. | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
int longestArrays(vector<int> a) { | |
vector<int> b(a.size(), 1); | |
int res = 1; | |
for (unsigned i = 1; i < a.size(); ++i){ | |
if (a[i] >= a[i - 1]) b[i] = b[i - 1] + 1; | |
res = max(b[i], res); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int root(int n) { | |
if (n >= 10) { | |
int s = 0; | |
while (n > 0) { | |
s += n % 10; | |
n /= 10; | |
} | |
return root(s); | |
} | |
return n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* SMPAIR - https://codechef.com/problems/SMPAIR | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<algorithm> | |
#include<vector> | |
using namespace std; | |
int main() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* [Tutorial] PAIRS1 - Count the pairs | |
* https://www.spoj.com/problems/PAIRS1/ | |
* Code by @trhquan - https://github.com/trhgquan | |
* Solution: sorting + binary search | |
*/ | |
#include<iostream> | |
#include<algorithm> | |
#include<vector> | |
using namespace std; |
OlderNewer