Geometric Progression
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 316 Accepted Submission(s): 86
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.
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(T≤20)
which denotes the number of test cases.
For each test case, there is an positive integer n(1≤n≤100) 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 .
For each test case, there is an positive integer n(1≤n≤100) 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
4 1 0 3 1 1 1 3 1 4 2 5 16 8 4 2 1
Sample Output
Yes Yes No Yes
等比数列a[i - 1] * a[i + 1] == a[i] * a[i]的应用。
由于是大数操作,所以选择Java语言,运用相关函数即可。
AC代码:
import java.io.*;
import java.util.*;
import java.math.BigInteger;
class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
for(int cas = 0; cas < t; ++cas) {
int n = cin.nextInt();
BigInteger a = cin.nextBigInteger();
boolean allzero = true, nozero = true, ok = true;
if(a.equals(BigInteger.ZERO) == false) allzero = false;
else nozero = false;
if(n == 1) {
System.out.println("Yes");
continue;
}
BigInteger b = cin.nextBigInteger();
if(b.equals(BigInteger.ZERO) == false) allzero = false;
else nozero = false;
for(int i = 2; i < n; ++i) {
BigInteger x = cin.nextBigInteger();
if(x.equals(BigInteger.ZERO) == false) allzero = false;
else nozero = false;
if(ok)
if(a.multiply(x).equals(b.multiply(b)) == false) ok = false;
a = b;
b = x;
}
if(allzero) System.out.println("Yes");
else if(!nozero) System.out.println("No");
else if(!ok) System.out.println("No");
else System.out.println("Yes");
}
}
}