BestCoder Round #54 (div.2) HDU5329 Geometric Progression

49 篇文章 0 订阅
35 篇文章 0 订阅



Geometric Progression

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


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
  
  
4 1 0 3 1 1 1 3 1 4 2 5 16 8 4 2 1
 

Sample Output
  
  
Yes Yes No Yes
 

Source


出题人:判断是否为等比数列,可以检验对所有1 < i < n \quad A[i-1]*A[i+1]=A[i]*A[i]1<i<n


A[i1]A[i+1]=A[i]A[i] 是否都成立。


直接高精度也是资词的。比较简单的方法是选择若干质数


(保证乘积大于10^{200}10200),在模意义下检验。


复杂度O(k*n)O(kn)kk表示选取的质数个数。


import java.util.*;
import java.math.*;
import java.nio.file.*;
import java.io.*;
public class Main
{
    public static void main(String args[])
    {
        BigInteger []a = new BigInteger[111];
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        while(t-->0)
        {
            int n = cin.nextInt();
            int i,tmp = 0;
            for(i = 1; i <= n; ++i)
            {
                a[i] = cin.nextBigInteger();
                if(a[i].equals(BigInteger.ZERO))
                    tmp++;
            }
            int flag = 1;
            for(i = 2; i <= n-1; ++i)
                if(!a[i].multiply(a[i]).equals(a[i-1].multiply(a[i+1])) )
                {
                    flag = 0;
                    break;
                }
            if(tmp != 0 && tmp != n)
                flag = 0;
            if(flag == 1)
                System.out.println("Yes");
            else
                System.out.println("No");
        }
    }
}




1003:HDU5329 Geometric Progression



Geometric Progression

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


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
   
   
4 1 0 3 1 1 1 3 1 4 2 5 16 8 4 2 1
 

Sample Output
   
   
Yes Yes No Yes
 

Source


出题人:判断是否为等比数列,可以检验对所有1 < i < n \quad A[i-1]*A[i+1]=A[i]*A[i]1<i<n

A[i1]A[i+1]=A[i]A[i] 是否都成立。直接高精度也是资词的。

比较简单的方法是选择若干质数(保证乘积大于10^{200}10200),

在模意义下检验。复杂度O(k*n)O(kn)kk表示选取的质数个数。


import java.util.*;
import java.math.*;
import java.nio.file.*;
import java.io.*;
public class Main
{
    public static void main(String args[])
    {
        BigInteger []a = new BigInteger[111];
        Scanner cin = new Scanner(System.in);
        int t = cin.nextInt();
        while(t-->0)
        {
            int n = cin.nextInt();
            int i,tmp = 0;
            for(i = 1; i <= n; ++i)
            {
                a[i] = cin.nextBigInteger();
                if(a[i].equals(BigInteger.ZERO))
                    tmp++;
            }
            int flag = 1;
            for(i = 2; i <= n-1; ++i)
                if(!a[i].multiply(a[i]).equals(a[i-1].multiply(a[i+1])) )
                {
                    flag = 0;
                    break;
                }
            if(tmp != 0 && tmp != n)
                flag = 0;
            if(flag == 1)
                System.out.println("Yes");
            else
                System.out.println("No");
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值