hdu 5611——Balls Rearrangement

來源:互聯網
上載者:User

http://blog.csdn.net/dyx404514/article/details/9474251

上面的那個很好的解釋了。

以11 5 3 為例  5 3 的最小公倍數15

1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

0
 1  2  3  4  0  1  2  3  4 
 0   1   2   3   4  放進a箱子 i%5的值以5為迴圈

0  1  2  0  1  2
 0  1  2  0   1   2   0   1   2  放進b箱子 i%3的值以3為迴圈

0  0  0  3  3  2  1  1  1  4   1   1   2   2   2  從他們的cost找出規律:發現cost值是 相等的區間最左端對應的上方都是0。

 

#include<iostream>#include<cstdio>using namespace std;#define LL __int64LL gcd(LL a,LL b){LL r;while(b){r=a%b;a=b;b=r;}return a;}LL min(LL a,LL b){if(a<b)return a;return b;}int main(){int t;cin>>t;while(t--){LL n,a,b;LL ans=0;LL temp=0;cin>>n>>a>>b;LL lcm=a/gcd(a,b)*b;LL m=n/lcm;LL k=n%lcm;for(LL i=0;i<lcm;)if(i%a==i%b)i+=min(a-i%a,b-i%b);else{temp+=abs(i%a-i%b)*min(a-i%a,b-i%b);i+=min(a-i%a,b-i%b);}for(LL i=0;i<k;)if(i%a==i%b)i+=min(a-i%a,b-i%b);else{ans+=abs(i%a-i%b)*min(min(a-i%a,b-i%b),k-i);i+=min(min(a-i%a,b-i%b),k-i);}ans+=temp*m;cout<<ans<<endl;}return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.