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]);
}
}