上一页 1 ··· 18 19 20 21 22 23 24 25 26 ··· 43 下一页
摘要: 使用异或和与,模拟机器的加法。http://blog.csdn.net/htyurencaotang/article/details/11125415#include #include using namespace std; void add(int &sum, int &carry){ int a = sum ^ carry; int b = (sum & carry) > x >> y) { while (y != 0) add(x, y); cout << x << endl; } return 0;} 阅读全文
posted @ 2013-11-02 11:08 阿牧遥 阅读(227) 评论(0) 推荐(0)
摘要: 推导一下,就是斐波那契数列那样的。但是要注意的是,int存不下,算一下需要long long才行,因为是指数级上升的。#include #define LEN 75#define ulong unsigned long longusing namespace std; ulong F[LEN]; int main(){ F[0] = 1; F[1] = 1; for (int i = 2; i <= 70; i++) { F[i] = F[i-1] + F[i-2]; } int n; while(scanf("%d", &n)!=EO... 阅读全文
posted @ 2013-11-01 23:17 阿牧遥 阅读(187) 评论(0) 推荐(0)
摘要: 做法是n&(n-1)。据说还有变态的查表法:http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html。最后,居然必须用scanf/printf。#include #include using namespace std; int countOne(int x){ int cnt = 0; while (x != 0) { x &= x - 1; cnt++; } return cnt;} int main(){ int n; scanf("%d", &n)... 阅读全文
posted @ 2013-10-31 23:58 阿牧遥 阅读(169) 评论(0) 推荐(0)
摘要: 由于思维的惯性,用了queue。后来发现一要注意要用集合判重,二是每次往queue里放的多,后来溢出了,要用long long。但这样要用数组,集合,队列,内存多。效率是O(n*logn)的。#include #include #include #include #include #include #include #define LEN 1505#define ulong unsigned long longusing namespace std; ulong dp[LEN]; int main(){ priority_queue, greater > que; set visi... 阅读全文
posted @ 2013-10-31 23:05 阿牧遥 阅读(198) 评论(0) 推荐(0)
摘要: Skip list(跳跃表)是一种可以代替平衡树的数据结构。Skip lists应用概率保证平衡,平衡树采用严格的旋转(比如平衡二叉树有左旋右旋)来保证平衡,因此Skip list比较容易实现,而且相比平衡树有着较高的运行效率。目前开源软件 Redis 和 LevelDB 都有用到它,它的效率和红黑树以及 AVL 树不相上下,但跳表的原理相当简单,只要你能熟练操作链表,就能轻松实现一个 SkipList。http://www.cnblogs.com/davad/articles/3049086.html空间复杂度: O(n) (期望)跳跃表高度: O(log n) (期望)相关操作的时... 阅读全文
posted @ 2013-10-31 11:51 阿牧遥 阅读(485) 评论(0) 推荐(0)
摘要: 树形DP。用F[k][0]和F[k][1]表示某节点不选和选了之后子树的最大值。那么:f[i][0]=sigma(max(f[k][0],f[k][1]))f[i][1]=sigma(f[k][0])+v[i]解题中用了备忘录。一开始要先找树根。#include #include #include #include #include #define MAX(a, b) a>b?a:b#define LEN 6005using namespace std;int F[LEN][2]; // dp state, F[i][0] for i not selected, F[i][1] for 阅读全文
posted @ 2013-10-30 22:55 阿牧遥 阅读(337) 评论(0) 推荐(0)
摘要: 典型BFS。#include #include #include #include #define LEN 5using namespace std;int graph[LEN][LEN];int ans = 0;int dx[4] = {-1, 1, 0, 0};int dy[4] = {0, 0, -1, 1};void printAns(map &visit, int p) { if (p == -1) return; int prev = visit[p]; printAns(visit, prev); int x = p / 5; int y = p -... 阅读全文
posted @ 2013-10-29 23:44 阿牧遥 阅读(175) 评论(0) 推荐(0)
摘要: http://wikioi.com/problem/1116/典型的DFS。#include #include #define LEN 8using namespace std;int graph[LEN][LEN];int color[LEN]; // 1,2,3,4 for 4 colorsint ans = 0;int n = 0;void dfs(int step) { if (step == n) { ans++; return; } for (int x = 1; x > n; for (int i = 0; i > graph[... 阅读全文
posted @ 2013-10-29 22:58 阿牧遥 阅读(200) 评论(0) 推荐(0)
摘要: http://wikioi.com/problem/1079/单源最短路径,可以用dijkstra来做。这里采用了heap优化,复杂度是(V+E)logV。这里用了STL的优先队列(堆),重复加入pair没有问题,因为dist小的会先出来。为了避免重复扩展,用了visit判重,这个也不是必须的。注意的是:1.pair使用的时候,把距离放在first的位置,那么在priority queue里,距离小的会先出来。2.priority_queue, greater > que;这样定义,小的先出来。3.使用graph[from][to] ?来判断是否用from=>to这条路。#incl 阅读全文
posted @ 2013-10-27 17:54 阿牧遥 阅读(220) 评论(0) 推荐(0)
摘要: http://wikioi.com/problem/1231/Kruskal+并查集。comp函数里面如果用const引用的话,可以减少copy。并查集find的时候是递归找父亲的根。无他。#include #include #include #include #include #include #define MAX(a, b) a>b?a:b#define LEN 100005using namespace std;struct Edge { int a; int b; int val;};int n, m;Edge edges[LEN];int father[LEN];... 阅读全文
posted @ 2013-10-24 23:12 阿牧遥 阅读(210) 评论(0) 推荐(0)
上一页 1 ··· 18 19 20 21 22 23 24 25 26 ··· 43 下一页