Cod sursa(job #1003043)

Utilizator diana97Diana Ghinea diana97 Data 29 septembrie 2013 17:51:49
Problema Farfurii Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f ("farfurii.in");
ofstream g ("farfurii.out");

int n, k;

void citeste ()
{
    f>>n>>k;
}

void rezolva ()
{
    int m, p;
    for (p=1; p<n; p*=2);
        for (m=0; p>0; p/=2)
            if (m+p<=n && (m+p)*(m+p-1)<=2*k) m=m+p;
    k=k-m*(m-1)/2;
    if (k==0)
    {
        for (int i=1; i<=n-m; i++) g<<i<<' ';
        for (int i=n; i>n-m; i--) g<<i<<' ';
        g<<'\n';
    }
    else
    {
        k=m-k;
        m++;
        for (int i=1; i<=n-m; i++) g<<i<<' ';
        g<<n-k<<' ';
        for (int i=n; i>n-k; i--) g<<i<<' ';
        for (int i=n-k-1; i>n-m; i--) g<<i<<' ';
        g<<'\n';
    }
}

int main ()
{
    citeste ();
    rezolva ();
    return 0;
}