hdu 2466 Cryptography Reloaded

题目:hdu 2466 Cryptography Reloaded

思路:这题就是给出RSA加密的公钥和私钥求初始的两个质数。

开始的时候被几个同余方程给迷惑了,不知从何下手。

然后看了这里的 解题报告 。

然后联立方程组得到:

(p-1)*(q-1)=t   p*q=n

delta=sqrt((t-n-1)^2-4*n)

p=(n+1-t+sqrt(delta))/2;

q=(n+1-t-sqrt(delta))/2;

一直找到满足这样的式子能够整除能够开方的值(竟然不需要判素数)输出就行


import java.math.BigInteger;
import java.util.*;
public class Main{
    public static BigInteger sqrt(BigInteger n)
    {
        BigInteger l=BigInteger.ZERO,r=n,mid,tmp;
        while(l.compareTo(r)<0)
        {
            mid=(l.add(r)).divide(BigInteger.valueOf(2));
            tmp=mid.multiply(mid);
            if(tmp.compareTo(n)>0)
                r=mid.subtract(BigInteger.ONE);
            else if(tmp.compareTo(n)<0)
                l=mid.add(BigInteger.ONE);
            else
                return mid;
        }
        return l;
    }

    public static boolean can_sqrt(BigInteger n) {
        BigInteger tmp=sqrt(n);
        tmp=tmp.multiply(tmp);
        if(tmp.compareTo(n)==0)
            return true;
        return false;
    }

    public static void main(String args[])
    {
        int cas=0;
        Scanner cin=new Scanner(System.in);
        BigInteger n,d,e;
        while(cin.hasNext())
        {
            n=cin.nextBigInteger();
            d=cin.nextBigInteger();
            e=cin.nextBigInteger();
            if(n.compareTo(BigInteger.ZERO)==0 && e.compareTo(BigInteger.ZERO)==0 && d.compareTo(BigInteger.ZERO)==0)
            {
                break;
            }
            d=d.multiply(e);
            d=d.subtract(BigInteger.ONE);
            int i=0;
            while(true)
            {
                i++;
                //  pq  = n
                // de-1 = k*(p-1)*(q-1)      枚举(p-1)(q-1)=tmp
                BigInteger tmp=d.mod(BigInteger.valueOf(i));
                if(tmp.compareTo(BigInteger.ZERO)>0)
                    continue;
                tmp=d.divide(BigInteger.valueOf(i));
                BigInteger delta=((n.subtract(tmp.add(BigInteger.ONE))).pow(2)).subtract(tmp.multiply(BigInteger.valueOf(4)));
                if(can_sqrt(delta)==true)
                {
                    delta=sqrt(delta);
                    BigInteger p,q;
                    p=((n.subtract(tmp.subtract(BigInteger.ONE))).subtract(delta)).divide(BigInteger.valueOf(2));
                    q=((n.subtract(tmp.subtract(BigInteger.ONE))).add(delta)).divide(BigInteger.valueOf(2));
                    if(p.compareTo(q)>0)
                    {
                        BigInteger cnt=p;
                        p=q;
                        q=cnt;
                    }
                    System.out.println("Case #"+ ++cas +": "+p+" "+q);
                    break;

                }

            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值