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;
}
### 使用 `pad_sequences` 函数 在 Python 的 Keras 中,`pad_sequences` 是一个非常有用的工具,主要用于处理不同长度的序列数据。这使得模型能够接受固定大小的输入矩阵,从而更高效地训练神经网络。 #### 参数解释 - **sequences**: 输入的数据列表,通常是由整数索引构成的嵌套列表。 - **maxlen (可选)**: 输出序列的最大长度。如果给定,则较长的序列会被截断;较短的则被填充至此长度[^1]。 - **dtype (默认 'int32')**: 返回数组的数据类型。 - **padding ('pre' 或 'post')**: 填充位置的选择——是在开头还是结尾添加填充项[^5]。 - **truncating ('pre' 或 'post')**: 当需要裁剪序列时,决定是从前面还是后面开始删除多余部分[^4]。 - **value (默认 0.0)**: 用来填补不足最大长度的部分所使用的数值[^2]。 #### 实际应用案例 下面是一个简单的例子来展示如何利用 `pad_sequences` 对不均匀长度的文本序列进行标准化: ```python import numpy as np from tensorflow.keras.preprocessing.sequence import pad_sequences # 定义一些样本序列 sample_sequences = [ [7, 8], [9, 10, 11, 12], [13] ] # 调用 pad_sequences 进行填充/截取 padded_data = pad_sequences(sample_sequences, maxlen=4, dtype='float', padding='post', truncating='post', value=-1) print(padded_data) ``` 这段代码会输出如下所示的结果: ``` [[ 7. 8. -1. -1.] [ 9. 10. 11. 12.] [13. -1. -1. -1.]] ``` 在这个实例中,所有原始序列都被调整到了相同的长度(即4),并且对于那些原本长度不够的情况,在其末端补充了指定值 `-1.` 来达到目标长度的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值