Corner Valley conjecture---memory search

Source: Internet
Author: User

2969 Corner Valley conjecture

time limit: 1 s space limit: 32000 KB

    Title Description Description

    The so-called Angular Valley conjecture, that is, given a positive integer n, the following two transformations are repeated on N:
    1) If n is an even number, divide by 2;
    2) If n is an odd number, multiply by 3 plus 1.
    The final result is always 1.

    The number of transformations we need to make from N to 1 is called the transformation length of N, as the transformation of the number 7 is:

    7-22-11-34-17-52-26-13-40-20-10-5-16-8-4-2-1

    A total of 16 transformations were made, thus 7 of the transformation length was 16.

    Wish is now interested in the longest transformation length in a given interval, but the hand counts too much, so he finds you in the informatics contest, can you help him?

    Enter a description input Description

    Each test point contains multiple sets of data, and the first row is a number T, representing the number of data.
    The second line to line t+1, two numbers a, b for each line, represents the longest transformation length of a and B (including A, b).


    outputs description output Description

    Output format
    T row, each row outputs the longest transformation length for each interval of the input data.

    sample input to sample

    2
    1 7
    9 20

    Sample output Sample outputs

    16
    20

    data size & Hint

    Data range
    1 <= T <= 100
    1 <= A, b <= 10^8
    Interval length not exceeding 10^5

    The idea is to open a large array of pre-storage results, and then the memory of the search is much faster, between the magnitude of the point

    To use a 64-bit integer

    #include <stdio.h> #include <string.h>int a[1000000];int dfs (long long i) {if (I < 1000000) {if (a[i]! =-1) return a[i];} if (i% 2 = = 0) return Dfs (I/2) + 1;else return DFS (i * 3 + 1) + 1;} int main () {Long Long i;memset (A,-1, sizeof (a)); A[1] = 0;for (i = 2; i < 1000000; i++) {A[i] = DFS (i);} int m, n, J, T, sum;scanf ("%d", &t), while (t--) {int tmp;scanf ("%d%d", &m, &n), if (M > N) {tmp = M;m = N;n = TMP;} sum = 0;for (i = m; i <= N; i++) {tmp = DFS (i); if (tmp > SUM) sum = tmp;} printf ("%d\n", sum);} return 0;}


    Corner Valley conjecture---memory search

    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.