Positive Con Sequences(UVALive 7295)

Description

Your younger sister is studying for an upcoming standardized test in mathematics. She needs practice
with the common style of problem in which the student is asked to fill in the missing value in a sequence
of numbers.
The vast majority of these problems feature either arithmetic sequences (where each number in the
sequence is formed by adding an integer constant to the prior number) or geometric sequences (where
each number in the sequence is formed by multiplying the prior number by an integer constant).
Write a program that will help your sister practice on this style of problem by allowing her to check
her answers on sample problems.

Input

Input will consist of one or more datasets.
Each dataset will be a single line containing 4 integers defining a sequence. One of these will be ‘-1’,
denoting the missing value. The remainder will be positive integers in the range 1…10,000, inclusive.
Other than the ‘-1’ placeholder value, the values will be in non-decreasing order.
End of input will be signaled by a line containing four ‘-1’ values.

Output

For each dataset, print one line of output.
If an integer in the range 1…1,000,000 inclusive exists that can be filled in to the missing value
position to create an arithmetic or geometric sequence, print that missing value.
If there is no such positive integer, print ‘-1’.

Sample Input

1 2 -1 4
2 4 8 -1
7 8 -1 21
5 -1 11 14
-1 2 4 6
-1 -1 -1 -1

Sample Output

3
16
-1
8
-1

题解:

纯暴力…就是给你4个数求一个非递减的等差/等比序列,注意数据范围!!!还有就是考虑等比时不能整除的情况,(推荐把c/b=b/a换成bb=ac)。

代码如下:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 27
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1001113
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;


ll a,b,c,d;
int main()
{
    while(cin>>a>>b>>c>>d)
    {
        if(a==-1&&a==b&&a==c&&a==d)
            break;
        if(a==-1)
        {
            int dd=d-c;
            int x=b-dd;
            if(x+dd==b&&x+dd+dd==c&&x+dd+dd+dd==d&&x>0&&x<=1000000)
                cout<<x<<endl;
            else
            {
                if(d%b==0&&d%c==0)
                {
                    dd=d/c;
                    x=b/dd;
                    if(x*dd==b&&x*dd*dd==c&&x*dd*dd*dd==d&&x>0&&x<=1000000)
                        cout<<x<<endl;
                    else
                        cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }
        if(b==-1)
        {
            int dd=d-c;
            int x=a+dd;
            if(x-dd==a&&x+dd==c&&x+dd+dd==d&&x>0&&x<=1000000)
                cout<<x<<endl;
            else
            {
                if(d%a==0&&d%c==0)
                {
                    dd=d/c;
                    x=a*dd;
                    if(x*dd==c&&x*dd*dd==d&&x/dd==a&&x>0&&x<=1000000)
                        cout<<x<<endl;
                    else
                        cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }
        if(c==-1)
        {
            int dd=b-a;
            int x=d-dd;
            if(x+dd==d&&x-dd==b&&x-dd-dd==a&&x>0&&x<=1000000)
                cout<<x<<endl;
            else
            {
                if(d%a==0&&d%b==0)
                {
                    dd=b/a;
                    x=d/dd;
                    if(x*dd==d&&x/dd/dd==a&&x/dd==b&&x>0&&x<=1000000)
                        cout<<x<<endl;
                    else
                        cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }
        if(d==-1)
        {
            int dd=b-a;
            int x=c+dd;
            if(x-dd==c&&x-dd-dd==b&&x-dd-dd-dd==a&&x>0&&x<=1000000)
                cout<<x<<endl;
            else
            {
                if(c%a==0&&c%b==0)
                {
                    dd=b/a;
                    x=c*dd;
                    if(x/dd==c&&x/dd/dd/dd==a&&x/dd/dd==b&&x>0&&x<=1000000)
                        cout<<x<<endl;
                    else
                        cout<<"-1"<<endl;
                }
                else
                    cout<<"-1"<<endl;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值