Question address: http://poj.org/problem? Id = 3892
Give you a number N and a K
Where N can be decomposed by two prime numbers p and q.
A condition is added: | Q-KP | <= 105
Ask for p and q
Solution:
Q <= P and | Q-KP | <= 105
Therefore, KP-q <= 105
Then kpq-Q * q <= 10 ^ 5 * q
Because n = PQ
So kn-Q * q <= 10 ^ 5 * q
So Q ^ 2> = kn-10 ^ 5 * q, get this formula can be seen in fact Q is in the vicinity of SQRT (kN), then we enumerate near him just fine
With Java, you can easily get rid of it.
import java.math.BigInteger;import java.util.Scanner;public class Main {/** * @param args */static BigInteger n;static BigInteger k;public static void main(String[] args) {// TODO Auto-generated method stubScanner cin = new Scanner(System.in);BigInteger one = BigInteger.ONE;BigInteger two = BigInteger.valueOf(2);BigInteger zero = BigInteger.ZERO;BigInteger l,r,tmp,mid;mid=zero;while(cin.hasNext()){n = cin.nextBigInteger();k = cin.nextBigInteger();k = k.multiply(n);l = zero;r = k;while(l.compareTo(r)<=0){mid = l.add(r).divide(two);tmp = mid.multiply(mid);if(tmp.compareTo(k) == 0)break;if(tmp.compareTo(k) < 0)l=mid.add(one);elser=mid.subtract(one);}BigInteger p,q;p = mid;q = mid;while(true){if(n.mod(p).equals(zero) && !p.equals(one) && !p.equals(n)){q = n.divide(p);break;}if(n.mod(q).equals(zero)&& !q.equals(one) && !q.equals(n)){p = n.divide(q);break;}p = p.add(one);q = q.subtract(one);}if(p.compareTo(q)>0){tmp = p;p = q;q = tmp;}System.out.println(p+" * "+q);}}}