Cod sursa(job #1210091)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 19 iulie 2014 10:55:26
Problema NextSeq Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.54 kb
#include <fstream>
#include <algorithm>
#include <cstdio>
using namespace std;
short Vcar[10005];
short Array[10005];
short Power[1000005];
short Result[1000005],Result2[1000005],Aux1[1000005],Aux2[1000005],N,M,P,A[10005],B[10005],one[]={1,1,0};
void Read()
{
    short i;
    scanf("%hd",&N);
    scanf("%hd",&M);
    scanf("%hd",&P);
    for(i=1;i<=N;i++)
        scanf("%hd",&Array[i]);
    sort(Array+1,Array+N+1);
}

void buildVcar()
{
    short i;
    for(i=1;i<=N;i++)
        Vcar[Array[i]]=i;
}

void mul(short A[], short B)
{
      short i, t = 0;
      for (i = 1; i <= A[0] || t; i++, t /= 10)
              A[i] = (t += A[i] * B) % 10;
      A[0] = i - 1;
}

void sub(short A[], short B[])
{
      short i, t = 0;
      for (i = 1; i <= A[0]; i++) {
              A[i] -= ((i <= B[0]) ? B[i] : 0) + t;
              A[i] += (t = A[i] < 0) * 10;
      }
      for (; A[0] > 1 && !A[A[0]]; A[0]--);
}

void add(short A[], short B[])
{
      short i, t = 0;
      for (i=1; i<=A[0] || i<=B[0] || t; i++, t/=10)
              A[i] = (t += A[i] + B[i]) % 10;
      A[0] = i - 1;
}
void readAB()
{
    short i;
    for(i=1;i<=M;i++)
    {
        short value;
        scanf("%hd",&value);
        A[i]=Vcar[value];
    }
    for(i=1;i<=P;i++)
    {
        short value;
        scanf("%hd",&value);
        B[i]=Vcar[value];
    }
}
void countResultB()
{
    Power[0]=Power[1]=Aux2[0]=Aux2[1]=1;
    mul(Aux2,B[P]);
    add(Result,Aux2);
    for(short i=P-1;i>=1;i--)
    {
        mul(Power,N);
        for(short i=Aux2[0];i>Power[0];i--)
            Aux2[i]=0;
        for(short j=0;j<=Power[0];j++)
            Aux2[j]=Power[j];
        mul(Aux2,B[i]-1);
        add(Result,Aux2);
        if(P-i>=M)
            add(Result,Power);
    }
}

void countResultA()
{
    Power[0]=Power[1]=Aux1[0]=Aux1[1]=1;
    mul(Aux1,A[M]);
    add(Result2,Aux1);
    for(short i=M-1;i>=1;i--)
    {
        mul(Power,N);
        for(short i=Aux1[0];i>Power[0];i--)
            Aux1[i]=0;
        for(short j=0;j<=Power[0];j++)
            Aux1[j]=Power[j];
        mul(Aux1,A[i]-1);
        add(Result2,Aux1);
    }
    for(short i=1;i<=Power[0];i++)
        Power[i]=0;
    Power[0]=0;
}
int main()
{
    freopen("nextseq.in","r",stdin);
    freopen("nextseq.out","w",stdout);
    Read();
    buildVcar();
    readAB();
    countResultA();
    countResultB();
    sub(Result,Result2);
    sub(Result,one);
    for(short i=Result[0];i>=1;i--)
        printf("%hd",Result[i]);
    return 0;
}