Nbrevu

Tuenti Contest 13 (Pablo Moreno)

Jun 20th, 2011
975
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Author: Pablo Moreno Olalla
  3.     Email address: darthbrevu@yahoo.es
  4. */
  5. #include <cstdio>
  6. #include <string>
  7. #include <numeric>
  8. #include <sstream>
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. unsigned long long initial=36;  //Six zeros. From here, we add the delta for each second.
  14.  
  15. unsigned long long transitions[]={0,4,1,1,2,1,1,4,0,1};
  16. unsigned long long transition50=2;
  17. unsigned long long transition30=2;
  18. unsigned long long transition20=2;
  19.  
  20. unsigned long long accum10,accum6,accum4,accum2_;
  21.  
  22. unsigned long long accumx[6];
  23. unsigned long long times[]={36000,3600,600,60,10};
  24.  
  25. inline void initA() {
  26.     accum10=accumulate(&transitions[0],&transitions[10],0);
  27.     accum6=accumulate(&transitions[0],&transitions[5],transition50);
  28.     accum4=accumulate(&transitions[0],&transitions[3],transition30);
  29.     accum2_=transitions[0]+transitions[1]+transition20;
  30.     accumx[0]=8786*accum10+1464*accum6+accum2_+accum4;
  31.     accumx[1]=3661*accum10+610*accum6;
  32.     accumx[2]=366*accum10+61*accum6;
  33.     accumx[3]=61*accum10+10*accum6;
  34.     accumx[4]=6*accum10+accum6;
  35.     accumx[5]=accum10;
  36. }
  37.  
  38. unsigned long long totalLeds(unsigned long long secs)   {
  39.     unsigned long long res=initial;
  40.     unsigned long long d=secs/86400;
  41.     secs%=86400;
  42.     unsigned long q;
  43.     res+=d*accumx[0];
  44.     for (size_t i=0;i<5;++i)    {
  45.         q=secs/times[i];
  46.         secs%=times[i];
  47.         res+=accumulate(&transitions[0],&transitions[q],q*accumx[i+1]);
  48.     }
  49.     return accumulate(&transitions[0],&transitions[secs],res);
  50. }
  51.  
  52. int main(int argc,char **argv)  {
  53.     //The first two lines are not strictly necessary, but make the code a little more handy.
  54.     if (argc>=2) freopen(argv[1],"r",stdin);
  55.     if (argc>=3) freopen(argv[2],"w",stdout);
  56.     initA();
  57.     string tmp;
  58.     unsigned long long ull;
  59.     do  {
  60.         getline(cin,tmp);
  61.         istringstream iss(tmp);
  62.         iss>>ull;
  63.         if (!iss.fail()) cout<<totalLeds(ull)<<endl;
  64.     }   while (!cin.eof());
  65.     return 0;
  66. }
RAW Paste Data