D - Obtain Desired Expected Value

You are given n non-negative integers x1, x2, …, xn. You are also given a positive integer E. You have to find n non-negative real numbers p1, p2, …, pn such that p1 · x1 + p2 · x2 + p3 · x3 + … + pn · xn = E and p1 + p2 + … + pn = 1.

If it’s not possible to find n such numbers, output -1 instead.

Input

The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains two space-separated integers n and E.
The second line contains n space-separated integers x1, x2, …, xn.

Output

For each test case, print a single line containing n space-separated real numbers denoting the values of p1, p2, …, pn. If there is more than one possible solution, you may output any one. If there is no solution, print -1 instead.

When a solution exists, your answer will be considered correct if the absolute value of the expression p1 · x1 + p2 · x2 + p3 · x3 + … + pn · xn - E doesn’t exceed 10-6 and the value of |(p1 + p2 + p3 + … + pn) - 1| doesn’t exceed 10-6.

Constraints

1 ≤ T ≤ 105
1 ≤ n, E ≤ 103
1 ≤ xi ≤ 103
sum of n over all test cases doesn’t exceed 106

Example

Input

3
1 2
1
3 3
1 2 3
4 4
1 2 3 6

Output

-1
0 0 1.00
0 0 0.66666666666 0.33333333333

其实挺简单的一个题,所以自己开了这道题,结果自己不细心少考虑了一种情况,结果最后这道题没A掉,老老实实把锅背好,QAQ

思路

其实就是不定方程求解问题,求个特解就可以了,如果给定的序列中有E存在,就让序列中E的p为1,其余的p全为0.如果序列中没有E,则只要有一个比E大的,一个biE小的就可以按照公式求解,否则无解

题目链接

#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<algorithm>
#include<iomanip>
#include<cctype>
using namespace std;
map<int,int> in;
int num[1001];
int dis[1001];
int main()
{
    long double p1,p2;
    int T;
    cin>>T;
    while(T--)
    {
        in.clear();
        int _max=0;
        int _i,_j=-1;
        int n,e;
        cin>>n>>e;
        for(int i=0; i<n; i++)
        {
            cin>>num[i];
            if(_max<num[i])
            {
                _max=num[i];
                _i=i;
            }
            if(num[i]<e)
                _j=i;
            in[num[i]]=1;
            dis[num[i]]=i;
        }
        if(_max<e||_j==-1&&in[e]==0)
            cout<<"-1\n";
        else if(in[e]==1)
        {
            int flag=0;
            for(int i=0; i<n; i++)
            {
                if(i!=dis[e])
                {
                    if(!flag)
                    {
                        cout<<"0";
                        flag=1;
                    }
                    else
                        cout<<" 0";
                }
                else
                {
                    if(!flag)
                    {
                        cout<<"1.00";
                        flag=1;
                    }
                    else
                        cout<<" 1.00";
                }
            }
            cout<<endl;
        }
        else
        {
            int x1=_max;
            int x2=num[_j];
            p1=(long double)(x1-e)/((long double)(x1-x2));
            p2=1-p1;
            int flag=0;
            for(int i=0; i<n; i++)
            {
                if(i!=_i&&i!=_j)
                {
                    if(!flag)
                    {
                        cout<<"0";
                        flag=1;
                    }
                    else
                        cout<<" 0";
                }
                if(i==_i)
                {
                    if(!flag)
                    {
                        cout<<setprecision(11)<<p2;
                        flag=1;
                    }
                    else
                        cout<<setprecision(11)<<" "<<p2;
                }
                if(i==_j)
                {
                    if(!flag)
                    {
                        cout<<setprecision(11)<<p1;
                        flag=1;
                    }
                    else
                        cout<<setprecision(11)<<" "<<p1;
                }
            }
            cout<<endl;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值