Cod sursa(job #7250)

Utilizator stef2nStefan Istrate stef2n Data 21 ianuarie 2007 13:15:19
Problema Diviz Scor 20
Compilator cpp Status done
Runda preONI 2007, Runda 1, Clasele 11-12 Marime 1.47 kb
#include <stdio.h>
#include <string>
#include <set>
using namespace std;

#define infile "diviz.in"
#define outfile "diviz.out"
#define MOD 30103

FILE *fin,*fout;
int K,a,b;
char sir[205];
set <string> sol;
int nrcifre;
int st[205];
string value;
int n,rest;

void back(int k)
  {
   if(k==nrcifre)
     {if(!rest)
        sol.insert(value);
      return;}
   int start,oldrest;
   string oldvalue;
   char xxx[3];
   if(!k)
     start=0;
   else
     start=st[k-1]+1;
   for(st[k]=start;st[k]<n-nrcifre+k+1;st[k]++)
      {
       if(!k)
         {if(sir[st[k]]!='0')
            {
             sprintf(xxx,"%c",sir[st[k]]);
             oldrest=rest;
             rest=(rest*10+(sir[st[k]]-'0'))%K;
             oldvalue=value;
             value+=string(xxx);
             back(k+1);
             value=oldvalue;
             rest=oldrest;
            }}
       else
        {
         sprintf(xxx,"%c",sir[st[k]]);
         oldrest=rest;
         rest=(rest*10+(sir[st[k]]-'0'))%K;
         oldvalue=value;
         value+=string(xxx);
         back(k+1);
         value=oldvalue;
         rest=oldrest;
        }
      }
  }

int main()
{
fin=fopen(infile,"r");
fscanf(fin,"%d %d %d\n%s",&K,&a,&b,&sir);
fclose(fin);
n=strlen(sir);
for(int i=a;i<=b;i++)
   {
    nrcifre=i;
    value=string("");
    rest=0;
    back(0);
   }

//for(set<string>::iterator j=sol.begin();j!=sol.end();j++)
//   printf("%s\n",j->c_str());

fout=fopen(outfile,"w");
fprintf(fout,"%d\n",sol.size());
fclose(fout);
return 0;
}