uva 10560 - Minimum Weight(數論)

來源:互聯網
上載者:User

標籤:style   http   color   os   width   art   

題目串連:uva 10560 - Minimum Weight

題目大意:給出n,問說至少須要多少個不同重量的砝碼才幹稱量1~n德重量,給出所選的砝碼重量,而且給出k,表示有k個重量須要用上述所選的砝碼測量。

解題思路:重量為1的砝碼肯定要選,它能夠表示到1的重量,那麼下一個砝碼的重量肯定選擇3(2?1+1),這樣1,3分別能夠用一個砝碼錶示,而2,4分別為3-1和3+1,這樣1~4的重量也都能夠表示。於是有公式ai=si?1?2+1。

#include <cstdio>#include <cstring>#include <vector>using namespace std;typedef unsigned long long ll;ll n, S;vector<ll> ans;void init (ll n) {    S = 0;    ans.clear();    while (S < n) {        ll u = S * 2 + 1;        ans.push_back(u);        S += u;    }    printf("%lu", ans.size());    for (int i = 0; i < ans.size(); i++)        printf(" %lld", ans[i]);    printf("\n");}void solve () {    ll k, s = S;    scanf("%lld", &k);    int sign = 1, flag = 0;    for (int i = ans.size() - 1; i >= 0; i--) {        ll t = (s - 1) / 3;        s -= ans[i];    //    printf("%lld %lld %lld\n", ans[i], s, k);        if (k <= t)            continue;        if (flag)             printf("%c", sign > 0 ? ‘+‘ : ‘-‘);        if (k < ans[i]) {            sign *= -1;            k = ans[i] - k;        } else {            k = k - ans[i];        }        flag = 1;        printf("%lld", ans[i]);    }    printf("\n");}int main () {    int k;    while (scanf("%lld%d", &n, &k) == 2 && n + k) {        init(n);        for (int i = 0; i < k; i++)            solve();    }    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.