Uva 1583 Digit Generator

Source: Internet
Author: User

The upper limit of this problem is 100000, the time to find the smallest generator in 100,000 numbers is too high, and there is a limit to the sample number, even if the algorithm is more concise to write time out. Therefore, the way to solve the problem by using the table method, but there is still a problem, that is, if you find from 1 to 100000 is still very time-consuming, then, Rujia words to understand the direction ——— to find out the generator of each number, and constantly refresh the smallest generated element.

To tell the truth, although I finished the problem nine months ago, but the idea of the table I have to return to Liu Ru Jia =, now regain.

When seeking the most niche narimoto of each number, it is advisable to start with the largest number of tables to find out the respective generators, and then constantly refresh the minimum value until it is convenient to 1.

Here's the code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int data[100300];

int Mix (int num)
{
    int mid = 0, nums = num, pri = num;
    while (num > 0)
    {
        num%=;
        Mid + = num;
        Nums/=;
        num = nums;
    }
    return mid + pri;
}

void Prepare ()
{
    memset (data, 0, sizeof (data));
    for (int i = 99970; I >= 1; i--)
    {
        int num = Mix (i);
        Data[num] = i;
    }
}

int main ()
{
    Prepare ();
    int t;
    scanf ("%d", &t);
    while (t--)
    {
        int mid;
        scanf ("%d", &mid);
        printf ("%d\n", Data[mid]);
    }
}

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.