ACM 2008 杭州的C题
试试看java的BigInteger的
java写出来好丑哇
直接枚举k,不用判p, q的素数
Code
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static BigInteger sqrt(BigInteger a)
{
BigInteger l = BigInteger.ONE;
BigInteger h = a;
while (l.compareTo(h)<0)
{
BigInteger m = l.add(h).divide(BigInteger.valueOf(2));
int tmp = m.pow(2).compareTo(a);
if (tmp == 0)
{
// System.out.println("sqrt return "+a.toString()+' '+m.toString());
return m;
}
if (tmp < 0) l = m.add(BigInteger.ONE);
else
h = m.subtract(BigInteger.ONE);
}
return l;
}
public static void main(String args[]) throws Exception
{
Scanner cin=new Scanner(new BufferedInputStream(System.in));
int ctr = 0;
while (cin.hasNextBigDecimal())
{
BigInteger n = cin.nextBigInteger();
BigInteger d = cin.nextBigInteger();
BigInteger e = cin.nextBigInteger();
if (n.compareTo(BigInteger.ZERO)==0)
return ;
//System.out.println(n.toString()+ ' '+ d.toString()+ ' '+ e.toString());
for (int kk = 1; kk < 100; kk++)
{
BigInteger k = BigInteger.valueOf(kk);
// System.out.println("k = "+k.toString());
BigInteger tmp = d.multiply(e).subtract(BigInteger.ONE).mod(k);
if (tmp.compareTo(BigInteger.ZERO) == 0)
{
BigInteger b = d.multiply(e);
// System.out.println("b = "+b.toString());
b = b.subtract(BigInteger.ONE);
b = b.divide(k);
// System.out.println("b = "+b.toString());
b = b.subtract(n).subtract(BigInteger.ONE);
b = BigInteger.ZERO.subtract(b);
// System.out.println("b = "+b.toString());
if (b.compareTo(BigInteger.ZERO) >= 0)
{
BigInteger delt = b.pow(2).subtract(n.multiply(BigInteger.valueOf(4)));
BigInteger ddelt = sqrt(delt);
// System.out.println("delt = "+delt.toString()+"ddelt = "+ddelt.toString());
if (ddelt.pow(2).compareTo(delt) == 0)
{
BigInteger p = b.subtract(sqrt(delt)).divide(BigInteger.valueOf(2));
BigInteger q = b.add(sqrt(delt)).divide(BigInteger.valueOf(2));
if (e.gcd(p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE))).compareTo(BigInteger.ONE) == 0)
{
ctr += 1;
System.out.print("Case #");
System.out.print(ctr);
System.out.println(": "+p.toString()+' '+q.toString());
break;
}
}
}
}
}
}
}
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static BigInteger sqrt(BigInteger a)
{
BigInteger l = BigInteger.ONE;
BigInteger h = a;
while (l.compareTo(h)<0)
{
BigInteger m = l.add(h).divide(BigInteger.valueOf(2));
int tmp = m.pow(2).compareTo(a);
if (tmp == 0)
{
// System.out.println("sqrt return "+a.toString()+' '+m.toString());
return m;
}
if (tmp < 0) l = m.add(BigInteger.ONE);
else
h = m.subtract(BigInteger.ONE);
}
return l;
}
public static void main(String args[]) throws Exception
{
Scanner cin=new Scanner(new BufferedInputStream(System.in));
int ctr = 0;
while (cin.hasNextBigDecimal())
{
BigInteger n = cin.nextBigInteger();
BigInteger d = cin.nextBigInteger();
BigInteger e = cin.nextBigInteger();
if (n.compareTo(BigInteger.ZERO)==0)
return ;
//System.out.println(n.toString()+ ' '+ d.toString()+ ' '+ e.toString());
for (int kk = 1; kk < 100; kk++)
{
BigInteger k = BigInteger.valueOf(kk);
// System.out.println("k = "+k.toString());
BigInteger tmp = d.multiply(e).subtract(BigInteger.ONE).mod(k);
if (tmp.compareTo(BigInteger.ZERO) == 0)
{
BigInteger b = d.multiply(e);
// System.out.println("b = "+b.toString());
b = b.subtract(BigInteger.ONE);
b = b.divide(k);
// System.out.println("b = "+b.toString());
b = b.subtract(n).subtract(BigInteger.ONE);
b = BigInteger.ZERO.subtract(b);
// System.out.println("b = "+b.toString());
if (b.compareTo(BigInteger.ZERO) >= 0)
{
BigInteger delt = b.pow(2).subtract(n.multiply(BigInteger.valueOf(4)));
BigInteger ddelt = sqrt(delt);
// System.out.println("delt = "+delt.toString()+"ddelt = "+ddelt.toString());
if (ddelt.pow(2).compareTo(delt) == 0)
{
BigInteger p = b.subtract(sqrt(delt)).divide(BigInteger.valueOf(2));
BigInteger q = b.add(sqrt(delt)).divide(BigInteger.valueOf(2));
if (e.gcd(p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE))).compareTo(BigInteger.ONE) == 0)
{
ctr += 1;
System.out.print("Case #");
System.out.print(ctr);
System.out.println(": "+p.toString()+' '+q.toString());
break;
}
}
}
}
}
}
}
Code