Cod sursa(job #7148)

Utilizator SpiriSpiridon Alexandru Spiri Data 21 ianuarie 2007 12:51:07
Problema 1-sir Scor 10
Compilator cpp Status done
Runda preONI 2007, Runda 1, Clasele 11-12 Marime 2.1 kb
#include <fstream>
using namespace std;
#define MAX 257

ifstream fin ("1-sir.in");
ofstream fout ("1-sir.out");

int n, s;
int x[MAX];
int sum = 0;
long long int rez = 0;
long long int nr = 194767;
void Back(int k);

int main()
{
    fin >> n >> s;
    x[1] = 0;
    Back(2);
    
    fout << rez;
    
    fin.close();
    fout.close();
}

void Back(int k)
{
     int x1 = x[k-1];
     int y1 = x[k-1] + (n-k);
     if ( sum + y1*(y1+1)/2 - x1*(x1-1)/2 < s )
     {
          x[k] = x[k-1] + 1;
          sum+=x[k];
          if ( k == n )
          {
               if ( sum == s )
               {
              /*      for ( int j = 1; j <= n; j++ )
                    {
                        fout << x[j] << " ";
                    }
                    fout << "\n";*/
                    rez++;
                    rez%=nr;
               }
          }
          else Back(k+1);
          sum-=x[k];
     }
     else
     {
                  x[k] = x[k-1] + 1;
                  sum+=x[k];
                  if ( k == n )
                  {
                     if ( sum == s )
                     {
                /*        for ( int j = 1; j <= n; j++ )
                        {
                               fout << x[j] << " ";
                        }
                        fout << "\n";*/
                        rez++;
                        rez%=nr;
                     }
                  }
                  else Back(k+1);
                  sum-=x[k];
                  
                  x[k] = x[k-1] - 1;
                  sum+=x[k];
                  if ( k == n )
                  {
                     if ( sum == s )
                     {
                  /*      for ( int j = 1; j <= n; j++ )
                        {
                            fout << x[j] << " ";
                        }
                        fout << "\n";*/
                        rez++;
                        rez%=nr;
                     }
                  }
                  else Back(k+1);
                  sum-=x[k];
     }
}