Cryptography Reloaded UVALive - 4353(BigInteger)

时间:2024-08-13 12:05:03

写写式子就出来了方程。。

然后解方程。。不过数很大。。用Java就好啦。。

就不贴呃的代码了。。。贴别人的。。https://blog.****.net/qq_15714857/article/details/49790693?locationNum=5&fps=1

import java.util.*;
import java.math.*; public class Main {
public static BigInteger sqrt( BigInteger n ) {
BigInteger L = BigInteger.ZERO , R = n ;
while ( L.compareTo(R) < 0 ) {
BigInteger M = L.add(R) ;
M = M.divide(BigInteger.valueOf(2)) ;
if ( M.multiply(M).compareTo(n) == 0 ) return M ;
else if ( M.multiply(M).compareTo(n) > 0 ) R = M.subtract(BigInteger.ONE);
else if(M.multiply(M).compareTo(n) < 0) L = M.add(BigInteger.ONE) ;
}
if ( L.multiply(L).compareTo(n) == 0 ) return L ;
else return BigInteger.valueOf(-1) ;
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger n , d , e ;
BigInteger p , q;
int kase = 1 ;
while(cin.hasNext()){
n = cin.nextBigInteger();
d = cin.nextBigInteger() ;
e = cin.nextBigInteger() ;
if ( n.compareTo(BigInteger.ZERO) == 0 && d.compareTo(BigInteger.ZERO) == 0 && e.compareTo(BigInteger.ZERO) == 0 ) break ; d = d.multiply(e);
d = d.subtract(BigInteger.ONE);
int k = 0 ;
while(true){
k++ ;
BigInteger t = d.mod(BigInteger.valueOf(k));
if ( t.compareTo(BigInteger.ZERO) > 0 ) continue ;
t = d.divide(BigInteger.valueOf(k)) ;
BigInteger b = (t.subtract(n)).subtract(BigInteger.ONE);
BigInteger delta = (b.multiply(b)).subtract(BigInteger.valueOf(4).multiply(n)) ;
if ( delta.compareTo(BigInteger.ZERO) >= 0 ) {
delta = sqrt(delta) ;
if( delta.compareTo(BigInteger.valueOf(-1)) == 0 ) continue ;
b = BigInteger.ZERO.subtract(b) ;
p = b.add(delta) ;
p = p.divide(BigInteger.valueOf(2)) ;
q = b.subtract(delta) ;
q = q.divide(BigInteger.valueOf(2)) ;
if ( p.multiply(q).compareTo(n) == 0 ) {
if ( p.compareTo(q) > 0 ) { t = p ; p = q ; q = t ;}
System.out.println("Case #"+ kase++ + ": " + p + " " + q);
break ;
} }
} }
cin.close();
}
}