Posted in ACM-ICPC, Dynamic Programming

CF 474D Flowers

/* 7/14/2018
* Problem: http://codeforces.com/problemset/problem/474/D
* Solution: Dp. dp[i] = dp[i-1] + dp[i-k]. One state can be transferred to from two sources: 1. eat only white flower from the i-k day 2. otherwise.
* Notes: 1. Remember to mod the MOD every time there’s a summing process.
2. The result of a mod can be negative.
* A little bit gibber-gabber:
For both the notes, I KNEW it but I now don’t..??
What is the point of everything..
*/

#include
using namespace std;

int main() {
	int t, k;
	cin >> t >> k;
	long long ind[100005] = {0}, sum[100005] = {0};
	int MOD = 1000000007;
	for (int i = 0; i < k; i++) {
		ind[i] = 1;
	}
	for (int i = k; i <= 100005; i++) {
		ind[i] = (ind[i - 1] + ind[i - k]) % MOD;
	}
	sum[0] = 1;
	for (int i = 0; i  a >> b;
		long long ans = (sum[b] - sum[a - 1]) % MOD;
		if (ans < 0) {
			ans += MOD;
		}
		cout << ans << endl;
	}
	return 0;
}


Posted in ACM-ICPC, Graph theory

CF 977E Cyclic Components

Date: 5/8/2018
Problem: http://codeforces.com/problemset/problem/977/E
Solution: According to the definition, if several vertices are a cycle, all the vertices must have a degree of two. So, check only the vertices that has a degree of two, and see how many are circles can be constructed.


#include
#include
using namespace std;

int deg[200005] = {0};
bool vst[200005];
vector e[200005];
vector cycle;

void dfs(int i) {
	vst[i] = true;
	cycle.push_back(i);
	for (vector::iterator it = e[i].begin(); it != e[i].end(); ++it) {
		if (!vst[*it]) {
			dfs(*it);
		}
	}
}

int main() {
	int n, m;
	int ans = 0;
	cin >> n >> m;
	for (int i = 1; i > a >> b;
		deg[a]++;
		deg[b]++;
		e[a].push_back(b);
		e[b].push_back(a);
	}	
	for (int i = 1; i <= n; i++) {
		if (!vst[i]) {
			cycle.clear();
			vst[i] = true;
			dfs(i);
			bool b = true;
			for (vector::iterator it = cycle.begin(); it != cycle.end(); ++it) {
				if (deg[*it] != 2) {
					b = false;
					break;
				}
			}
			if (b) {
				ans++;
			}
		}
	}
	cout << ans;
	return 0;
}

Posted in Dynamic Programming

CF 431C k-Tree

2/8/2018
Problem: http://codeforces.com/problemset/problem/431/C
Solution: DP. We can generalie the problem into, divided number n as the sum of a couple numbers less than k. All so, one of them must be greater or equal to d.
So, we construct a 2-D dp with 0 or 1 denoting whether there already is one number greater or equal to d as the 2nd dimension.


#include
using namespace std;

int main() {
    int n, k, d;
    cin >> n >> k >> d;
    long long dp[105][2] = {0};
    long long mod = 1e9 + 7;
    dp[0][0] = 1;
    dp[0][1] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= k; j++) {
            if (i < j) break;
            if (j < d) {
                dp[i][0] = (dp[i][0] + dp[i - j][0]) % mod;
                dp[i][1] = (dp[i][1] + dp[i - j][1]) % mod;
            } else {
                dp[i][1] = (dp[i][1] + dp[i - j][0] + dp[i - j][1]) % mod;
            }
        }
    }
    cout << dp[n][1];
    return 0;
}

Posted in ACM-ICPC, BFS, Kuangbin

POJ 3984/ kuangbin 1K/ maze problem

Problem: http://poj.org/problem?id=3984
Solution: BFS.

#include
#include
#include
#include
using namespace std;

int a[6][6] = {0};
int vis[6][6] = {0};
int dx[5] = {0, -1, 0, 1, 0};
int dy[5] = {0, 0, -1, 0, 1};
int prevx[6][6] = {0};
int prevy[6][6] = {0};

struct T {
	int x;
	int y;
	void init(int a, int b) {
		x = a;
		y = b;
	}
};

queue q;
stack ans;

void print(int x, int y) {
	if (x != 0 || y != 0) {
		T temp;
		temp.init(x, y);
		ans.push(temp);
		print(prevx[x][y], prevy[x][y]);
	}
}

