poj3892 高精度

來源:互聯網
上載者:User
RSA Factorization
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 471   Accepted: 124

Description

The positive integer n is given. It is known that n = p * q, where p and q are primes, q <= p and |q - kp| <= 105 for some given positive integer k. You must find p and q.

Input

Each line contains integers n (1 < n < 10120) and k (0 < k < 108).

Output

For each pair of numbers n and k print in separate line the product p * q such that q <= p.

Sample Input

35 1 121 1 1000730021 9

Sample Output

5 * 7 11 * 11 10007 * 100003
java大數功能很強大啊,有木有!可能,這裡,是我認為,java比c++,好用的地方吧!想不通啊,為什麼用c++寫就非要逾時,用java寫這麼容易就過了,java 是個好東西啊!題意不想理解錯了,其實,就是,要把一個n可以分成兩個數,把這兩個數求出來,而且一定,是只有一個組合,沒想到,這題這麼坑啊!無語,但是學學java是個好東西啊!
import java.math.BigInteger;import java.util.Scanner;public class Main {    static BigInteger n;static BigInteger k;public static void main(String[] args) {Scanner cin = new Scanner(System.in);//相當於scanfBigInteger one = BigInteger.ONE;BigInteger two = BigInteger.valueOf(2);//2要轉換BigInteger zero = BigInteger.ZERO;BigInteger l,r,tmp,mid;mid=zero;while(cin.hasNext())//相當於!=EOF{n = cin.nextBigInteger();//相當於scanfk = 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=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);}}}找了一些函數JAVA大數處理(BigInteger,BigDecimal)這兩個類都在java.math.*包中,因此每次必須在開頭處引用該包。Ⅰ基本函數:1.valueOf(parament); 將參數轉換為制定的類型比如 int a=3;BigInteger b=BigInteger.valueOf(a);則b=3;String s=”12345”;BigInteger c=BigInteger.valueOf(s);則c=12345;2.add(); 大整數相加BigInteger a=new BigInteger(“23”);BigInteger b=new BigInteger(“34”);a. add(b);3.subtract(); 相減4.multiply(); 相乘5.divide(); 相除取整6.remainder();取餘7.pow(); a.pow(b)=a^b//b不能為大數8.gcd(); 最大公約數9.abs(); 絕對值10.negate();取反數11.mod(); a.mod(b)=a%b=a.remainder(b);13.punlic int comareTo();14.boolean equals(); 是否相等15.BigInteger建構函式:一般用到以下兩種:BigInteger(String val);將指定字串轉換為十進位表示形式;BigInteger(String val,int radix);將指定基數的BigInteger的字串表示形式轉換為BigIntegerⅡ.基本常量:A=BigInteger.ONE 1B=BigInteger.TEN 10C=BigInteger.ZERO 0Ⅲ.基本操作1. 讀入:用Scanner類定義對象進行控制台讀入,Scanner類在java.util.*包中Scanner cin=new Scanner(System.in);// 讀入while(cin.hasNext()) //等同於!=EOF{int n;BigInteger m;n=cin.nextInt(); //讀入一個int;m=cin.BigInteger();//讀入一個BigInteger;System.out.print(m.toString());}Ⅳ.運用四則預算:import java.util.Scanner;import java.math.*;import java.text.*;public class Main {public static void main(String args[]){Scanner cin = new Scanner ( System.in );BigInteger a,b;int c;char op;String s;while( cin.hasNext() ){a = cin.nextBigInteger();s = cin.next();op = s.charAt(0);if( op == '+'){b = cin.nextBigInteger();System.out.println(a.add(b));}else if( op == '-'){b = cin.nextBigInteger();System.out.println(a.subtract(b));}else if( op == '*'){b = cin.nextBigInteger();System.out.println(a.multiply(b));}else{BigDecimal a1,b1,eps;String s1,s2,temp;s1 = a.toString();a1 = new BigDecimal(s1);b = cin.nextBigInteger();s2 = b.toString();b1 = new BigDecimal(s2);c = cin.nextInt();eps = a1.divide(b1,c,4);//System.out.println(a + " " + b + " " + c);//System.out.println(a1.doubleValue() + " " + b1.doubleValue() + " " + c);System.out.print( a.divide(b) + " " + a.mod(b) + " ");if( c != 0){temp = "0.";for(int i = 0; i < c; i ++) temp += "0";DecimalFormat gd = new DecimalFormat(temp);System.out.println(gd.format(eps));}else System.out.println(eps);}}}import java.math.BigInteger;import java.util.Scanner;public class Main {    public static void main(String[] args) {BigInteger n;BigInteger k;BigInteger c;Scanner cin = new Scanner(System.in);//相當於scanfBigInteger one = BigInteger.ONE;BigInteger two = BigInteger.valueOf(2);//2要轉換BigInteger zero = BigInteger.ZERO;BigInteger l,r,tmp,mid;mid=zero;double a;while(cin.hasNext())//相當於!=EOF{n = cin.nextBigInteger();//相當於scanfk = cin.nextBigInteger();a = cin.nextDouble();System.out.printf("%.3f\n",a);//double float 都要用f而不是lfSystem.out.println(n+""+k);c=n.add(k);System.out.println("add"+c);c=n.subtract(k);System.out.println("substrack"+c);c=n.multiply(k);System.out.println("multiply"+c);c=n.divide(k);System.out.println("divide"+c);c=n.remainder(k);System.out.println("remainder"+c);c=n.pow(2);//這裡不能是大數System.out.println("pow"+c);c=n.gcd(k);System.out.println("gcd"+c);c=n.abs();System.out.println("abs"+c);c=n.negate();System.out.println("negate"+c);System.out.printf("compare%d\n",n.compareTo(k));//System.out.printf("equal%d\n", n.equals(k));String mm="23232";}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.