Codeforces Round #730 (Div. 2)部分题解(A-C)

A. Exciting Bets
题意:给出两个整数,定义操作为使两个整数同时加一或减一,保证两个数始终为正,求在一些操作过后能得到的最大的gcd和最小的操作次数。
思路:易得gcd(a,b)=gcd(b,a-b)=a-b时gcd最大。
代码:
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    //IO
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        if(n==m)cout<<0<<" "<<0<<endl;
        else
        {
            ll kk=abs(n-m);
            if(n>m)swap(n,m);
            ll k1=n/kk;
            ll k2=min(n-k1*kk,(k1+1)*kk-n);
            cout<<kk<<" "<<k2<<endl;
        }
    }
}
View Code

B. Customising the Track

题意:给定长为n的数列,你可以从任意一个数中取出数加到另一个数中,求任意次操作后的∑∑| a[i] - a[j] | (j>i)的最小值。

思路:尽可能均摊,剩下的每个各分1。

代码:

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    //IO
    cin>>t;
    while(t--)
    {
        sum=0;
        cin>>n;
        rep(i,1,n)
        {
            cin>>a[i];
            sum+=a[i];
        }
        ll kk=sum-sum/n*n;
        cout<<kk*(n-kk)<<endl;
    }
}
View Code

C. Need for Pink Slips

题意:Problem - C - Codeforces

思路:dfs累加概率。

代码:

double ans;
ll a[N],b[N],vis[N],dis[N],head[N];
double p[10];
void dfs(double now,double p1,double p2,double p3)
{
    double v;
    ans+=now;
    if(p1>1e-9)
    {
        v=min(p1,p[3]);
        if(p2>1e-9)dfs(now*p1,p1-v,p2+v/2.0,p3+v/2.0);
        else dfs(now*p1,p1-v,p2,p3+v);
    }
    if(p2>1e-9)
    {
        v=min(p2,p[3]);
        if(p1>1e-9)dfs(now*p2,p1+v/2.0,p2-v,p3+v/2.0);
        else dfs(now*p2,p1,p2-v,p3+v);
    }
    return ;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr),cout.tie(nullptr);
    //IO
    cin>>t;
    while(t--)
    {
        ans=0;
        rep(i,0,3)cin>>p[i];
        dfs(1,p[0],p[1],p[2]);
        cout<<nps ans<<endl;
    }
}
View Code

 

posted @ 2021-07-13 14:34  Geospiza  阅读(55)  评论(0)    收藏  举报