Cod sursa(job #561414)

Utilizator thecurlyPintea Andrei thecurly Data 20 martie 2011 12:06:01
Problema Descompuneri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include<iostream.h>
#include<fstream.h>
long long n,p,k,st[1000001],x,a[1000001];
int as,ev,e,y;

void init()
{
	if(x==1) st[x]=1;
	else st[x]=st[x-1]-1;
}

int succesor()
{
	if(st[x]<n)
	{
	  st[x]++;
	  return 1;
	}
	else {
		p=p/st[x-1];
		return 0;
		}
}

int valid()
{
	if(p*st[x]<=n)
	{
	  p=p*st[x];
	  return 1;
	}
	else return 0;
}

int solutie()
{ return (p==n);}

void tipar()
{
	int i=1;
	while(i<x)
		{
		a[i]=st[i];
		i++;
		}
	a[i]=st[x];
	y=x;
} 


int bt()
{
	x=1;
	init();
	while(x>0)
	{
	  as=1; ev=0;
	  while(as && !ev)
		{
		as=succesor();
		if(as) ev=valid();
		}
	  if(as)
		if(solutie()) 
			{ e++; 
			  p=p/st[x]; 
			  if(e==k) tipar();
			}
		else { x++; init(); }
	  else x--;
	  }
	return e;
}

int main()
{
	ifstream f;
	f.open("desc.in");
	ofstream g;
	g.open("desc.out");
	g<<bt();
	g<<endl;
	for(int i=1;i<=y;i++)
		g<<a[i]<<" ";
	f.close();
	g.close();
	return 0;
}