Cod sursa(job #231653)

Utilizator katakunaCazacu Alexandru katakuna Data 14 decembrie 2008 12:46:30
Problema Tablete Scor 0
Compilator cpp Status done
Runda Algoritmiada 2009, Runda 1, Clasele 9-10 Marime 0.92 kb
#include<stdio.h>
#include<string.h>
#define INF 1<<30;

int min,i,x1,x2,I,a,b,s[1000111];
char c,C[10000011];
long long rez;

int binar(int x){
int p=1,u=s[0],mij;

   while(p<=u){
   mij=(p+u)/2;

     if(s[mij] >= x)
     u=mij-1;
     
     else
     p=mij+1;
     
   }

   if(s[p]==x)
   return x;

   if(p <= s[0])
   return s[p];

return -1;
}


int main(){

FILE *f=fopen("jstc.in","r");
fscanf(f,"%d %d\n",&a,&b);

fscanf(f,"%s",C);

  //while(!feof(f) && c!='\n' && c!=0){
int N=strlen(C);

  for(i=0;i<N;i++){
  c=C[i];
    if(c=='I'){
    I++;
    s[0]++;
    s[s[0]]=I;
    }

    if(c=='E'){
    s[s[0]]=0;
    s[0]--;
    }

    if(c=='Q'){
    x2=(a*x1 + b)%I+1;
    x1=x2;
    min=binar(x1);
    rez+=(long long)min;
    }

  fscanf(f,"%c",&c);
  }

fclose(f);



FILE *g=fopen("jstc.out","w");
fprintf(g,"%lld",rez);
fclose(g);

return 0;
}