算法
算法刷题
trq1995
这个作者很懒,什么都没留下…
展开
-
AC自动机 病毒侵袭 hdu2896
和hdu2222题相似的水题提示:1)连着RE了好多发,没想明白,看了一下网上题解才知道,输入的不一定都是字母,所以next要开100!!!!!!!#include #include int tot;char str[10005];int t;//int time[100];struct trie { trie *fail; trie *next[100];原创 2014-08-23 16:00:30 · 501 阅读 · 0 评论 -
二叉树先序遍历
#include #include struct node{ node *left,*right; char letter; node(){ left=NULL; right=NULL; letter=NULL; }}*root;int num;void buildtree(node *p,char *str){ num++; if(str[num]=='#'原创 2014-11-14 09:37:32 · 365 阅读 · 0 评论 -
DP(AC自动机) Keywords Search hdu222
AC自动机的算法参考:http://www.cppblog.com/mythit/archive/2009/04/21/80633.html#include #include int tot;char str[1000005];int t;//int time[100];struct trie { trie *fail; trie *next[26];转载 2014-08-23 14:51:13 · 450 阅读 · 0 评论 -
并查集+欧拉回路+字典树 Colored Sticks POJ 2513
输入多组数据,每组数据两种颜色,表示一根木头两端的颜色,现在要将这些木头相连,要求相连部分颜色相同,问能否全部连通提示1)一个要判断所有的木头是否在一个集合中,即是否能相连2)判断一种颜色出现的数量3)一棵树如果只有0或2个点出现次数为奇数,则树可以一笔画成#include #include #define maxn 500005int tot;int f[maxn];原创 2014-08-17 11:01:18 · 647 阅读 · 0 评论 -
UVA 10200 Prime Time 暴力水题
一个简单的暴力水题,只是输出方式让人无语。。。#include #include int prime(int n){ int i; for(i=2;i*i<=n;i++) { if((n%i)==0) return 0; } return 1;}int main(){ int num[10010原创 2015-05-09 22:27:36 · 2205 阅读 · 1 评论 -
Number of Ways--CodeForces 466C 水题
题意:输入一组数,将其割成三段,每一段的值相同解法:因为要割三组值相同,所以每一组的值一定是整体求和的1/3千万记得long long,血的代价#include #include long long num[500005];int main(){ int n; long long a; int i,j; long long all; wh原创 2015-03-21 15:45:02 · 591 阅读 · 0 评论 -
HDU 5305 Friends
水水的DFS题意:n个人m个关系,要求每个人的网上朋友和现实朋友一样多,求一共有多少种关系解:因为数据小,暴力DFS就可以了,不过要一点小剪枝#include #include int point[10];struct aaa{ int x,y;}line[30];struct bbb{ int online,outline;}node[10];int原创 2015-07-26 10:11:04 · 355 阅读 · 0 评论 -
Light OJ 1341 Aladdin and the Flying Carpet
题意:求大于b的a的因数对有几组。例10 2结果为{2,5},12 2结果为{2,6}{3,4}-----不重复解一:分解质因数+DFS#include #include #include using namespace std;typedef long long ll;const int maxn=1000005;int prime[maxn];int num[maxn]原创 2015-07-21 08:46:00 · 575 阅读 · 0 评论 -
华为机考笔试题 找城市
一个城市规划问题,一个地图有很多城市,两个城市之间只有一种路径,切断通往一个城市i的所有路径之后,其他的城市形成了独立的城市群,这些城市群里最大的城市数量,就是聚集度DPi,现在给出一个地图上各个城市的路径,输出聚集度最小的城市,如果有多个结果,按照编号从小到大第一行输入 城市节点数目N后面N-1输入城市之间的路径......原创 2022-06-22 11:08:40 · 1354 阅读 · 2 评论 -
hdu 5492 Find a path DP
题意:矩阵中找一条路径,是的路径上的各个点的数值方差最小解一:DP将题中算是化简可得(n+m-1)*segma(Ai^2)-(segma(Ai))^2dp[i][j][k]表示到矩阵中i,j这个点和为k的最小的Ai^2的和dp[i][j+1][k+num[i][j+1]]=MIN(dp[i][j+1][k+num[i][j+1],dp[i][j][k]+num[i][j]*num[原创 2015-09-29 15:45:05 · 388 阅读 · 0 评论 -
hdu 5303 Delicious Apples(dp)
题意:一个长为L的圈种上n颗树,每棵树的坐标为xi,结了ai个苹果,用大小为k的篮子把所有苹果装回来,问最少走多少路解:被神奇的dp教做人了(其实我比较水,本来以为左边贪心一下,右边贪心一下在最后转一圈就搞定的水题=-=)#include #include #include #define ll __int64#define MIN(a,b) ((a)<(b)?(a):(b))c原创 2015-07-27 16:38:30 · 481 阅读 · 0 评论 -
DP Good Sequences
给N个升序的数字,要求找出一个子串,每相邻两个数字不互质,求最长串的长度提示1)dp[i]表示到第i个数字的最长串2)dp[i]用前i-1项中与第i项不互质的最大项更新3)寻找与第i项不互质,即找与第i项有公公因数,所以建立数组dig[i]表示该因数出现的次数#include #include #define maxn 100005int dp[maxn];int di原创 2014-08-17 17:01:12 · 618 阅读 · 0 评论 -
The World is a Theatre codeforces131c
题意:n个男生和m个女生选t个人,至少4男1女提示:要用long long类型,不然会爆方法一:直接排列组合#include #include typedef long long ll;ll doo(ll i,ll n){ ll a=1; for(ll l=1;l<=i;l++) { a*=n-l+1; a/=l;原创 2015-03-22 15:18:40 · 602 阅读 · 0 评论 -
欧拉函数
φ函数的值φ(x)通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn)原创 2014-11-16 16:28:22 · 327 阅读 · 0 评论 -
勒让德定理
小于n,#include int min(int a,int b){ return a<b?a:b;}int lrd(int sum,int n,int k){ int t=n/k; if(t!=0) { k*=k; sum=lrd(sum+t,n,k); } return sum;}int main()原创 2014-11-25 13:09:01 · 1790 阅读 · 0 评论 -
poj1061 青蛙的约会 扩展欧几里得
(1)at+bp=c有解的条件是c%gcd(a,b)=0;(2)求解at+bp=gcd(a,b)得x1,y1,则原式解x=x1*(c/gcd(a,b)),y=y1%(c/gcd(a,b));(3)问题等化为(n-m)t+pl=x-y;#include long long x1,y1;long long extended(long long a,long long b){原创 2015-05-01 14:24:29 · 295 阅读 · 0 评论 -
找素数
(1)增加布尔型数组原创 2014-10-23 17:10:41 · 612 阅读 · 0 评论 -
HDU 4746 Mophues 莫比乌斯第三弹
题意:1例:24=2*2*2*3,k=4解:设f[n]为gcd(a,b)=n的对数 F[d]为d|gcd(a,b)的对数 f[n]=sigema(mu[i],F[i*n]): f[1]=mu[1]*F[1]+mu[2]*F[1*2]+...+mu[n]*F[1*n] f[2]=mu[2]*F[2]+mu[2]*F[2*2]+...+mu[n]*F原创 2015-08-02 16:35:15 · 604 阅读 · 0 评论 -
UVA 11426 GCD Extreme (II) 欧拉函数
1)欧拉函数φ(n)为所有小于n的正整数与n的GCD和,φ(n)=n*(1-1/a1)*(1-1/a2)......(a1,a2...为n的约数)2)若对于B有n个数与其互质,则循环到i*B则可为pe[i*B]增加pe[B]*i3)最后值即求a[1]+a[2]+....+a[n]#include #include #define N 4000010int pe[N];long原创 2015-05-03 16:41:40 · 533 阅读 · 0 评论 -
Steps 数论水题
原题DescriptionOne steps through integer points of the straight line. The length of a step must be nonnegative and can be by one bigger than, equal to, or by one smaller than the length of the previ原创 2015-08-10 15:19:27 · 482 阅读 · 0 评论 -
hdu 5382 GCD?LCM!
题意:[exp]为一个判断式,若exp为真,则值为1,否则为0F[n]=segema(1,n,segema(1,n,[lcm(i,j)+gcd(i,j)>=n]))S[n]=segema(1,n,F[i])解:#include #include #define ll __int64const int maxn=5+1e6;const ll mod=258280327;in原创 2015-08-15 16:10:19 · 704 阅读 · 0 评论 -
URAL 1720 Summit Online Judge
题意:给了x,y,l,r,求由x,y可以线性组合出多少个在[l,r]内的数。例x=4,y=5,l=7,r=13,则x,y可以组合出8,9,10,12,13解:所有x,y线性组合的数可化为区间[x,y],[2x,2y],[3x,3y]......从[kx,ky]开始区间重叠,则(k+1)x在[l,r]区间内的数可化为f[r]-f[l-1],f[x]为x之前由x,y线性组合的数的个数#原创 2015-07-19 15:50:32 · 460 阅读 · 0 评论 -
zoj 3175 Number of Containers 分块加速
题意:计算n以内i的倍数的个数,不包括i本身,求和解:直接暴力for一遍一定是不行的,不过用一个分块加速就可以了#include #include int main(){ int t; long long n; while(scanf("%d",&t)!=-1) { while(t--) { sc原创 2015-08-13 09:52:36 · 627 阅读 · 0 评论 -
HDU 1695 GCD 莫比乌斯第二发
题意:求[1,b]和[1,d]内公约数为k的对数(错了N发之后才看到a和c为1。。。)解一:容斥原理和欧拉函数http://www.cnblogs.com/kuangbin/p/3269182.html参考大神的文章吧,我没写=-=解二:莫比乌斯设f[x]为GCD(a,b)=k的对数 F[x]为k|x的对数所以b,d均除k就是求所有GCD为1的对数sum+=原创 2015-08-01 14:01:41 · 602 阅读 · 0 评论 -
HDU 4675 GCD of Sequence
题意:给N个数,要求改变其中K个数,是改变后的数列GCD为1~M,问对于各个GCD一共有多少组数解:1、预处理C(n,m),这里用快速幂计算n!,m!,(n-m)!,quick(a,mod-1)=a2、给定数列中统计i的倍数的个数为temp,如果n-temp>k,那么把k个数换掉,GCD依旧不为i,所以sum[i]=0;若n-tempsum[i]=(m/i)^(n-temp)*(m原创 2015-08-04 09:49:41 · 453 阅读 · 0 评论 -
Digital Clock 数论水题
DescriptionDigital clocks usually show the time in the form hh:mm:ss, where hh is a number between 00 and 23, and both mm and ss are numbers between 00 and 59. Removing the colons from hh:mm:ss will原创 2015-08-10 16:13:18 · 565 阅读 · 0 评论 -
数论 Number Transformation HDU4952
已知n,k,操作k次,每次操作求大于n且能被次数i整除的最小的数已知x*i,所以(i+1)*y>=x*i,y>=x-[x/(i+1)],当x#include int main(){ long long n,k; long long i; int time=0; while(scanf("%I64d%I64d",&n,&k)!=-1) {原创 2014-08-16 14:58:16 · 720 阅读 · 0 评论 -
SPOJ VLATTICE Visible Lattice Points 初入莫比乌斯
题意:求两个点(x,y,z)的连线不经过其他点有几个解:即为求GCD(x,y,z)为1的点有几个解一:因为x,y,z均在1~n内,所以可以用欧拉函数解出解二:莫比乌斯反演设f[n]为GCD(x,y,z)=n的个数设F[b]为b|GCD(x,y,z)的个数,很明显F[b]=(n/i)*(n/i)*(n/i)所以F[n]=sigema(b|n,f[b]);f[n]=sige原创 2015-07-30 11:55:51 · 1009 阅读 · 0 评论