Python hungry Little Yi (NetEase written questions)

Source: Internet
Author: User

Earlier this week, the learning brother sent me a netease pen test, hungry small easy, feel a little meaning ~ share to everyone

Title Description:

Small easy always feel hungry, so as Octopus's small easy often go out looking for shells to eat. Initially small easy to x_0 in an initial position. For the current position of the small easy x, he can only move through the mysterious force to 4 * x + 3 or C. Because the use of mystical power consumes too much energy, it can only use mystical forces up to 100,000 times. Shells are always grown in locations that can be divisible by 1,000,000,007 (e.g. position 0, position 1,000,000,007, position 2,000,000,014, etc.). Small easy need you to help calculate the minimum number of times you need to use the mysterious power to eat shells.

Input:

Enter an initial position of x_0, ranging from 1 to 1,000,000,006

Output:

Output small easy minimum need to use the number of mysterious force, if the use of the number of times the shell has not been found, then output-1

at first glance at this problem, is a little confused, if the violent law to do 100% overtime. Judging from my experience that this is a mathematical problem, let's analyze the key information in the topic description and see if there is any mystery. Small octopus can only be moved to 4 * x + 3 or 8 * x + 7, then we think F (x) =4 * x + 3, g (x) =8 * x + 7. The key part arrived, I stare two eyes for a long time, finally found

1. F (g (x)) = g (f (x)) We can assume that the final movement of the small octopus is a string that can be expressed in FG, and that FG can switch positions at will ~ so greasy, ffggffgf=fffffggg

2. F (f (f (x))) =g (g (x)) that is, every 3 F-move is equal to 2 G-move, then we can change the result of the FG string to 2 G for every 3 F, then the result of the FG string is a maximum of 2 F of the FG string ~ so greasy, fffffggg=ffggggg

Now the problem-solving thinking is very clear, with 0,F,FF as the starting position, each time to move g, see when can be moved to 1000000007 divisible by the position. The code is as follows:

#!/usr/bin/python#-*-coding:utf-8-*-defGet_result (n): L_= [N, N * 4 + 3, + * n + 15]     forJ, MinchEnumerate (l_): forIinchRange (100000): M= (8 * m + 7)% 1000000007ifm = =0:returnI+j+1return-1if __name__=='__main__': N=input ()PrintGet_result (N)

Well, this problem has been perfectly solved.

Hope to be helpful to everyone ~

Python hungry Little Yi (NetEase written questions)

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.