杭電ACM 1017 1018 1019

1017 .A Mathematical Curiosity

Problem Description
Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a < b < n and (a^2+b^2 +m)/(ab) is an integer.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input
You will be given a number of cases in the input. Each case is specified by a line containing the integers n and m. The end of input is indicated by a case in which n = m = 0. You may assume that 0 < n <= 100.

Output
For each case, print the case number as well as the number of pairs (a,b) satisfying the given property. Print the output for each case on one line in the format as shown below.

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

題目大意:給定m,n,且a,b滿足條件0 < a < b < n,求使(a^2+b^2 +m)/(ab) 爲整數的a,b的對數。

import java.util.Scanner;

public class Main{

    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int T=s.nextInt();
        for(int i=0;i<T;i++){
            int c=0;
            while(true){
                int n=s.nextInt();
                int m=s.nextInt();
                c++;
                if(n==0&&m==0)
                    break;
                int count=0;
                for(int a=1;a<n;a++){
                    for(int b=a+1;b<n;b++){
                        if((m+a*a)%b==0&&(m+b*b)%a==0){
                            if(((a*a+m)/b+b)%a==0){
                                count++;
                            }
                        }
                    }
                }
                System.out.println("Case "+c+": "+count);
            }
            if(i<T-1)
                System.out.println();
        }
    }
}

1018 .Big Number

Problem Description
In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of digits in the factorial of the number.

Input
Input consists of several lines of integer numbers. The first line contains an integer n, which is the number of cases to be tested, followed by n lines, one integer 1 ≤ n ≤ 107 on each line.

Output
The output contains the number of digits in the factorial of the integers appearing in the input.

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

題目大意:求n的階乘的位數。

分析:不要用java中的BigInteger去計算結果再輸出位數,雖然BigInteger很方便,但是在這裏也得不出結果的,用Strling公式。
[log10(n)]+1就是n的位數。

import java.util.Scanner;

public class Main {

    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        for(int i=0;i<n;i++){
            int d=s.nextInt();
            double bt=((0.5*(Math.log((2*3.14159265*d))))+
                    d*Math.log(d/Math.E))/Math.log(10);
            int c=(int)bt+1;
            System.out.println(c);
        }
    }
}

1019 .Least Common Multiple

Problem Description
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105.

Input
Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 … nm where m is the number of integers in the set and n1 … nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.

Output
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.

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

題目大意:求大數的最小公倍數。

分析:直接用BigInteger 類,這種類型的題目對於java編程來說簡直不需要任何思考。

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

public class Main {

    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int N=s.nextInt();
        for(int i=0;i<N;i++){
            int n=s.nextInt();
            BigInteger lcm=new BigInteger("1");
            for(int j=0;j<n;j++){
                BigInteger a=s.nextBigInteger();
                lcm=(a.multiply(lcm)).divide(a.gcd(lcm));
            }
            System.out.println(lcm.toString());
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章