欧几里得算法计算两数最大公约数和最小公倍数是常遇到的问题。现在写几个问题来回顾一下它的应用。
hdu 1222 wolf and rabbit (gcd)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1222
大意:给定长度n,wolf从0开始捕捉兔子,下一点是k%n,再下一点是2k%n,3k%n……问,有没有点是wolf不能访问到的。
可以发现,只有wolf一次移动的距离和总的长度的最大公约数是1,所有的点才都能被遍历。
poj 2773 Happy 2006
题目:http://poj.org/problem?id=2773大意:给定m,求出第k个和m互质的数字。
由于m (1 <= m <= 1000000), K (1 <= K <= 100000000),所以求出第k个互质数字要考虑到一种压缩方法,不要一个一个的暴力判断。
【曾经的思路:对于k的判断,可以用欧拉函数断定其范围,如果kth primer[k<=phi(m)]在m之内,则可以用fac[k]直接输出[m内,可以用素因子排除不互质的数字]。超出m的范围则直接用gcd() 或 素因子判断即可。但是这样TLE了。想用euler数组加二分,但是MLE。-_-怎么办?】
欧几里得算法含有的信息:gcd(b×t+a,b)=gcd(a,b) (t为任意整数)
则如果a与b互素,则b×t+a与b也一定互素,如果a与b不互素,则b×t+a与b也一定不互素,故与m互素的数对m取模具有周期性。由此一来就可解决K较大时的情况。得到的信息:设m范围内的素数有k个,数字n对应至少有互质数n/m*k。当n是k的倍数时,对应的互质数:m*(n/k-1)+fac[k]. 当其不是倍数关系:n/k*m+fac[n%k]
zoj 1577 GCD & LCM
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1577
GCD & LCM
Time Limit: 2 Seconds Memory Limit: 65536 KB
Given x and y (2 <= x <= 100,000, 2 <= y <= 1,000,000), you are to count the number of p and q such that:
1) p and q are positive integers;
2) GCD(p, q) = x;
3) LCM(p, q) = y.
Input
x and y, one line for each test.
Output
Number of pairs of p and q.
Sample Input
3 60
Sample Output
4
分析:GCD(p, q) = x; --> GCD(p/x,q/x)=1;
LCM(p, q) = y. --> LCM(p/x,q/x)=(p*q/x^2)/gcd(p/x,q/x)=p*q/x^2=y/x
设p/x,q/x分别是t1,t2. 有gcd(t1,t2)=1; t1*t2=y/x=w 现在问题转换成寻找有多少对t1,t2互质且乘积就是w.
于是对w素因子分解,设w对应的因子展开式是(1+q1+q1^2+……+q1^n1)(1+q2+q2^2+……+q2^n2)……(1+qk+qk^2+……+qk^nk),那么互质的两个乘积因子情况:qi^ni的组合数,即C(n,0)+C(n,1)+C(n,2)+……+C(n,n)=2^n。【不可能是qi^nj,其中0<nj<ni,那样的话就不互质了】
hdu 1019 Least Common Multiple
http://acm.hdu.edu.cn/showproblem.php?pid=1019
分析:单纯的求多个数字的最小公倍数。对的,没有别的陷阱。