Cryptography Reloaded UVALive - 4353(BigInteger)

本文介绍了一种使用Java解决包含大数的方程的方法。通过提供的代码示例,展示了如何利用BigInteger类来实现平方根计算及进一步解方程的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9328203.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值