JSOI2009 BZOJ2257 瓶子和燃料
想法其实是挺巧妙的。。找到所有数的所有因数,统计最大的一个出现过K次的因数。
Code:
#include <cstdio> #include <iostream> using namespace std; long a[1001]; long s[1000001]; long gcd(long a,long b){ if (a%b==0) { return b; } if (b%a==0) { return a; } return gcd(b,a%b); } void qqsort(long l,long r){ long i=l,j=r,x; x=s[(l+r)/2]; /*cout <<l <<" " <<r <<endl;*/ long t; while (i<=j){ while (s[i]<x){ i++; } while (x<s[j]){ j--; } if (i<=j){ t=s[i];s[i]=s[j];s[j]=t; i++;j--; } } if (i<r){ qqsort(i,r); } if (j>l){ qqsort(l,j); } } int main(){ /*freopen("bottle.in", "r", stdin) ; freopen("bottle.out", "w", stdout) ;*/ long n,k; cin >>n >>k; for (long i=1;i<=n;i++){ cin>>a[i]; } long r=0; for (long i=1;i<=n;i++){ for (long j=1;j*j<=a[i];j++){ if (a[i]%j==0){ r++;s[r]=j; //cout <<s[r] <<endl; r++;s[r]=a[i]/j; //cout <<s[r] <<endl; } } } qqsort(1,r); long app=1; for (long i=r-1;i>0;i--){ if (s[i]==s[i+1]){ app++; } else { app=1; } if (app>=k){ cout <<s[i] <<endl; break; } } /*cin >>n;*/ }