HDU4392 Maximum Number Of Divisors

Source: Internet
Author: User

2012 Multi-University Training Contest 10
1003 questions
Given P, find a number K that is less than or equal to P, so that K has the largest number of factors and K is as small as possible.
Searchsearch, each node contains the following records:
K: current number
F: number of factors
N: quantity of prime numbers
A []: quantity of each Prime Number
Yi Zhi (note: the true meaning is that I don't know how to prove it). The final result must use the minimum prime numbers.
Therefore, assume that the prime number is prime [] and the coordinate starts from 0.
Add the p operation for the existing Prime Number:
{K, F, N, A []} → {K * prime [p], F/(A [p] + 1) * (A [p] + 2 ), n, A [p] + 1}
Add a new prime number operation:
{K, F, N, A []} → {K * prime [N], F * 2, N + 1, A [N] = 1}

At the same time, the larger K can be deleted when F is the same.
Here, map is used to record the ing from F to a node, and only the minimum K is retained.

The answer has a monotonous relationship with P, so you can read all P at the beginning and get all the answers after a wide search.

[Java]
Import java. io. BufferedInputStream;
Import java. math. BigInteger;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. Collections list;
Import java. util. List;
Import java. util. Map;
Import java. util. Queue;
Import java. util. collections;
 
Class Node
{
Private static final int MAXP = 60;
 
Public BigInteger K;
Public long F;
Public int N;
Public int [];
 
Public Node ()
{
K = BigInteger. ZERO;
A = new int [MAXP];
}
}
 
Public class Main
{
Private static final int MAXIP = 250;
Private static final int MAXP = 60;
 
Private static BigInteger [] prime;
 
Private static void init ()
{
Boolean [] isPrime = new boolean [MAXIP];
For (int I = 0; I <MAXIP; ++ I)
{
IsPrime [I] = true;
}
IsPrime [0] = isPrime [1] = false;
For (int I = 4; I <MAXIP; I + = 2)
{
IsPrime [I] = false;
}
For (int I = 3; I <MAXIP; I + = 2)
{
For (int j = 3; I * j <MAXIP; j + = 2)
{
IsPrime [I * j] = false;
}
}
Prime = new BigInteger [MAXP];
For (int I = 0, j = 0; I <MAXIP; ++ I)
{
If (isPrime [I])
{
Prime [j ++] = BigInteger. valueOf (I );
}
}
}
 
Public static void main (String args [])
{
Init ();
List <BigInteger> P = new ArrayList <BigInteger> ();
BigInteger MP = BigInteger. ZERO;
List <Node> ans = new ArrayList <Node> ();
Partition cin = new partition (new BufferedInputStream (System. in ));
While (cin. hasNext ())
{
BigInteger temp = new BigInteger (cin. nextLine ());
P. add (temp );
If (temp. compareTo (MP) = 1)
{
MP = temp;
}
Ans. add (new Node ());
}
Map <Long, BigInteger> map = new HashMap <Long, BigInteger> ();
Queue <Node> queue = new Queue list <Node> ();
Node origin = new Node ();
Origin. K = BigInteger. ONE;
Origin. F = 1;
Origin. N = 0;
Queue. add (origin );
Map. put (origin. F, origin. K );
While (! Queue. isEmpty ())
{
Node u = queue. peek ();
Queue. remove ();
BigInteger compare = map. get (u. F );
If (compare! = Null)
{
If (compare. compareTo (u. K) =-1)
{
Continue;
}
}
For (int I = 0; I <P. size (); ++ I)
{
If (u. K. compareTo (P. get (I) <= 0)
{
If (u. F> ans. get (I). F)
{
Ans. get (I). F = u. F;
Ans. get (I). K = u. K;
}
Else if (u. F = ans. get (I). F)
{
If (u. K. compareTo (ans. get (I). K) =-1)
{
Ans. get (I). K = u. K;
}
}
}
}
For (int I = 0; I <u. N; ++ I)
{
Node v = new Node ();
V. K = u. K. multiply (prime [I]);
If (v. K. compareTo (MP) <= 0)
{
V. F = u. F/(u. A [I] + 1) * (u. A [I] + 2 );
V. N = u. N;
For (int j = 0; j <u. N; ++ j)
{
V. A [j] = u. A [j];
}
++ V. A [I];
Boolean flag = true;
Compare = map. get (v. F );
If (compare! = Null)
{
If (compare. compareTo (v. K) <= 0)
{
Flag = false;
}
Else
{
Map. remove (v. F );
}
}
If (flag)
{
Queue. add (v );
Map. put (v. F, v. K );
}
}
}
Node v = new Node ();
V. K = u. K. multiply (prime [u. N]);
If (v. K. compareTo (MP) <= 0)
{
V. F = u. F * 2;
V. N = u. N + 1;
For (int I = 0; I <u. N; ++ I)
{
V. A [I] = u. A [I];
}
++ V. A [u. N];
Boolean flag = true;
Compare = map. get (v. F );
If (compare! = Null)
{
If (compare. compareTo (v. K) <= 0)
{
Flag = false;
}
Else
{
Map. remove (v. F );
}
}
If (flag)
{
Queue. add (v );
Map. put (v. F, v. K );
}
}
}
For (int I = 0; I <ans. size (); ++ I)
{
System. out. println (ans. get (I). K. toString () + "" + ans. get (I). F );
}
}
}

Related Article

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.