HDU4447 Yuanfang, What Do You Think?

Source: Internet
Author: User

2012 Asia JinHua Regional Contest
Problem F. Yuanfang, What Do You Think?
Official solution report:
Manual or written brute-force programs calculate less n, and we can see that the decomposition formula of (x ^ N-1) contains the decomposition formula of (x ^ ni-1.
Among them, ni is the approximate number of n (except n itself ).
In addition to the decomposition formula (x ^ ni-1), (x ^ N-1) also contains a unique decomposition formula, recorded as P (n ).
So we get the decomposition of (x ^ N-1): (x ^ N-1) = P (n1) P (n2) P (n3)... P (n)
Among them, n1, n2, n3,... is the approximate number of n (except for n itself ).
From n = 1, P (1) = X-1.
All n, P (n) can be calculated using the preceding result: P (n) = (x ^ N-1)/P (n1)/P (n2) /P (n3 )/...
Finally, sort P (n1) P (n2) P (n3)... P (n.
Time Complexity: O (n ^ 2 * logn ).

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Const int MAXN = 1111;
 
Inline void printX (int x, int v, bool show)
{
If (0 = v)
{
Return;
}
If (show)
{
If (v> 0)
{
Printf ("+ ");
}
Else
{
Printf ("-");
}
}
If (0 = x)
{
Printf ("1 ");
}
Else
{
If (v! = 1 & v! =-1)
{
Printf ("% d", abs (v ));
}
Printf ("x ");
If (x> 1)
{
Printf ("^ % d", x );
}
}
}
 
Struct Polynomial
{
Int n;
Int value [MAXN];
 
Void output () const
{
PrintX (n, value [n], false );
For (int I = n-1; I> = 0; -- I)
{
PrintX (I, value [I], true );
}
}
} Intrinsic [MAXN], sets [MAXN];
Int setNumber;
Bool visit [MAXN];
 
Bool operator <(const Polynomial & a, const Polynomial & B)
{
If (a. n = B. n)
{
For (int I = a. n; I> = 0; -- I)
{
If (abs (a. value [I]) = abs (B. value [I])
{
If (a. value [I]! = B. value [I])
{
Return a. value [I] <B. value [I];
}
}
Else
{
Return abs (a. value [I]) <abs (B. value [I]);
}
}
}
Return a. n <B. n;
}
 
Polynomial operator/(Polynomial a, const Polynomial & B)
{
Polynomial c;
C. n = a. n-B. n;
For (int I = c. n; I> = 0; -- I)
{
If (a. n-B. n <I)
{
C. value [I] = 0;
Continue;
}
C. value [I] = a. value [a. n]/B. value [B. n];
Int d = a. n-B. n;
A. n-= B. n;
For (int j = B. n; j> = 0; -- j)
{
A. value [j + d]-= B. value [j] * c. value [I];
If (a. value [j + d])
{
A. n = max (a. n, j + d );
}
}
}
Return c;
}
 
Void makeSet (int n)
{
SetNumber = 0;
For (int I = 1; I <= n; ++ I)
{
If (0 = n % I)
{
If (! Visit [I])
{
Visit [I] = true;
Intrinsic [I]. n = I;
Intrinsic [I]. value [I] = 1;
Intrinsic [I]. value [0] =-1;
For (int j = 1; j <I; ++ j)
{
If (0 = I % j)
{
Intrinsic [I] = intrinsic [I]/intrinsic [j];
}
}
}
Sets [setNumber ++] = intrinsic [I];
}
}
Sort (sets, sets + setNumber );
}
 
Int main ()
{
Int n;
While (scanf ("% d", & n), n)
{
If (1 = n)
{
Printf ("X-1 \ n ");
}
Else
{
MakeSet (n );
For (int I = 0; I <setNumber; ++ I)
{
Printf ("(");
Sets [I]. output ();
Printf (")");
}
Printf ("\ n ");
}
}
Return 0;
}

 

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.