Ultraviolet A 10560-minimum weight (number theory)

Source: Internet
Author: User

Question connection: Ultraviolet A 10560-minimum weight

Question: Give N and ask how many weights must be weighed at least 1 ~ N weight, give the weight of the selected weight, and give K, indicating that there are K weights need to use the weight of the selected weight.

Solution: the weight of 1 must be selected. It can represent the weight of 1, so the weight of the Next weight must be 3 (2? 1 + 1), so that 1, 3 can be expressed by a weight, while 2, 4 is 3-1 and 3 + 1 respectively, so that 1 ~ The weight of 4 can also be expressed. So there is a formula: 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.