随笔分类 -  其他

摘要:线性筛选法之所以称之为线性就是因为其对于每一个合数只用其最小的素因子将其筛选出来。代码如下:#include <cstdlib>#include <cstdio>#include <algorithm>#define MAXN 1000000using namespace std;int p[1000005], pri[1000005], idx = -1;void GetPrime(){ for (int i = 2; i <= MAXN; ++i) { if (!p[i]) { // 说明i是一个素数 pri[++idx] = i; ... 阅读全文
posted @ 2012-08-06 17:27 沐阳 阅读(629) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2012-08-06 08:15 沐阳 阅读(266) 评论(0) 推荐(0)
摘要:这题就是一个数据量大点的a+b+c+d+e = 0的判定,可以用哈希表来做,也可以用来个有序表来寻找解。时间复杂度前者接近O(n^3),后者则为O(N^3).代码如下:哈希表:#include<iostream>#include<map>#define MOD 1000003LLusing namespace std;typedef long long LL;const int N = 205;LL a[6][N];int head[1000005], idx;struct Node{ LL v; int next; }e[40005];void add(LL key) 阅读全文
posted @ 2012-08-02 21:07 沐阳 阅读(389) 评论(0) 推荐(0)
摘要:直接维护好两个数组就可以了,每次访问得到区间的最小值和最大值。代码如下:#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int N, M, seq[50005], Min[50005][20], Max[50005][20];void Minbuild(){ int LIM = (int)log2(double(N)); for (int i = 1; i <= 阅读全文
posted @ 2012-08-01 22:26 沐阳 阅读(213) 评论(0) 推荐(0)
摘要:最后才明白题目的意思可以直接抽象为对于每一位,所有数字的0个数乘以1的个数,再将每一位的数恢复过来。自己的做法也是枚举每一个二进制位,对于已经求了 ai,ai+1...aN-1,aN个数的二进制位之后再用ai-1的这一位的情况来判定这一位会增加多少个1。代码如下:#include <cstring>#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;typedef long long int Int64;// 1000000 这个数字只有最多19位i 阅读全文
posted @ 2012-08-01 09:25 沐阳 阅读(271) 评论(0) 推荐(0)
摘要:这题不要去考虑每一个时刻的情况,而要考虑每一个位在所有时间上的叠加效果。对于长度为Lf的母串和长度为Ls的子串来说,每个字符匹配的长度都是Lf-Ls+1位。代码如下:#include <cstring>#include <cstdio>#include <cstdlib>using namespace std;char f[2000005], s[2000005];int lens, lenf, cnt[30];int main(){ int T, LIM; long long int ans; scanf("%d", &T); 阅读全文
posted @ 2012-08-01 09:21 沐阳 阅读(206) 评论(0) 推荐(0)
摘要:一个网格中的图形,如果其面积只取方格的整数倍的时候,那么我们有如下公式。设面积为Area,多边形内部的整点个数为OnEdge,多边形轮廓线上的整点个数为InSide,那么有公式 Area = OnEdge / 2 + InSide - 1代码如下:#include <cstdlib>#include <cstdio>#include <cstring>using namespace std;int main(){ int T, N; long long int ret, S; scanf("%d", &T); while (T-- 阅读全文
posted @ 2012-08-01 09:00 沐阳 阅读(274) 评论(0) 推荐(0)
摘要:该题题义是给定如下一个方程组:F(1) = C1 (mod) PF(2) = C2 (mod) PF(3) = C3 (mod) P ...其中F(1) = A(1,1)*x1 + A(1, 2)*x2 + A(1, 3)*x3...其中A(i, j) = i ^ (j-1).面对这样一个方程,我们的做法就是先不管方程右端的(mod)P,因为F(i) 和 Ci 是同余的,那么在方程两端乘以一个数是没有影响的,代入某一个方程同样是允许的。于是剩下的就是高斯消元。由于解是唯一的,因此解出来的系数矩阵一定是满秩。接下来就可一用扩展gcd求解未知元了。代码如下:#include <cstdlib 阅读全文
posted @ 2012-07-27 12:11 沐阳 阅读(426) 评论(0) 推荐(0)
摘要:该题具有一定的技巧性,需要在Nlog(N)的时间复杂度下计算出任意一个点,N-1个点到其的距离综和,这里需要运用这样一个技巧,将x,y分开计算,首先计算x轴的距离,那么就先排一次序,然后有到P号点的距离和为 (P-1) * Xp - sum(X1...Xp-1) + sum(Xp+1...Xn) - (N-P) * xp; 同理可计算出y轴的距离,这两个距离是累加到一个结构体上的。所以最后直接找最小值即可。代码如下:#include <cstdlib>#include <cstring>#include <cstdio>#include <cmath& 阅读全文
posted @ 2012-07-27 01:10 沐阳 阅读(1162) 评论(0) 推荐(0)
摘要:代码如下:#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#define TO(x, y) (x-1)*N+yusing namespace std;char G[30][30];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}, N;int C;inline bool judge(int x, int y){ if (x < 1 || x > N || y < 1 || y > N) { 阅读全文
posted @ 2012-07-26 22:56 沐阳 阅读(320) 评论(0) 推荐(0)
摘要:这题我们可以参考开关那题,只不过这里是求最少的操作次数,那么我们需要对变元进行枚举,算出所有的情况下,最少需要改变的次数。代码如下:#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#define TO(x, y) (x-1)*4+yusing namespace std;char G[6][6];int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};inline bool judge(int x, int y){ if 阅读全文
posted @ 2012-07-26 22:06 沐阳 阅读(404) 评论(0) 推荐(0)
摘要:简单的快速矩阵幂:Fn 1 1 Fn-1 = * Fn-1 1 0 Fn-2把前面的矩阵作快速幂就可以了。代码如下:#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>#define MOD 10000using namespace std;struct Matrix // ['meitriks]{ int a[2][2]; void New (int x, int y, int z, int w) { a[0][0] ... 阅读全文
posted @ 2012-07-25 10:05 沐阳 阅读(250) 评论(0) 推荐(0)
摘要:这题直接二分就可以了,注意下二分的返回值,以后都最好手动模拟一下。代码如下:#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long int Int64;Int64 N, M, seq[1000005];Int64 sum(Int64 h){ Int64 ret = 0; for (int i = 1; i <= N; ++i) { if (seq[i] > h) { re 阅读全文
posted @ 2012-07-24 00:31 沐阳 阅读(296) 评论(0) 推荐(0)
摘要:代码如下:#include <cstdlib>#include <cstring>#include <cmath>#include <cstdio>using namespace std;int p[40010], a[10000], b[10000];void pre(){ for (int i = 4; i <= 40010; i += 2) { p[i] = 1; } for (int i = 3; i <= 200; i += 2) { if (!p[i]) { int k = 2 * i; fo... 阅读全文
posted @ 2012-07-14 00:33 沐阳 阅读(238) 评论(0) 推荐(0)
摘要:这题用map就超时了,所以用字典树来优化,第一次写静态的,现在都不习惯用指针了。由于这里不要回到源点,所以不许要所有点的度都为偶数,零个或者两个均可,图也必须是连通的。代码如下:#include <cstring>#include <cstdlib>#include <cstdio>#include <string>using namespace std;char s1[15], s2[15];int idx = 0, flag = 0, ptr = 1;int set[2500005];struct Node{ int cnt, No; int 阅读全文
posted @ 2012-07-12 15:00 沐阳 阅读(238) 评论(0) 推荐(0)
摘要:这题树状数组加离散化TLE,归并排序却过了。代码如下:#include <cstdlib>#include <cstdio>#include <cstring>#include <iostream>#define MAXN 500005using namespace std;int N, a[MAXN], c[MAXN];long long ans;void merge_sort(int l, int r){ if (r > l) { int mid = (l+r) >> 1; merge_sort(l, mid); merge 阅读全文
posted @ 2012-07-11 01:38 沐阳 阅读(269) 评论(0) 推荐(0)
摘要:直接暴力代码如下:#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#include <map>#include <string>#include <iostream>using namespace std;int N;string ans;map<string, int>mp[15]; map<string, int> :: iterator it;int main(){ int T; ch 阅读全文
posted @ 2012-07-10 01:26 沐阳 阅读(246) 评论(0) 推荐(0)
摘要:直接暴力。代码如下:#include <cstring>#include <cstdio>#include <cstdlib>#include <map>#include <string>using namespace std;char word[10005][55], c[55];map<string, int>mp;bool Del(int x){ int length = strlen(c), sum = 0; if (length != strlen(word[x])+1) { return false; } fo 阅读全文
posted @ 2012-07-10 00:17 沐阳 阅读(199) 评论(0) 推荐(0)
摘要:该题是在给定一系列的计算逻辑的基础上,询问一个表达式是否恒为真的问题,由于构成的表达式是一个以运算符开始的先序遍历的式子,所以可以利用递归进行求解。什么是构造法呢? 1.对所讨论的对象能进行较为直观的描述; 2.实现的具体性,就是不只是判明某种解的存在性,而且要实现具体求解。代码如下:#include <cstring>#include <cstdio>#include <cstdlib>using namespace std;char s[105];int K[2][2] = {0, 0, 0, 1}, A[2][2] = {0, 1, 1, 1};int 阅读全文
posted @ 2012-06-29 16:10 沐阳 阅读(303) 评论(0) 推荐(0)
摘要:该题算是一个暴力大表题了,给定了最多24根火柴棍,能够构成最大的数就是9992了,直接暴力。代码如下:#include <cstdlib>#include <cstdio>#include <cstring>#include <map>#include <algorithm>using namespace std;int N, make[10] = {6,2,5,5,4,5,6,3,7,6}, A, B, C, cnt; int ans[30] = {0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,8,9,6,9,29,3 阅读全文
posted @ 2012-06-06 22:13 沐阳 阅读(238) 评论(0) 推荐(0)