Mathematics - high precision

1.Integer Inquiry

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1047

Solution: to find the sum of large numbers, we can easily solve it by using BigInteger in java. However, we need to pay attention to the following format problems. Each output will have a blank line, and the last one will not be used

code:

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] arg)
    {
        Scanner in=new Scanner(System.in);
        int t;
        while(in.hasNext())
        {
            t=in.nextInt();
            while(t-->0)
            {
                BigInteger b=new BigInteger("0");
                while(in.hasNextBigInteger())
                {
                    BigInteger c=new BigInteger("0");
                    c=in.nextBigInteger();
                    if(!c.equals(BigInteger.ZERO))
                    {
                        b=b.add(c);
                    }
                    else
                    {
                        System.out.println(b);
                        if(t!=0)
                            System.out.println();
                        break;
                    }
                }
            }
        }
    }
}

 

2.Exponentiation

Title: https://vjudge.net/problem/POJ-1001/origin

Solution: This is the power of a floating-point number. You can complete the corresponding solution by using the pow method of BigDecimal in java.

There are special requirements for the output. After the decimal point, the meaningless 0 is removed. Use the stripTrailingZeros() method to remove all tail zeros, and then use toplanstring() to convert BigDecimal to String

After that, compare whether the first character of the converted string is "0". If it is 0, remove the number after the decimal point and use substring(1) to intercept the string

code:

import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
    public static void main(String[] arg)
    {
        Scanner in=new Scanner(System.in);
        while(in.hasNext())
        {
            BigDecimal a=in.nextBigDecimal();
            int k=in.nextInt();
            a=a.pow(k);
            String str=a.stripTrailingZeros().toPlainString();
            if(str.charAt(0)=='0')
                System.out.println(str.substring(1));
            else
                System.out.println(str);
        }
    }
}

 

3.How many Fibs?

Title: https://vjudge.net/problem/POJ-2413/origin

Idea: This is a way to find the number of Fibonacci numbers in a certain range. Because the range is 100 times of 10, we set the length of the large integer array to 10000, which is enough to include.

code:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    static BigInteger a[]=new BigInteger[10000];
    public static void main(String[] arg)
    {
        Scanner in=new Scanner(System.in);
        for(int i=0;i<10000;i++)
            a[i]=BigInteger.ZERO;
        a[0]=BigInteger.valueOf(1);
        a[1]=BigInteger.valueOf(2);
        for(int i=2;i<10000;i++)
            a[i]=a[i].add(a[i-1].add(a[i-2]));
        BigInteger b,c;
        while(true)
        {
            b=in.nextBigInteger();
            c=in.nextBigInteger();
            if(b.equals(BigInteger.ZERO)&&c.equals(BigInteger.ZERO))
                break;
            int res=0;
            for(int i=0;i<10000;i++)
            {
                if((a[i].compareTo(b)==1||a[i].compareTo(b)==0)&&(a[i].compareTo(c)==-1||a[i].compareTo(c)==0))
                    res++;
                if(a[i].compareTo(c)==1)
                    break;
            }
            System.out.println(res);
        }
    }
}

 

4.Segment

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5666

Solution: This is a way to find the number of integer points in a triangle area, excluding the points on the line. Through recursive analysis, it is known after finding the law that the number is: (q-1)*(q-2)/2%p

Because q is very large, we need to use large integers to solve it.

code:

import java.math.BigInteger;
import java.util.Scanner;

public class Main{
    public static void main(String[] arg)
    {
        Scanner in=new Scanner(System.in);
        int T=in.nextInt();
        while(T-->0)
        {
            BigInteger q=in.nextBigInteger();
            BigInteger p=in.nextBigInteger();
            BigInteger f=q.subtract(BigInteger.valueOf(2));
            BigInteger res=q.subtract(BigInteger.ONE).multiply(f).divide(BigInteger.valueOf(2));
            res=res.mod(p);
            System.out.println(res);
        }
    }
}

 

5.Problem B

Title: https://vjudge.net/problem/HDU-5686/origin

Solution: this should be a problem of Baidu. After simulating several sets of data, it turns out that it's actually a Fibonacci sequence, so it's very simple. For n < = 200, so ordinary longlong can't meet it. We need to use BigInteger

code:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] arg)
    {
        Scanner in=new Scanner(System.in);
        BigInteger a[]=new BigInteger[10000];
        for(int i=0;i<10000;i++)
            a[i]=BigInteger.ZERO;
        a[0]=BigInteger.valueOf(1);
        a[1]=BigInteger.valueOf(1);
        for(int i=2;i<10000;i++)
            a[i]=a[i].add(a[i-1].add(a[i-2]));
        while(in.hasNext())
        {
            int k=in.nextInt();
            if(k>=1&&k<=200)
                System.out.println(a[k]);
        }
    }
}

Tags: Java PHP

Posted on Sun, 21 Jun 2020 23:45:38 -0400 by tail