BZOJ 2257 JSOI2009 瓶子和燃料 數論

來源:互聯網
上載者:User

標籤:bzoj   bzoj2257   數論   

題目大意:給定n個瓶子,選擇k個,可以隨便導油,問選擇k個瓶子可以匯出的油數量的最小值的最大值

首先易知k個瓶子能匯出的油最小值一定是k個瓶子容量的最大公因數 於是問題轉化成了在n個數中選擇k個 使最大公因數最大

找出n個數的所有因數 排序 找出最大的且出現次數大於等於k的輸出即可

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 1600100using namespace std;int n,k;int factors[M],tot;void Get_Factors(int x){int i;for(i=1;i*i<x;i++)if(x%i==0)factors[++tot]=i,factors[++tot]=x/i;if(i*i==x)factors[++tot]=i;}int main(){int i,x;cin>>n>>k;for(i=1;i<=n;i++){scanf("%d",&x);Get_Factors(x);}sort(factors+1,factors+tot+1);int cnt=0;for(i=tot;i;i--){if(factors[i]!=factors[i+1])cnt=0;++cnt;if(cnt==k){printf("%d\n",factors[i]);return 0;}}}


BZOJ 2257 JSOI2009 瓶子和燃料 數論

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.