The idea of solving this problem is to calculate the coefficients one by one from the largest to the smallest base.
Why can we design algorithms like this:
1. n = a0 + a1 * p0 + a2 * p0 * p1 + a3 * p0 * p1 * p2 + ..., it indicates that every n of the input can be completely decomposed, which is the premise.
2. The deal () function is available in the code (). It completes two functions. The first is to find the appropriate base, which is not greater than the input n, And the next base is greater than n. Because the coefficient of the base greater than n is positive
It is 0, so no processing is required.
3. Why can we use the second for loop in deal () to obtain the coefficients of each base?
This depends on the features of each base: 1, 2, 2*3, 2*3*5, 2*3*5*7, 2*3*5*7*11 .... For example, if n = 123, its maximum base is 2*3*5. We use n % (2*3*5) to get the value must be less than 7! Why?
If the value is greater than 7, the maximum base value must be 2*3*5*7! This proves that the initial processing is correct. Then, we assume that the k base is correct. We will process the k base
The subsequent data must be smaller than the k base. Assume that the k base is 2*3*5*7*11. After processing, n = (2*3*5*7) * (0---11 ). This shows that the processing of the second K-1 base must be correct!
4. For output, see output () function.
5. careful readers may find that array a has exceeded, but it does not affect the result, because it is not used in computing. The data entered by the question is only 32 bits.
AC code:
[Cpp]
# Include <iostream>
Using namespace std;
Const int prime [] = {,}; // The first 20 prime Numbers
_ Int64 a [21]; // stores each base, such as 1, 2*3, 2*3*5, 2*3*5*7...
Int B [21], rem; // The number of each base in the array. The input data of the rem record is no less than the previous base and smaller than the next base.
Void init () // calculate each base
{
A [0] = 1;
For (int I = 1; I <= 20; I ++)
{
A [I] = a [I-1] * prime [I-1];
}
}
Void deal (int n) // locate and calculate the number of each base
{
Int I;
For (I = 0; I <= 20; I ++)
{
If (a [I] <= n & a [I + 1]> n)
{
Rem = I;
Break;
}
}
Memset (B, 0, sizeof (B ));
For (I = rem; I> = 0; I --)
{
B [I] = n/a [I];
N = n % a [I];
}
}
Void output (int n) // output
{
Cout <n <"= ";
For (int I = 0; I <= rem; I ++)
{
If (B [I])
{
Cout <B [I];
For (int j = 0; j <I; j ++)
{
Cout <"*" <prime [j];
}
If (I! = Rem)
Cout <"+ ";
}
}
Cout <endl;
}
Int main ()
{
Init ();
Int n;
While (cin> n, n)
{
Deal (n );
Output (n );
}
Return 0;
}
From ON THE WAY