Codeforces 847C

847C - Sum of Nestings

思路:简单的递归。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

void c(ll n,ll k)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    if(n==0)return ;
    if(k>=n-1)
    {
        cout<<'(';
        c(n-1,k-(n-1));
        cout<<')';
    }
    else 
    {
        cout<<"()";
        c(n-1,k);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,k;
    cin>>n>>k;
    if(n*(n-1)/2<k)cout<<"Impossible";
    else c(n,k);
    cout<<endl;
    return 0;
} 
原文地址:https://www.cnblogs.com/widsom/p/7569173.html