[Huawei machine trial exercise questions] 14. Separated by integers, Huawei exercises

Source: Internet
Author: User

[Huawei machine trial exercise questions] 14. Separated by integers, Huawei exercises

Question

Description: an integer can be divided into the sum of the power of 2. For example: 7 = 1 + 2 + 47 = 1 + 2 + 2 + 27 = 1 + 1 + 1 + 47 = 1 + 1 + 1 + 2 + 27 = 1 + 1 + 1 + 1 + 1 + 1 + 27 = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 there are six different splitting methods. For example, 4 can be split into 4 = 1 + 1 + 1 + = 2 + = 1 + 1 + 2. F (n) indicates the number of different splits of n, for example, f (7) = 6. write a program, read n (no more than 1000000), and Output f (n) % 1000000000. Question type: null difficulty: Primary running time limit: 10Sec memory limit: 128MByte stage: pre-entry exercise input: each group of inputs contains an integer: N (1 <= N <= 1000000 ). Output: f (n) % 1000000000 is output for each group of data. The output contains multiple rows, with one result per row. If the input data is out of the range, output-1. Sample input: 7 sample output: 6

Ideas

If n = 2 k + 1 is an odd number, f (2 k + 1) = f (2 k ). In fact, the first item of the 2 k + 1 split must be 1. If this 1 item is removed, it will be the same as the 2 k split. When n = 2 k is an even number, we consider splitting between 1 and 1. If one exists, the first two items are 1, which is the same as the 2k-2 split. If there is no 1, divide each item by 2, and the split is the same as that of k. Therefore, f (2 k) = f (2k-2) + f (k );

Code 1

/* ------------------------------------- * Date: 2015-06-30 * Author: SJF0115 * Subject: integer separation * Source: huawei machine --------------------------------------- */# include <iostream> # include <string> # include <algorithm> # include <vector> using namespace std; # define Max limit 00int Count [Max + 1]; int Split (int n) {Count [1] = 1; Count [2] = 2; for (int I = 3; I <= n; ++ I) {// odd if (I % 2) {Count [I] = Count [I-1];} // if else {Count [I] = (Count [I-2] + Count [I/2]) % 1000000000 ;} // else} // for return Count [n];} int main () {int n; // freopen ("C: \ Users \ Administrator \ Desktop \ c00000000.txt "," r ", stdin); while (cin> n) {if (n> Max | n <0) {return-1;} // if cout <Split (n) <endl ;}// while return 0 ;}

Code 2

Timeout

/* ------------------------------------- * Date: 2015-06-30 * Author: SJF0115 * Subject: integer separation * Source: huawei machine --------------------------------------- */# include <iostream> # include <string> # include <algorithm> # include <vector> using namespace std; # define Max limit 00int Split (int n) {if (n = 1) {return 1;} // if (n = 2) {return 2;} // if // odd if (n % 2) {return Split (n-1) % 1000000000;} // if else {return Split (n-2) % 1000000000 + Split (n/2) % 1000000000 ;} // else} int main () {int n; // freopen ("C: \ Users \ Administrator \ Desktop \ c00000000.txt", "r", stdin ); while (cin >>n) {if (n> Max) {return-1 ;}// if cout <Split (n) <endl ;} // while return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.