题意:给出一个大数数列,问是不是等比数列。
解法:拿java大数搞,注意全是0的情况也是Yes。我把公比用分数表示了,灰常麻烦,题解说只要判a[i - 1] * a[i + 1] == a[i] * a[i]就可以了,涨姿势了。
代码:
import java.math.*; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); while(cin.hasNext()) { int T = cin.nextInt(); while(T-- != 0) { boolean flag = false; BigInteger fenzi = BigInteger.ZERO; BigInteger fenmu = BigInteger.ZERO; boolean ans = true; BigInteger a0 = BigInteger.ZERO, a1 = BigInteger.ZERO; int n = cin.nextInt(); for(int i = 0; i < n; i++) { BigInteger x = cin.nextBigInteger(); if(!ans) continue; if(i == 0) { if(x.equals(BigInteger.ZERO)) { flag = true; } a0 = x; } else { if(flag) { if(!x.equals(BigInteger.ZERO)) ans = false; } else { if(x.equals(BigInteger.ZERO)) { ans = false; continue; } a1 = x; if(i == 1) { BigInteger r = a0.gcd(a1); fenzi = a1.divide(r); fenmu = a0.divide(r); } else { BigInteger r = a0.gcd(a1); if(!fenzi.equals(a1.divide(r))) ans = false; if(!fenmu.equals(a0.divide(r))) ans = false; } a0 = a1; } } } if(ans) System.out.println("Yes"); else System.out.println("No"); } } } }