void bfs(T temp) {
	int m = temp.x;
	int n = temp.y;
	if (m == 5 && n == 5) {
		print(m, n);
	} else {
		q.pop();
		for (int i = 1; i = 1 && m + dx[i] = 1 && n + dy[i] <= 5 && 
						a[m + dx[i]][n + dy[i]] == 0 && vis[m + dx[i]][n + dy[i]] == 0) {
				vis[m + dx[i]][n + dy[i]] = 1;
				T temp;
				temp.init(m + dx[i], n + dy[i]);
				prevx[m + dx[i]][n + dy[i]]	= m;
				prevy[m + dx[i]][n + dy[i]] = n;
				q.push(temp);
			}
		}
		bfs(q.front());
	}
}

int main () {
	for (int i = 1; i  a[i][j];
		}
	}
	T temp;
	temp.init(1, 1);
	prevx[temp.x][temp.y] = 0;
	prevy[temp.x][temp.y] = 0;
	vis[temp.x][temp.y] = 1;
	q.push(temp);
	bfs(temp);
	while(!ans.empty()) {
		cout << '(' << ans.top().x - 1 << ", " << ans.top().y - 1 << ')';
		ans.pop();
		if (!ans.empty()) {
			cout << endl;
		}
	}
	return 0;
}
Posted in BFS, Kuangbin

POJ 3126 Prime Path/ kuangbin 1F

12/23/2017
Problem: http://poj.org/problem?id=3126
Solution: BFS.

#include<iostream>
#include<queue>
#include<string.h> 
using namespace std;
int mp[10000];

struct node {
	int num, cnt;
	node(int numm, int cntt) {
		num = numm;
		cnt = cntt;
	}
};

queue<node> q;

bool prime(int n) {
	for (int i = 2; i * i <= n; i++) {
		if (n % i == 0) {
			return false;
		}
	}
	return true;
} 

void digit(node n) {
	int div = 0, rem = 0;
	int tmp = 1;	
	for (int i = 1; i <= 3; i++) {
		tmp *= 10;
		div = n.num / tmp;
		rem = n.num % (tmp / 10);
		for (int j = 0; j <= 9; j++) {
			int newNum = div * tmp + j * (tmp / 10) + rem;
			// cout << newNum << endl; 
			if (prime(newNum) && !mp[newNum]) {
				// cout << newNum << endl;
				q.push(node(newNum, n.cnt + 1)); 
				mp[newNum] = 1;
			} 
		}
	}
	rem = n.num % 1000;
	for (int j = 1; j <= 9; j++) {
		int newNum = j * 1000 + rem;
		if (prime(newNum) && !mp[newNum]) {
			// cout << newNum << endl; 
			q.push(node(newNum, n.cnt + 1));
			mp[newNum] = 1;
		}
	}
}

int main() {
	int n;
	cin >> n;
	while (n--) {
		int start = 0, end = 0;
		cin >> start >> end;
		memset(mp, 0, sizeof(mp));
		while (!q.empty()) {
			q.pop();
		}
		node head = node(start, 0);
		mp[start] = 1;
		q.push(head);
		while (!q.empty() && q.front().num != end) {
			digit(q.front());
			q.pop();
		}
		if (q.empty()) {
			cout << "Impossible" << endl; 
		} else {
			cout << q.front().cnt << endl;			
		}
	}
	return 0;
}
Posted in Graph theory

hdu 1272/ kuangbin 5-M(graph)

10/17/2017
Problem: http://acm.split.hdu.edu.cn/showproblem.php?pid=1272
Solution: Use the identity that for a tree, the number of nodes should be one
more than that of edges. Pay special attention to that there could be
zero node, but the answer should still be true.
Note: Another possible solution, using dsu, is posted here: https://prionanthaspace.wordpress.com/2017/10/18/hdu-1272-kuangbin-5-m/


#include<iostream>
#include<string.h>
using namespace std;

int flag[100005] = {0};

int main() {
	int a, b;
	int cntn = 0, cnte = 0;
	while(cin >> a >> b) {
		if (a == -1 && b == -1) {
			return 0;
		} else if (!a && !b) {
			bool ans = (cntn == cnte + 1) || cntn == 0; 
			cout << (ans? "Yes": "No") << endl;
			memset(flag, 0, sizeof(flag));
			cntn = 0;
			cnte = 0;
			continue;
		}
		cnte ++;
		if (!flag[a]) {
			flag[a] = 1;
			cntn ++;
		}
		if (!flag[b]) {
			flag[b] = 1;
			cntn ++;
		}
	}
	return 0;
} 

Posted in DSU

hdu 1272/ kuangbin 5-M(dsu)

10/17/2017
Problem: http://acm.split.hdu.edu.cn/showproblem.php?pid=1272
Solution: DSU. However, also need to check if all dots are connected. For that,
simply loop every dot that exists, and check if all their father are
the same.
Note: This problem would be much more easier if we consider the identity of a
tree, which is the number of dot is always one more than that of edges.
Link: https://prionanthaspace.wordpress.com/2017/10/18/hdu-1272-kuangbin-5-mgraph/


