XMU1428 find the regular number of Recursion

Source: Internet
Author: User

1428. hofstadter G-sequenceTime Limit: 1000 MS Memory Limit: 65536 K Total Submissions: 228 (66 users) Accepted: 42 (39 users) [My Solution] DescriptionHofstadter G-sequence is an interesting series. However, due to the limited space, we will not introduce it in detail. In the following code, the requested function G (n) is the Hofstadter G-sequence series: int G (int n) {if (n <= 1) return 1; return n-G (n-1);} Of course, the requirement for this question is not to ask you to find the value of the function G (n), but to give you a positive integer x, you need to solve the problem. How many times does the above function need to call the function G (n) to calculate the value of G (x? Input has only one row, a positive integer x (1 <= x <= 1,000,000 ). Output because the answer may be large, you only need to Output the result after the modulo of the answer 1,000,000,007. Sample Input3Sample Output5Source For details, refer to the Code [cpp] # include <stdio. h> # include <string. h> int f [1000011], ans [1000011]; const int mod = 1000000007; void get () {int I, j; f [1] = 1; for (I = 2; I <= 1000000; I ++) {f [I] = I-f [f [I-1];} ans [1] = 1; for (I = 2; I <= 1000000; I ++) {ans [I] = (ans [I-1] % mod + ans [f [I-1] % mod + 1) % mod ;}} int main () {int I, j, m, n; get (); while (scanf ("% d", & n) = 1) {printf ("% d \ n ", ans [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.