RSA factorization -- poj3892 -- mathematical problem + large number

Source: Internet
Author: User

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);}}}



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.