摘要:
快速幂View Code #include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define LL long longint m, h;LL power(LL a, LL b, LL m){ if (a == 0) return 0; if (b == 0) return 1; a %= m; LL x = a; LL ret = 1; for (; b > 0; b >>= 1, x = x * 阅读全文
posted @ 2011-09-01 16:06
undefined2024
阅读(273)
评论(0)
推荐(0)
摘要:
最短路View Code #include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;#define INF 0x3f3f3f3f#define N 105int path[N], vis[N];int cost[N][N];int lowcost[N];int n, a, b;void Dijkstra(int cost[][N], int lowcost[N], int n, int beg){ int i, j, mi 阅读全文
posted @ 2011-09-01 15:44
undefined2024
阅读(268)
评论(0)
推荐(0)
摘要:
题意:给定一个64位整数,问是否为质数,如果不是,则输出其最小因子。分析:经典题!!数学题miller_rabbin素数判定。若不是,则pollard_rho分解质因子,找到最小即可。Miller-rabinMiller-rabin算法是一个用来快速判断一个正整数是否为素数的算法。它利用了费马小定理,即:如果p是质数,且a,p互质,那么a^(p-1) mod p恒等于1。也就是对于所有小于p的正整数a来说都应该复合a^(p-1) mod p恒等于1。那么根据逆否命题,对于一个p,我们只要举出一个a(a#include #include #include #include constint S= 阅读全文
posted @ 2011-09-01 15:15
undefined2024
阅读(1895)
评论(0)
推荐(0)
摘要:
规律题题意:n个人站成一圈,从第一个人开始,每隔一个杀一个,问哪个人最后被杀。分析:虽然题目中认为这些人是站成一圈一杀到底,但是我们可以认为这些人是站成一排,每次从第二个活人开始,或者从第一个活人开始,从左到右每隔一个杀一个,直到只剩一个活人。如果我们每次给活人从1开始编号,那么每次从第二个杀,则每个活人的编号会变为(i - 1)/2+1,从第一个则变为i/2。最后剩一个编号为1的人。我们要做的就是记录每次从第几个开始杀,然后逆推,将编号为1的最后一个活人的编号按照刚才的规则,并根据每次从第一还是第二开始杀,逆推出其原编号。View Code #include <iostream> 阅读全文
posted @ 2011-09-01 10:42
undefined2024
阅读(256)
评论(0)
推荐(0)
摘要:
树状数组View Code #include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define maxn 30005struct Elem{ int a; int id;} elem[maxn];int n, m;int pos[maxn];int get[maxn];int ar[maxn];int lowb(int t){ return t & (-t) 阅读全文
posted @ 2011-09-01 09:31
undefined2024
阅读(219)
评论(0)
推荐(0)