UVA10940-Throwing cards away II (query rule)

Source: Internet
Author: User

UVA10940-Throwing cards away II (query rule)

UVA10940-Throwing cards away II (query rule)

Question Link

There are n cards on the table, from top to bottom in the order of 1-n, each time the first card is lost, and then the second card is placed at the end of the stacked card. Repeat this operation until only one card is left.

Solution: You can only use brute force to print the n in front of the dot to see the rule.
Rule: f [2 ^ k + mod] = 2 * mod; (mod> 0); n = 1 requires special judgment.

Code:

# Include

  
   
# Include

   
    
Const int maxn = 5e5 + 5; int f [maxn]; void init () {int tmp = 1; f [1] = 1; for (int I = 2; I <= maxn-5; I ++) {if (I> tmp * 2) {tmp * = 2; f [I] = 2 * (I-tmp );} else f [I] = 2 * (I-tmp) ;}} int main () {init (); int n; while (scanf ("% d", & n) & n) {printf ("% d \ n", f [n]);} 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.