Ultraviolet A 11121-base-2

Source: Internet
Author: User

Question: Calculate the expression of the number based on-2.

Analysis: number theory. You can find the rule by writing the number intervals that can be expressed by different digits.

1: []; 2: [-2,-1]; 3: [];

It is observed that the interval length increases to 1, 2, 4, 8,..., 2 ^ K, and the parity interval is enabled;

In this way, we can find the corresponding location of 1 in order, and find the next location every time minus the corresponding base (-2 ^ K.

Note: It is also number theory.

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;long long l[40],r[40],b[40];int       ans[40];int main(){l[0] = 1; r[0] = 1; b[0] = 1;l[1] = -2;r[1] = -1;b[1] = -2;long long base = 4;for (int i = 2 ; i < 33 ; ++ i) {if (i%2) {r[i] = l[i-2]-1;l[i] = l[i-2]-base;}else {l[i] = r[i-2]+1;r[i] = r[i-2]+base;}base <<= 1;b[i] = b[i-1]*-2;}int n,m,s,e;while (~scanf("%d",&n))for (int i = 1 ; i <= n ; ++ i) {scanf("%d",&m);printf("Case #%d: ",i);for (int i = 0 ; i < 33 ; ++ i)ans[i] = 0;s = 32;while (s >= 0) {if (m >= l[s] && m <= r[s]) {ans[s] = 1;m -= b[s];}s --;}e = 32;while (e > 0 && !ans[e]) e --;while (e >= 0) printf("%d",ans[e --]);printf("\n");}return 0;}


Ultraviolet A 11121-base-2

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.