BZOJ2257(Jsoi2009)[瓶子和燃料]--最大公因數(裴蜀定理)

【鏈接】
bzoj2257

【解題報告】]

這題用裴蜀定理,解釋如下:

裴蜀定理:若ab 是整數,且Gcd(a,b)==d ,那麼ax+by (xy 爲整數)一定是d 的倍數。

所以只需要求出所有數的因子。答案就是數量超過K 的最大因子。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=10000005;
int n,m,cnt,num,a[maxn];
void Work(int x)
{
    for (int i=2,k=sqrt(x); i<=k; i++)
     if (x%i==0)
      {
        a[++cnt]=i;
        if (i*i!=x) a[++cnt]=x/i;
      }
    a[++cnt]=x;
}
int main()
{
    freopen("2257.in","r",stdin);
    freopen("2257.out","w",stdout);
    scanf("%d%d",&n,&m); cnt=0;
    for (int i=1,x; i<=n; i++) scanf("%d",&x),Work(x);
    sort(a+1,a+1+cnt); a[0]=0; num=1;
    for (int i=cnt; i>=0; i--)
     if (a[i]==a[i-1]) num++;
      else
       {
        if (num>=m) {printf("%d",a[i+1]); return 0;}
        num=1;
       }
    printf("1");
    return 0;
}
發佈了143 篇原創文章 · 獲贊 73 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章