Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define sf(nn)                  scanf ("%d", &nn)
  5. #define sfll(nn)                scanf ("%lld", &nn)
  6. #define pf                      printf
  7. #define casepf(nn)              printf ("Case #%d: ",nn)
  8. #define out(nn)                 cout <<nn <<endl
  9. #define loop(var,start,till)    for(int var=start; var<till; var++)
  10. #define pb                      push_back
  11. #define mem(a,b)                memset(a,b,sizeof(a))
  12. #define mp                      make_pair
  13. #define ll                      long long int
  14. #define inf                     2e9
  15. #define llinf                   2e18
  16. #define READ(f)                 freopen(f,"r",stdin)
  17. #define WRITE(f)                freopen(f,"w",stdout)
  18. #define Unique(a)               sort(all(a)),a.erase(unique(all(a)),a.end())
  19. #define mx                      20000005
  20. #define mod                     1000000007
  21.  
  22.  
  23. int dx[8]={0,0,1,-1,1,1,-1,-1};
  24. int dy[8]={1,-1,0,0,1,-1,1,-1};
  25.  
  26. string str;
  27. int lim,fl,inwhich;
  28.  
  29. ll calcsum(int n)
  30. {
  31.     ll totsum=0,sum;
  32.  
  33.     for(int i=0; i<str.length(); )
  34.     {
  35.         sum = 0;
  36.         for(int j=0; j<n && i<str.length(); j++)
  37.         {
  38.             sum = (ll) ((sum*10) + (ll)(str[i]-'0'));//cout<<(ll)(str[i]-'0')<<endl;
  39.             i++;
  40.         }//cout<<sum<<endl;
  41.         //sum = sum + temp;
  42.         totsum += sum;
  43.     }
  44.     return totsum;
  45. }
  46.  
  47. string tostring(ll res)
  48. {
  49.     string curstr = "";
  50.  
  51.     while(res != 0)
  52.     {
  53.         ll temp = (res%10);
  54.         curstr += ((char) temp+'0');
  55.         res /= 10;
  56.     }
  57.     reverse(curstr.begin(),curstr.end());
  58.  
  59.     return curstr;
  60. }
  61.  
  62. int ar[200005],vis[200005];
  63.  
  64. void printans(int n)
  65. {//out(n);//cin.ignore();
  66.     for(int i=0; i<str.length(); )
  67.     {
  68.         for(int j=0; j<n && i<str.length(); j++)
  69.         {
  70.             cout<<str[i];
  71.             i++;
  72.         }//out(i);
  73.         if(i < str.length()) pf("+");
  74.     }
  75.     pf("\n");
  76. }
  77.  
  78. void backtrack(int n,int len,string cur)
  79. {//cout<<n<<" "<<lim<<endl;
  80.     if(n == lim+1)
  81.     {
  82.         int pre = 0,tot=0;
  83.         for(int i=1; i<=lim; i++)
  84.         {//out(ar[i]);
  85.             int temp = 0;
  86.             for(int j=pre; j<ar[i]; j++)
  87.             {
  88.                 temp = (temp*10) + ((int)(cur[j]-'0'));//out(temp);//cout<<cur[j]<<"////\n";
  89.             }
  90.             pre = ar[i];
  91.             tot += temp;
  92.         }
  93. //cout<<tot<<" ... "<<endl;
  94.         int temp = 0;
  95.         for(int j=pre; j<cur.length(); j++)
  96.         {
  97.             temp = (temp*10) + ((int)(cur[j]-'0'));//out(temp);
  98.         }
  99.         tot += temp;
  100.         //cout<<"tot : "<<tot<<endl;
  101.         string res = tostring(tot);
  102.  
  103.         int jogfol = 0;
  104.  
  105.         for(int i=0; i<res.size(); i++)
  106.         {
  107.             jogfol += ((int)res[i]-'0');
  108.         }
  109.  
  110.         string strjogfol = tostring(jogfol);//cout<<strjogfol<<" "<<strjogfol.size()<<endl;
  111.         if(strjogfol.size() == 1 && !fl)
  112.         {
  113.             fl = 1;
  114.             ///printans
  115.  
  116.             printans(inwhich);
  117.  
  118.  
  119.             int pre = 0,tot=0;
  120.                 for(int i=1; i<=lim; i++)
  121.                 {
  122.                     int temp = 0,flnow=0;
  123.                     for(int j=pre; j<ar[i]; j++)
  124.                     {
  125.                         cout<<cur[j];
  126.                         flnow=1;
  127.                     }
  128.                     pre = ar[i];
  129.                     if(flnow)cout<<"+";
  130.                 }
  131.                 int temp = 0;
  132.                 for(int j=pre; j<cur.length(); j++)
  133.                 {
  134.                     cout<<cur[j];
  135.                 }
  136.                 cout<<endl;
  137.  
  138.                 for(int i=0; i<res.size(); i++)
  139.                 {
  140.                     if(!i)cout<<res[i];
  141.                     else cout<<"+"<<res[i];
  142.                 }
  143.                 cout<<endl;
  144.             return;
  145.         }
  146.     }
  147.  
  148.     for(int i=1; i<len; i++)
  149.     {
  150.         if(!vis[i])
  151.         {
  152.             vis[i] = 1;
  153.             ar[n] = i;
  154.             backtrack(n+1,len,cur);
  155.             vis[i] = 0;
  156.         }
  157.     }
  158. }
  159.  
  160. int main()
  161. {
  162.     int n;
  163.     sf(n);
  164.  
  165.     fl=0;
  166.     cin>> str;
  167.  
  168.     if(str.size() == 1)
  169.     {
  170.         cout<<str<<endl;
  171.         cout<<str<<endl;
  172.         cout<<str<<endl;
  173.         return 0;
  174.     }
  175.     else if(str.size() == 2 && str[1] == '0')
  176.     {
  177.         cout<<str[0]<<"+"<<str[1]<<endl;
  178.         cout<<str[0]<<endl;
  179.         cout<<str[0]<<endl;
  180.  
  181.         return 0;
  182.     }
  183.  
  184.     ll res = calcsum(1);
  185.     string curstr = tostring(res); //out(res);
  186.  
  187.  
  188.     int len = curstr.length();
  189.     inwhich = 1;
  190.     for(int i=len-1,j=1; j<=3 && i>=1; j++,i--)
  191.     {
  192.         mem(ar,0);
  193.         mem(vis,0);
  194.         lim = i;//cout<<lim<<""<<endl;
  195.         backtrack(1,len,curstr);
  196.     }
  197.  
  198.     res = calcsum(2);
  199.     curstr = tostring(res);//out(res);
  200.  
  201.     if(fl) return 0;
  202.  
  203.     len = curstr.length();
  204.     inwhich = 2;
  205.     for(int i=len-1,j=1; j<=3 && i>=1; j++,i--)
  206.     {
  207.         mem(ar,0);
  208.         mem(vis,0);
  209.         lim = i;//cout<<lim<<""<<endl;
  210.         backtrack(1,len,curstr);
  211.     }
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement