codeforces 8VC Venture Cup 2016 - Elimination Round C. Lieges of Legendre

來源:互聯網
上載者:User

標籤:

C. Lieges of Legendre

題意:給n,m表示有n個為2的倍數,m個為3的倍數;問這n+m個數不重複時的最大值 最小為多少?

資料:(0 ≤ n, m ≤ 1 000 000, n + m > 0)
ps:很水的題,主要是策略;

思路:由於裡面每隔6就會重複一次,不好直接類比,並且類比的效率很低,那就二分吧!二分即上界為2單獨的最大倍數與3單獨時的最大倍數之和,下界為前面二者的max;之後利用判斷是否mid/2 >= n && mid/3 >= m && mid/2 + mid/3 - mid/6 >= n + m 即可二分;這樣running 就是log(n)了;注意最後還要判斷ans是否為2|3的積即可;

#include<bits/stdc++.h>using namespace std;int main(){    int n,m;    scanf("%d%d",&n,&m);    int l = max(n*2,m*3),r = n*2 + m*3,ans = 0;    while(l <= r){        int mid = l + r >> 1;        if(mid/2 >= n && mid/3 >= m && mid/2 + mid/3 - mid/6 >= n + m) ans = mid,r = mid - 1;        else l = mid + 1;    }    if(ans%2 != 0 && ans%3 != 0) ans++;    printf("%d",ans);    return 0;}

 

codeforces 8VC Venture Cup 2016 - Elimination Round C. Lieges of Legendre

聯繫我們

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