Hdu4002 Find the maximum Euler function

Source: Internet
Author: User

Hdu4002 Find the maximum Euler function
// Obtain N smaller than n and n/phi (x). If there are multiple N, the output is the smallest.
// Phi (x) = x * (1-1/p1) * (1-1/p2) * (1-1/p3 )....
// To maximize x/phi (x), even if (1-1/p1) * (1-1/p2) * (1-1/p3 ).... minimum
// P1, p2, p3 ,....
// All are prime factor, so we can enumerate the product of all prime factor,
// After the table is typed, find the number less than or equal to n.
# Include
# Include
# Include
Using namespace std;
Const int maxn = 1010;
Char str [maxn];
Int num [maxn] [maxn];
Int num_1 [maxn];
Int len [maxn];
Int isp [maxn];
Int prime [maxn];
Int set ()
{
Memset (isp, 0, sizeof (isp ));
Isp [1] = 1;
For (int I = 4; I <maxn; I + = 2)
Isp [I] = 1;
For (int I = 3; I <maxn; I ++)
{
If (isp [I]) continue;
For (int j = I * I; j <maxn; j + = I)
Isp [j] = 1;
}
Int j = 0;
For (int I = 1; I <maxn; I ++)
If (! Isp [I]) prime [++ j] = I;
Return j;
}
Bool cmp (int len_1, int len_2, int pos)
{
If (len_1> len_2) return 0;
If (len_2> len_1) return 1;
For (int I = len_1; I> 0; I --)
{
If (num_1 [I]> num [pos] [I])
Return 0;
If (num_1 [I] <num [pos] [I])
Return 1;
}
Return 0;
}
Void init ()
{
Int sum = set ();
Num [0] [1] = 1;
Len [0] = 1;
For (int I = 1; I ++)
{
Int c = 0;
Len [I] = len [I-1];
For (int j = 1; j <= len [I]; j ++)
{
Num [I] [j] = prime [I] * num [I-1] [j];
Num [I] [j] + = c;
C = num [I] [j]/10;
Num [I] [j] % = 10;
}
While (c)
{
Num [I] [++ len [I] = c % 10;
C/= 10;
}
If (len [I]> 100) break;
}
}
Int main ()
{
Init ();
Int T;
Scanf ("% d", & T );
While (T --)
{
Scanf ("% s", str );
Int len_1 = strlen (str );
For (int I = len_1-1; I> = 0; I --)
Num_1 [len_1-I] = str [I]-'0 ';
Int pos = 1;
While (! Cmp (len_1, len [pos], pos) pos ++;
For (int I = len [pos-1]; I> = 1; I --)
Printf ("% d", num [pos-1] [I]);
Puts ("");
}
}

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.