CF 11B jumping Jack (greedy, mathematical proof,,)

Source: Internet
Author: User

Test instructions

Give a number x.

The starting point is the coordinate 0. 1th Step Jump 1 grid, 2nd step Jump 2, 3rd step jump 3 grid, ... And so on

You can jump to the left or right at a time.

Ask at least jump a few steps to coordinate x.

Ideas:

Suppose X is a positive number.

The quickest way to approximate x is to keep going to the right. If X is crossed, the X1 is assumed to be x1-x less than the last step d.

If X1-x is an even number, change the previous x to-X. Then you can go to X.

If X1-x is an odd number, because the previous x is changed to-X, it is actually back 2x. So you must take one more step to the right. Until X1-x is an even number. (In fact, just one or two steps)

X is negative and the x is positive.

Code:

ll X,d;intMain () {CIN>>x; if(x==0) {puts ("0"); return 0; }    if(x<0) {x=-x; } d=1;  while(D* (d+1)/2&LT;X) + +D; if(D* (d+1)/2==x) {printf ("%i64d\n", D); }    Else{ll x1=d* (d+1)/2; ll Deta=x1-x; if(deta%2==0) {printf ("%i64d\n", D); }        Else{             while(deta%2!=0){                ++D; X1+=D; Deta=x1-x; } printf ("%i64d\n", D); }    }    return 0;}

CF 11B jumping Jack (greedy, mathematical proof,,)

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.