poj3892 高精度

来源:互联网 发布:计算睡眠网络断开 编辑:程序博客网 时间:2024/05/21 17:03
RSA Factorization
Time Limit: 1000MS Memory Limit: 65536KTotal 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";}}}


	
				
		
原创粉丝点击