Advertisement
Guest User

sum_of_fact

a guest
Aug 25th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include<iostream>
  2. #include<list>
  3. #include<string>
  4. #include<cstring>
  5. #include<sstream>
  6. #include<cctype>
  7. #include<string.h>
  8. #include<algorithm>
  9. #include<cmath>
  10. #include<stack>
  11. #include<fstream>
  12. #include<cstdlib>
  13. #include<vector>
  14. #include<map>
  15. #include<set>
  16. #include<utility>
  17. #include<iomanip>
  18. #include<queue>
  19. using namespace std;
  20.  
  21. #define LL long long int
  22. #define uLL unsigned long long int
  23.  
  24. #define S(a) scanf("%d",&a)
  25. #define S2(a,b) scanf("%d%d",&a,&b)
  26. #define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
  27. #define SLL(a) scanf("%lld",&a)
  28. #define SLL2(a,b) scanf("%lld%lld",&a,&b)
  29. #define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
  30. #define SC(a) scanf("%c",&a)
  31. #define P(a) printf("%d",a)
  32. #define PS(a) printf("%s",a)
  33. #define PLL(a) printf("%lld",a)
  34. #define PCASE(kk) printf("Case %d: ",kk++)
  35. #define PCASENL(kk) printf("Case %d:\n",kk++)
  36. #define NL puts("")
  37.  
  38. #define pb(a) push_back(a)
  39. #define mp(a,b) make_pair(a,b)
  40. #define pi (2.0*acos(0.0))
  41. #define pii pair<int,int>
  42. #define SQR(a) (a*a)
  43.  
  44. template<typename T>inline T POW(T B,T P)
  45. {
  46.     if(P==0) return 1;
  47.     if(P&1) return B*POW(B,P-1);
  48.     else return SQR(POW(B,P/2));
  49. }
  50. template <typename T>inline T ModInv (T b,T m)
  51. {
  52.     return BigMod(b,m-2,m);
  53. }
  54. template<typename T>inline T ABS(T a)
  55. {
  56.     if(a<0)return -a;
  57.     else return a;
  58. }
  59. template<typename T>inline T gcd(T a,T b)
  60. {
  61.     if(a<0)return gcd(-a,b);
  62.     if(b<0)return gcd(a,-b);
  63.     return (b==0)?a:gcd(b,a%b);
  64. }
  65. template<typename T>inline T lcm(T a,T b)
  66. {
  67.     if(a<0)return lcm(-a,b);
  68.     if(b<0)return lcm(a,-b);
  69.     return a*(b/gcd(a,b));
  70. }
  71. template <class T> inline T BMOD(T p,T e,T m)
  72. {
  73.     T ret=1;
  74.     while(e)
  75.     {
  76.         if(e&1) ret=(ret*p)%m;
  77.         p=(p*p)%m;
  78.         e>>=1;
  79.     }
  80.     return (T)ret;
  81. }
  82.  
  83. //for(__typeof(pp.begin()) i=pp.begin(); i!=pp.end(); i++ )
  84.  
  85. //knight and king move....
  86.  
  87.  
  88.  
  89. //int Dx[]={-2,-1,1,2,1,2,-2,-1};
  90. //int Dy[]={-1,-2,2,1,-2,-1,1,2};
  91. //int dx[]={-1,1,0,0};
  92. //int dy[]={0,0,-1,1};
  93. //int dx[]= {-1,-1,0,0,1,1};
  94. //int dy[]= {-1,0,-1,1,0,1};
  95. //////////////////////////////////////////////////
  96.  
  97. LL n;
  98. LL fact[30];
  99. vector< pair<LL,LL> >v;
  100.  
  101. int main()
  102. {
  103.     fact[0]=1LL;
  104.     LL x=1LL;
  105.     for(int i=1; i<=20; i++)
  106.     {
  107.         fact[i]=i*fact[i-1];
  108. //        cout<<i<<" "<<fact[i]<<endl;
  109.         x+=fact[i];
  110.            if(x>1000000000000000000LL)
  111.            {
  112. //               P(i),NL;
  113.                break;
  114.            }
  115.     }
  116.  
  117. //        cout<<x<<endl;
  118. //        cout<<"......"<<endl;
  119.  
  120.     for(int i=1; i<=524288; i++)
  121.     {
  122.         LL tt=i;
  123.         LL res=0;
  124.         for(int j=0; j<=19; j++)
  125.         {
  126.             if((tt&(1LL<<j)))
  127.             {
  128.                 res+=fact[j];
  129.             }
  130.         }
  131. //        cout<<i<<" "<<res<<endl;
  132.         if(res>1000000000000000000LL)break;
  133.         v.pb(mp(res,tt));
  134.     }
  135.     sort(v.begin(),v.end());
  136.  
  137. //    cout<<"...... "<<v[524900].first<<endl;
  138.     int t,tc=1;
  139.     S(t);
  140.     while(t--)
  141.     {
  142. //        LL n;
  143.         SLL(n);
  144.  
  145.         LL lo=0,hi=v.size(),mid;
  146.         LL ans=-1;
  147.         while(lo<=hi)
  148.         {
  149.             mid=(lo+hi)/2;
  150. //            cout<<lo<<" "<<hi<<" "<<" "<<mid<<" "<<v[mid].first<<endl;
  151.             if(v[mid].first==n)
  152.             {
  153.                 ans=mid;
  154.                 break;
  155.             }
  156.  
  157.             if(v[mid].first<n)
  158.             {
  159.                 lo=mid+1;
  160.             }
  161.             else
  162.             {
  163.                 hi=mid-1;
  164.             }
  165.         }
  166.  
  167.         PCASE(tc);
  168.         if(ans==-1)printf("impossible\n");
  169.         else
  170.         {
  171.             LL ss=v[ans].second;
  172.             int fl=0;
  173.             for(int i=0; i<=19; i++)
  174.             {
  175.                 if((ss&(1LL<<i)))
  176.                 {
  177.                     if(!fl)
  178.                     {
  179.                         printf("%d!",i),fl=1;
  180.                     }
  181.                     else printf("+%d!",i);
  182.                 }
  183.             }
  184.             NL;
  185.         }
  186.     }
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement