hdu 5278 Geometric Progression 高精度

Geometric Progression

Time Limit: 1 Sec  

Memory Limit: 256 MB

题目连接

http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=628&pid=1001

Description

判断一个数列是否为等比数列。

在数学中,等比数列,是一个数列,这个数列中的第一项之后的每一项是前一项乘上一个固定的非零实数(我们称之为公比)。比如,数列 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可以是任何实数)

Input

第一行一个整数T,表示数据组数。T \leq 20T20
对于每一个组,第一行一个整数n(1 \leq n \leq 100)n(1n100),接下来第二行nn个数允许前导零的非负整数A_iAi​​,表示数列。保证A_iAi​​位数\leq 100100。

Output

对于每一个组,输出Yes或者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

HINT

 

题意

给你n个数,判断这n个数是否非等比数列

题解:

等比数列 a[i]*a[i]==a[i-1]*a[i+1]就好了

注意这道题全是0也是等比数列……(吐槽

要高精度,注意,还有前导0

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 151
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff;   //нчочfС
const int inf=~0u>>1;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//**************************************************************************************

const int MaxBignLength = 400; //大数长度
struct bign
{
    int len, s[MaxBignLength];
    bign ()
    {
        memset(s, 0, sizeof(s));
        len = 1;
    }
    bign (int num) { *this = num; }
    bign (const char *num) { *this = num; }
    bign operator = (const int num)
    {
        char s[MaxBignLength];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    bign operator = (const char *num)
    {
        for(int i = 0; num[i] == '0'; num++) ;  //去前导0
        len = strlen(num);
        for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0';
        return *this;
    }
    bign operator + (const bign &b) const //+
    {
        bign c;
        c.len = 0;
        for(int i = 0, g = 0; g || i < max(len, b.len); i++)
        {
            int x = g;
            if(i < len) x += s[i];
            if(i < b.len) x += b.s[i];
            c.s[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    bign operator += (const bign &b)
    {
        *this = *this + b;
        return *this;
    }
    void clean()
    {
        while(len > 1 && !s[len-1]) len--;
    }
    bign operator * (const bign &b) //*
    {
        bign c;
        c.len = len + b.len;
        for(int i = 0; i < len; i++)
        {
            for(int j = 0; j < b.len; j++)
            {
                c.s[i+j] += s[i] * b.s[j];
            }
        }
        for(int i = 0; i < c.len; i++)
        {
            c.s[i+1] += c.s[i]/10;
            c.s[i] %= 10;
        }
        c.clean();
        return c;
    }
    bign operator *= (const bign &b)
    {
        *this = *this * b;
        return *this;
    }
    bign operator - (const bign &b)
    {
        bign c;
        c.len = 0;
        for(int i = 0, g = 0; i < len; i++)
        {
            int x = s[i] - g;
            if(i < b.len) x -= b.s[i];
            if(x >= 0) g = 0;
            else
            {
                g = 1;
                x += 10;
            }
            c.s[c.len++] = x;
        }
        c.clean();
        return c;
    }
    bign operator -= (const bign &b)
    {
        *this = *this - b;
        return *this;
    }
    bign operator / (const bign &b)
    {
        bign c, f = 0;
        for(int i = len-1; i >= 0; i--)
        {
            f = f*10;
            f.s[0] = s[i];
            while(f >= b)
            {
                f -= b;
                c.s[i]++;
            }
        }
        c.len = len;
        c.clean();
        return c;
    }
    bign operator /= (const bign &b)
    {
        *this  = *this / b;
        return *this;
    }
    bign operator % (const bign &b)
    {
        bign r = *this / b;
        r = *this - r*b;
        return r;
    }
    bign operator %= (const bign &b)
    {
        *this = *this % b;
        return *this;
    }
    bool operator < (const bign &b)
    {
        if(len != b.len) return len < b.len;
        for(int i = len-1; i >= 0; i--)
        {
            if(s[i] != b.s[i]) return s[i] < b.s[i];
        }
        return false;
    }
    bool operator > (const bign &b)
    {
        if(len != b.len) return len > b.len;
        for(int i = len-1; i >= 0; i--)
        {
            if(s[i] != b.s[i]) return s[i] > b.s[i];
        }
        return false;
    }
    bool operator == (const bign &b)
    {
        return !(*this > b) && !(*this < b);
    }
    bool operator != (const bign &b)
    {
        return !(*this == b);
    }
    bool operator <= (const bign &b)
    {
        return *this < b || *this == b;
    }
    bool operator >= (const bign &b)
    {
        return *this > b || *this == b;
    }
    string str() const
    {
        string res = "";
        for(int i = 0; i < len; i++) res = char(s[i]+'0') + res;
        return res;
    }
};

istream& operator >> (istream &in, bign &x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream& operator << (ostream &out, const bign &x)
{
    out << x.str();
    return out;
}

int n;
bign p[maxn];
int main()
{
    int Case;
    scanf("%d",&Case);
    while(Case--)
    {
        int ffff=0;
        scanf("%d",&n);
        for(int i = 0 ; i < n ; ++ i)
            cin >>p[i];
        if(n == 1)
        {
            printf("Yes\n");
            ffff=1;
        }
        if(ffff)
            continue;
        int flag = 0;
        int flag2 = 0;
        for(int i = 0 ; i < n ; ++ i)
        {
            if(p[i].len) flag2=1;
            else flag = 1;
        }
        if(flag)
        {
            if(flag2) printf("No\n");
            else printf("Yes\n");
            ffff=1;
        }
        if(ffff)
            continue;
        for(int i = 1 ; i < n-1 ; ++ i)
        {
            bign kkk1 = p[i]*p[i];
            bign kkk2 = p[i-1]*p[i+1];
            if (kkk1 != kkk2)
            {
                printf("No\n");
                ffff=1;
                break;
            }
        }
        if(ffff)
            continue;
        printf("Yes\n");
    }
    return 0;
}

 

posted @ 2015-09-05 22:22  qscqesze  阅读(375)  评论(3编辑  收藏  举报