SGU 499 Greatest Greatest Common Divisor
2012-03-09 16:03
295 查看
给定N个数,每两个数有一个最大公约数,求最大的最大公约数。N<=10^5,每个数<=10^6
枚举求是N^2*logN的,必定超时
换一个思考的角度,如果我们假设最大公约数是多少,用多少时间可以判定是否成立呢?
假设判定x,那么给变量每次加x,看有没有这个值,有的话就计数器加一,计数器一旦大于1,就成立,跳出即可。
这样,我们开一个10^6的数组,v[i]记录值为i的数的个数,判定有没有值时就是O(1)的了,总体时间在max_number*logN左右
View Code
枚举求是N^2*logN的,必定超时
换一个思考的角度,如果我们假设最大公约数是多少,用多少时间可以判定是否成立呢?
假设判定x,那么给变量每次加x,看有没有这个值,有的话就计数器加一,计数器一旦大于1,就成立,跳出即可。
这样,我们开一个10^6的数组,v[i]记录值为i的数的个数,判定有没有值时就是O(1)的了,总体时间在max_number*logN左右
View Code
program sgu499(input,output); var v : array[0..1100000] of longint; n,max : longint; procedure init; var i,x : longint; begin fillchar(v,sizeof(v),0); max:=0; readln(n); for i:=1 to n do begin readln(x); inc(v[x]); if x>max then max:=x; end; end; { init } function check(x :longint ):boolean; var s,sum : longint; begin s:=0; sum:=x; while (sum<=max) do begin s:=s+v[sum]; if s>1 then begin check:=true; exit; end; sum:=sum+x; end; check:=false; end; { check } procedure main; var i : longint; begin for i:=max downto 1 do if check(i) then begin writeln(i); break; end; end; { main } begin init; main; end.
相关文章推荐
- SGU - 499 Greatest Greatest Common Divisor 枚举因子(n logn)
- SGU 499:Greatest Greatest Common Divisor
- ssu 499 Greatest Greatest Common Divisor
- sgu 499. Greatest Greatest Common Divisor(枚举因子)
- P32 (**) Determine the greatest common divisor of two positive integer numbers.
- Calculate the Greatest Commom Divisor (GCD) and Lowest Common Multiple (LCP) of Two Integers
- hdu 5207 Greatest Greatest Common Divisor(筛法求公约数)
- hdu 5207 Greatest Greatest Common Divisor
- hdu 5207 Greatest Greatest Common Divisor
- greatest common divisor introduction to algorithm 3rd, example 31.2
- HDU 5207 Greatest Greatest Common Divisor
- 最大公倍数_Greatest Common Divisor
- getGreatestCommonDivisor
- HDOJ 5207 Greatest Greatest Common Divisor 暴力枚举
- hdu 5210 Greatest Greatest Common Divisor
- 最大公约数(greatest common divisor)算法
- GCD Algorithm(Greatest Common Divisor)
- Eculid算法递归地求解GCD(Greatest Common Divisor,最大公约数)
- BestCoder Round #38 1002.Greatest Greatest Common Divisor
- [计算最大公因子]Finding the greatest common divisor (gcd) of 2 numbers