Skip to content

Instantly share code, notes, and snippets.

View balamark's full-sized avatar
🎯
Focusing

Mark balamark

🎯
Focusing
View GitHub Profile
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) {
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
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;
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];
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;
@balamark
balamark / b.cc
Created October 8, 2015 03:32
practice of programming
// 一開始每個元素都是可能的答案 low和high分別指向頭尾有可能是答案的位置
// (所以如果給定的長度是n, high是指向n-1的位置)
// 想想,如果mid不是我們要的答案,不論是low或high,指標都應該往前往後移一格
// 因為一開始說了,"low和high分別指向頭尾有可能是答案的位置"
// 最後想想如果low, high現在相鄰 下一個迭代只有兩種可能
// 1.找到 2.兩個指標現在指向同一個index
// 第二種情形的話,我們繼續往比較,這個最後一個還沒被查過的元素是不是我們要找的答案
// 2.找到 2.low超過high了
@balamark
balamark / _srm317.cc
Last active September 12, 2015 17:39
editorial version
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++;
}
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);
@balamark
balamark / 306c.cc
Created June 11, 2015 00:20
correct
#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){
@balamark
balamark / 306c.cc
Created June 10, 2015 23:54
think about it, how can we differentiate the case which takes zero digit with the valid case which take at least one digit?
#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);
}