2013年4月11日
摘要: 代码思想来自scturtle's 渣代码集散地,转载请注明出处不互质的中国剩余定理:利用扩展欧几里得定理将方程两两合并求解假设C ≡ A1 (mod B1),C ≡ A2 (mod B2)。令C = A1 + X1B,那么X1B1 ≡ A2 − A1 (mod B2)。用扩展欧几里德算法求出X1,也就求出C。令B = lcm(B1, B2),那么上面两条方程就可以被C’ ≡ C (mod B)代替。迭代直到只剩下一条方程。你可以理解为新的答案C'即要mod B1,又要mod B2,所以就mod lcm(B1, B2)。而余数C 是刚前面求得的答案,也就是说C'不管怎么折 阅读全文
posted @ 2013-04-11 14:57 行者1992 阅读(202) 评论(0) 推荐(0) 编辑
摘要: 1 /* 2 参考别人的代码写的 3 */ 4 #include <stdio.h> 5 #include <string.h> 6 #include <iostream> 7 #include <math.h> 8 using namespace std; 9 int ax,by;10 int ex_gcd(int a,int b)11 {12 if(b==0)13 {14 ax=1;15 by=0;16 return a;17 }18 int gcd=ex_gcd(b,a%b);19 int temp... 阅读全文
posted @ 2013-04-11 10:30 行者1992 阅读(173) 评论(0) 推荐(0) 编辑
  2013年4月10日
摘要: 题目链接:http://poj.org/problem?id=1061分析:两只青蛙若相遇的话,比满足方程(s*m+x)-(n*s+y)=k*l(其中,s为跳跃的次数,k为绕纬度线的圈数)方程整理得s*(n-m)+k*l=x-y(设gcd为(x-y)与k的最大公约数)这就是所谓的线性同余方程,一般表述为a*x+b*y=c;若该方程有解,必有c%gcd=0;解方程ax-by=gcd得特解x0,y0乘以c/gcd得原方程特解x1=x0*c/gcd,y1=y0*c/gcd.则原方程的一般解为x=x1+k*b/gcd;y=y1+k*a/gcd(k为常数)注:此类问题需要扩展欧几里得的基础,另外关于同余 阅读全文
posted @ 2013-04-10 19:52 行者1992 阅读(184) 评论(0) 推荐(0) 编辑
摘要: 注:扩展欧几里得的解是不唯一的,假设x0,y0为特解,则其解系为:x=x0+k*(b/gcd),y=y0-k*(a/gcd)递归代码实现如下://其中ax,by为全局变量int ex_gcd(int a,int b){ if(b==0) { ax=1; by=0; return a; } gcd=ex_gcd(b,a%b); int temp=ax; ax=by; by=temp-a/b*by; return gcd;} 阅读全文
posted @ 2013-04-10 19:34 行者1992 阅读(202) 评论(0) 推荐(0) 编辑
  2013年4月9日
摘要: 1 #include <string.h> 2 #include <stdio.h> 3 #include <math.h> 4 #include <iostream> 5 using namespace std; 6 const int maxn=100005; 7 bool prime[maxn]; 8 int k=0; 9 int p[maxn];10 void isprime()11 {12 13 memset(prime,1,sizeof(prime));14 prime[0]=prime[1]=0;15 int sq=sqrt((do 阅读全文
posted @ 2013-04-09 15:04 行者1992 阅读(255) 评论(1) 推荐(0) 编辑
  2013年4月3日
摘要: 七夕节 1 /* 2 写于13年3月28日,最朴素直接的算法 3 改用筛法重写了一遍,没想到朴素法居然不超时,毁三观啊 4 */ 5 6 #include <iostream> 7 #include <stdio.h> 8 #include <string.h> 9 #include <math.h>10 using namespace std;11 const int maxn=500005;12 long long data[maxn];13 int main()14 {15 int t;16 int n;17 memset(data,0,s 阅读全文
posted @ 2013-04-03 09:56 行者1992 阅读(175) 评论(0) 推荐(0) 编辑
  2013年4月2日
摘要: Keywords SearchAC自动机典型模板/*写于13年4月1日,AC自动机练习参考的别人代码,个人觉得自动机就是trie 外加kmp的有机组合*/#include<string.h>#include <iostream>#include <stdio.h>using namespace std;const int maxn=10010*50;char str[1000010];//文本串struct node{ node *fail; node *next[26]; int flag; node() { fail=NULL; f... 阅读全文
posted @ 2013-04-02 20:57 行者1992 阅读(175) 评论(0) 推荐(0) 编辑
  2013年3月21日
摘要: hdu 2138 How many prime numbers 1 /* 2 2013-03-21 15:10:20 Accepted 2138 15MS 544K 1338 B 3 筛法就是预先打表处理 4 任何一个非素数必能转换成若干素数之积 5 本题根据以上定理给出10以内的素数,然后打一个素数表, 6 再根据定理求给定的数是否为素数 7 */ 8 #include <iostream> 9 #include <stdio.h>10 #include <string.h>11 #include <math.h>12 using namesp 阅读全文
posted @ 2013-03-21 15:24 行者1992 阅读(300) 评论(0) 推荐(0) 编辑
摘要: 分拆素数和 1 /* 2 写于13年3月21日,练习素数筛法 3 用普通的方法超时啊啊啊啊!!! 4 2013-03-21 11:19:23 Accepted 2098 15MS 268K 5 */ 6 #include <iostream> 7 #include <stdio.h> 8 #include <string.h> 9 using namespace std;10 bool prim[10010];//prim[i]=1表示i为素数11 void is_prim()12 {13 memset(prim,1,sizeof(prim));14 p.. 阅读全文
posted @ 2013-03-21 11:30 行者1992 阅读(195) 评论(0) 推荐(0) 编辑
摘要: Count the string 1 /* 2 写于13年3月21日 3 http://acm.hdu.edu.cn/showproblem.php?pid=3336 4 2013-03-21 10:39:01 Accepted 3336 46MS 2016K 5 很巧妙的利用了KMP函数get_next()的思想 6 通过对其进行改写达到目的 7 data[i]表示[1....i]字符串出现的次数 8 */ 9 10 #include <iostream>11 #include <stdio.h>12 #include <string.h>13 using 阅读全文
posted @ 2013-03-21 10:40 行者1992 阅读(170) 评论(0) 推荐(0) 编辑