题解:P5646 ygg的题库

· · 题解

看到这道题,一眼不会做,然后看到如此之小的数据范围,直接乱搞过了。

思路

直接对答案数组中每一个数随机选取一个值,代回看是否满足要求。注意与答案相差 5 个以内都行。

代码

直接上代码。

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5; 
double p[N],a[N]; 
bool b[N];int n,m;
bool check(){
    int z=0;
    for(int i=1;i<=m;i++){
        double ans=0,q=1;
        for(int j=0;j<n;j++){
            ans+=p[j]*q;q*=a[i];
        }
        if((ans<=0&&b[i])||(ans>0&&!b[i])) z++;
        if(z>5) return false;
    }
    return true;
}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>a[i]>>b[i];
    }
    while(1){
        for(int i=0;i<n;i++) p[i]=(rand()%1000000000-500000000)/100000.0;
        if(check()) {for(int i=0;i<n;i++) cout<<fixed<<setprecision(5)<<p[i]<<" ";return 0;}
    }
}