#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;

int parent[100005] = {0};
int flag[100005] = {0};

int find(int a) {
	if (parent[a] == a) {
		return a;
	} else {
		return parent[a] = find(parent[a]);
	}
}

void merge(int a, int b) {
	int p1 = find(a);
	int p2 = find(b);
	if (p1 != p2) {
		parent[p1] = p2;
	}
}

int main() {
	int a, b;
	bool ans = true;
	int maxx = 0;
	int minn = 100005;
	for (int i = 1; i <= 100005; i++) {
		parent[i] = i;
	}	
	memset(flag, 0, sizeof(flag));
	while (cin >> a >> b) {
		if (a == -1 && b == -1) {
			return 0;
		} else if (!a && !b) {
			if (ans) {
				int tmp = find(minn);
				for (int i = minn + 1; i <= maxx; i++) {
					if (flag[i] && find(i) != tmp) {
						// cout << tmp << ' ' << find(i) << endl;
						ans = false;
						break;
					}
				}
			}		
			cout << (ans? "Yes" :"No") << endl;
			maxx = 0;
			minn = 100005;			
			ans = true;
			for (int i = 1; i <= 100005; i++) {
				parent[i] = i;
			}
			memset(flag, 0, sizeof(flag));
			continue;
		}
		minn = min(minn, min(a, b));
		maxx = max(maxx, max(a, b));
		flag[a] = 1;
		flag[b] = 1;
		if (find(a) != find(b)) {
			merge(a, b);
		} else {
			ans = false;
		}
	}
	return 0;
}

Posted in DSU

HDU 1213/ kuangbin 5-C

10/16/2017
Problem: http://acm.split.hdu.edu.cn/showproblem.php?pid=1213


#include<iostream>
#include<string.h>
using namespace std;

int parent[1005] = {0};
int cnt = 0;

int find(int a) {
	if (parent[a] == a) {
		return a;
	} else {
		return parent[a] = find(parent[a]);
	}
}

void merge(int a, int b) {
	int p1 = find(a);
	int p2 = find(b);
	if (p1 != p2) {
		parent[p1] = p2;
		cnt --;
	}
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		cnt =0;
		int n, m;
		cin >> n >> m;
		cnt = n;
		for (int i = 1; i <= n; i++) {
			parent[i] = i;
		}
		for (int i = 1; i <= m; i++) {
			int a, b;
			cin >> a >> b;
			if (find(a) != find(b)) {
				merge(a, b);
			}
		}
		cout << cnt << endl;
	}
	return 0;
} 

Posted in BFS, Kuangbin

POJ 1426/ kuangbin 1E

10/16/2017
Problem: http://poj.org/problem?id=1426
Solution: BFS. Manage a queue, and for each time push current number + 0 or 1 in
the end all the way untill curr is a multiple of given n. For some
mysterious reason limited to long long…


#include<iostream>
#include<queue>
using namespace std;

int main() {
	long long n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		long long ans = 0;
		queue<long long> q;
		q.push(1);
		while (!q.empty()) {
			long long cur = q.front();
			q.pop();
			if (cur % n == 0) {
				cout << cur << endl;
				break;
			} else {
				q.push(cur * 10);
				q.push(cur * 10 + 1);
			}
		}
	}
	return 0;
} 

Posted in BFS, Kuangbin

POJ 3278/ kuangbin 1C

10/13/2017
Problem: http://poj.org/problem?id=3278
Solution: For every element x in the queue, add in x-1. x+1, x*2.


#include<iostream>
#include<queue>
using namespace std;

bool v[100005];

bool inrange(int x) {
	return (x >= 0) && (x <= 100000); 
}

struct node{
	int t;
	int x;
	node(int tt, int xx) {
		t = tt;
		x = xx;
	}
};

int main() {
	int n, k;
	cin >> n >> k;
	queue<node> q;
	q.push(node(0, n));
	v[n] = true;
	while(!q.empty()) {
		node curr = q.front();
		q.pop();
		if (curr.x == k) {
			cout << curr.t;
			return 0;
		}
		if (inrange(curr.x - 1) && !v[curr.x - 1]) {
			q.push(node(curr.t + 1, curr.x - 1));
			v[curr.x - 1] = true;
		} 
		if (inrange(curr.x + 1) && !v[curr.x + 1]) {
			q.push(node(curr.t + 1, curr.x + 1));
			v[curr.x + 1] = true;
		}
		if (inrange(curr.x * 2) && !v[curr.x * 2]) {
			q.push(node(curr.t + 1, curr.x * 2));
			v[curr.x * 2] = true;
		}
	}
	return 0;
}