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
using namespace std; | |
using ll = long long; | |
class StrongPrimePower { | |
public: | |
bool isPrime(int p){ | |
for(int i=2;i*i<=p;++i) if(p%i==0) return false; | |
return true; | |
} | |
vector<int> baseAndExponent(string sn) { |
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
using ll = long long; | |
const size_t bssize = size_t(1e9); | |
typedef bitset<bssize> bs_t; | |
ll sz; | |
class StrongPrimePower { | |
public: | |
set<int> S; | |
bs_t *P = new bs_t();//better have smart ptr to deal with resource recycle |
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
class SmoothNumbers { | |
public: | |
//sieve | |
int countSmoothNumbers(int N, int k) { | |
vector<bool> isPrime(N+1, true); | |
vector<int> largestPF(N+1, 1); | |
long long i, j; //LL | |
for(i=2;i<=N;++i){ | |
if(isPrime[i]){ | |
largestPF[i]=i; |
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
vector<int> S; | |
int dp[1005][1005];//minimum difference | |
int inf = 0x3f3f3f3f; | |
//careful the base case | |
int solve(int p, int n){ | |
if(p==0) return 0; | |
if(n==1) return p==1 ? S[1]-S[0] : inf; | |
if(n<1) return inf; | |
int &ans = dp[p][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
class CandidateKeys { | |
public: | |
int NumberOfSetBits(int i){ | |
i = i - ((i >> 1) & 0x55555555); | |
i = (i & 0x33333333) + ((i >> 2) & 0x33333333); | |
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; | |
} | |
vector<int> getKeys(vector<string> table) { | |
vector<int> ans; |
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
// 一開始每個元素都是可能的答案 low和high分別指向頭尾有可能是答案的位置 | |
// (所以如果給定的長度是n, high是指向n-1的位置) | |
// 想想,如果mid不是我們要的答案,不論是low或high,指標都應該往前往後移一格 | |
// 因為一開始說了,"low和high分別指向頭尾有可能是答案的位置" | |
// 最後想想如果low, high現在相鄰 下一個迭代只有兩種可能 | |
// 1.找到 2.兩個指標現在指向同一個index | |
// 第二種情形的話,我們繼續往比較,這個最後一個還沒被查過的元素是不是我們要找的答案 | |
// 2.找到 2.low超過high了 |
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
class PalindromicNumbers { | |
public: | |
int countPalNums(int lower, int upper) { | |
string s,t; | |
int cnt=0; | |
for(int even=1;even<10000;even++){ | |
t = s = to_string(even); | |
reverse(s.begin(), s.end()); | |
if(lower<=stoi(t+s) && stoi(t+s)<=upper) cnt++; | |
} |
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 solve(int p, int take, int num){ | |
int &ret = dp[p][take][num]; | |
if(p==n){ | |
if(take>0 and num==0) return ret = 1; | |
return ret = 0; | |
} | |
if(ret!=-1) return ret; | |
int t = (num*10)+(s[p]-'0'); | |
//take digit at position p or not | |
int a = solve(p+1, take+1, t%8); |
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
#include <string> | |
#include <cstring> | |
#include <iostream> | |
using namespace std; | |
string s; | |
int n; | |
int dp[105][105][9]; | |
int solve(int p, int take, int num){ | |
if(p==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
#include <string> | |
#include <iostream> | |
using namespace std; | |
string s; | |
int n; | |
int dp[100][8]; | |
int solve(int p, int num){ | |
if(p==n){ | |
return dp[p][num] = (num==0); | |
} |