码迷,mamicode.com
首页 > 其他好文 > 详细

Cryptography Reloaded UVALive - 4353(BigInteger)

时间:2018-07-18 14:26:54      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:public   scanner   detail   reload   art   pareto   asn   pre   live   

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

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

就不贴呃的代码了。。。贴别人的。。https://blog.csdn.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();
    }
}

 

Cryptography Reloaded UVALive - 4353(BigInteger)

标签:public   scanner   detail   reload   art   pareto   asn   pre   live   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9328203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!