hdu 5429 Geometric Progression(java 大数)

来源:互联网 发布:算法分析 概述 ppt 编辑:程序博客网 时间:2024/05/16 08:23

Geometric Progression

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 286    Accepted Submission(s): 76


Problem Description
Determine whether a sequence is a Geometric progression or not.

In mathematics, a **geometric progression**, also known as a **geometric sequence**, is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed, non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, ... is a geometric progression with common ratio 3. Similarly 10, 5, 2.5, 1.25, ... is a geometric sequence with common ratio 1/2.

Examples of a geometric sequence are powers rk of a fixed number r, such as 2k and 3k. The general form of a geometric sequence is

a, ar, ar2, ar3, ar4, 

where r ≠ 0 is the common ratio and a is a scale factor, equal to the sequence's start value.
 

Input
First line contains a single integer T(T20) which denotes the number of test cases. 

For each test case, there is an positive integer n(1n100) which denotes the length of sequence,and next line has n nonnegative numbers Ai which allow leading zero.The digit's length of Ai no larger than 100.
 

Output
For each case, output "Yes" or "No".
 

Sample Input
41031 1 131 4 2516 8 4 2 1
 

Sample Output
YesYesNoYes
 

判断一个数列是否为等比数列。在数学中,等比数列,是一个数列,这个数列中的第一项之后的每一项是前一项乘上一个固定的非零实数(我们称之为公比)。比如,数列 2, 6, 18, 54, ... 是一个公比为3的等比数列。 类似的,10,5,2.5,1.25,...是一个公比为0.5的等比数列。等比数列的一般形式是:a,ar,ar^2,ar^3,ar^4,...a,ar,ar2,ar3,ar4,...其中r!=0,r为公比,a是首项(a可以是任何实数)

题解:(1)存在0的时候要全都是0,才能是Yes,存在0但不全是0则是No

             (2)不存在0,则A[i-1]*A[i+1]==A[i]*A[i]是否全都成立

import java.util.*;import java.math.*;public class Main {private static Scanner in;public static void main(String[] args) {in = new Scanner(System.in);BigInteger a, b, fz, fm, tt, fir;int t;t = in.nextInt();for (int i = 1; i <= t; i++) {int n;n = in.nextInt();int f = 1; // 等比int ff = 1; // 除第一位,其他都是0int f3 = 1; // 不存在0a = in.nextBigInteger();if (a.equals(BigInteger.ZERO) == false) {ff = 0;} elsef3 = 0;fir = a;if (n == 1) {System.out.println("Yes");continue;}b = in.nextBigInteger();if (b.equals(BigInteger.ZERO) == false) {ff = 0;} else {f3 = 0;}for (int j = 2; j < n; j++) {tt = in.nextBigInteger();if (tt.equals(BigInteger.ZERO) == false) {ff = 0;} else {f3 = 0;}if (f == 1) {if (a.multiply(tt).equals(b.multiply(b)) == false) {f = 0;}}a = b;b = tt;}if (ff == 1)System.out.println("Yes");// 全是0else if (f3 == 0)System.out.println("No"); // 有0else if (f == 0)System.out.println("No");elseSystem.out.println("Yes");}}}


0 0
原创粉丝点击