karlangadas

ineficiente

Jan 7th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #define cero 10000
  5. using namespace std;
  6. int x,y,z,h;
  7.  
  8. int mat[20010][22];
  9. void Leer(){
  10.     string s;
  11.     cin>>s;
  12.     int i=0;
  13.     while (s[i]!='d')
  14.         i++;
  15.     x=0;
  16.     for (int j=0;j<i;j++)
  17.         x=x*10+(s[j]-'0');
  18.     int len=s.size(),ant=i+1,sign=1;
  19.     while ((i<len) && (s[i]!='+')&&(s[i]!='-'))
  20.         i++;
  21.     y=0;
  22.     for (int j=ant;j<i;j++)
  23.         y=y*10+(s[j]-'0');
  24.     z=0;
  25.     if (i==len)
  26.         return;
  27.     ant=i+1;
  28.     if (s[i]=='-')
  29.         sign=-1;
  30.     for (int j=ant;j<len;j++)
  31.         z=z*10+(s[j]-'0');
  32.         z=z*sign;
  33. }
  34. double Procesar(){
  35.     memset(mat,0,sizeof(mat));
  36.     int aux=0; 
  37.     for (int i=1+z;i<=(y+z);i++){
  38.         mat[cero+i][1]=1;
  39.         if ((1==x)&&(i<h))
  40.             aux+=mat[cero+i][1];
  41.         if ((1==x)&&(i==h))
  42.             return ((pow(y,x)-aux)/pow(y,x));
  43.     }
  44.    
  45.     for (int i=2;i<=x;i++){
  46.         for (int j=(i+z);j<=((i*y)+z);j++){
  47.             mat[cero+j][i]=0;
  48.             for (int k=1;k<=y;k++)
  49.                     mat[cero+j][i]+=mat[cero+j-k][i-1];
  50.  
  51.             if ((i==x)&&(j<h)){
  52.                 aux+=mat[cero+j][i];
  53.                 }
  54.             if ((i==x)&&(j==h))
  55.                 break;
  56.         }
  57.     }
  58.     return ((pow(y,x)-aux)/pow(y,x));
  59. }
  60.  
  61. int main() {
  62.     int t,s;
  63.     double max,ans;
  64.     cin>>t;
  65.     for (int i=1;i<=t;i++){
  66.         cin>>h>>s;
  67.         max=0;
  68.         for (int j=0;j<s;j++){
  69.             Leer();
  70.             ans=Procesar();
  71.             if (ans>max)
  72.                 max=ans;
  73.         }
  74.         printf("Case #%d: %.6f\n",i,max);
  75.     }
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment