【NOIP2017GDKOI】a

Description

這裏寫圖片描述

Solution

很明顯的一道題,只要仔細想想就能發現:某兩個數所能構成的最小數是他們的最大公因數。當然,所求出的公因數也可以和其他數求最大公因數,那就相當於對所有數都一起求出最大公因數。爲什麼要求出最大公因數呢?因爲求出了最小的可組成的數時,就能夠一點點減掉,能減到零的數必然是這個公因數的倍數。

Code

var
    a,b:array[0..100000] of longint;
    n,m,i,j,ans,tot:longint;
    num:int64;
function gcd(x,y:int64):int64;
begin
    while x mod y<>0 do
    begin
        gcd:=x mod y;
        x:=y;y:=gcd;
    end;
    exit(y);
end;
begin
    readln(n);
    for i:=1 to n do read(a[i]);
    num:=a[1];
    for i:=2 to n do
    begin
        num:=gcd(num,a[i]);
    end;
    readln(m);
    for i:=1 to m do
    begin
        read(b[i]);
        if b[i] mod num=0 then inc(ans);
    end;
    writeln(ans);
end.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章