Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define f first
  4. #define se second
  5. #define s second
  6. #define vi_a vector<int>a;
  7. #define p_b push_back
  8. #define ll long long
  9. #define ld  double
  10. #define pll pair<ll,ll>
  11. #define pii pair<int,int>
  12. #define m_p make_pair
  13. #define fast_io cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
  14. #define all(x) x.begin(),x.end()
  15. #define getfiles    ifstream cin("birch.in");ofstream cout("birch.out");
  16. #define pw(x) (1ll << x)
  17. #define sz(x) (int)x.size()
  18. #define endl "\n"
  19. #define rall(x) x.rbegin(),x.rend()
  20. #define len(a) (ll)a.size()
  21. #define rep(x,l,r) for(ll x=l;x<r;x++)
  22. using namespace std;
  23. ld eps = (ld)1 / 1e15;
  24. ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
  25. ll sqr(ll a) { return a * a; }
  26. ll qb(ll a) { return a * a * a; }
  27. ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
  28. ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
  29. ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
  30. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //const ll to[5]={0,4,6,7,9};
  32. const ll N=2*1e3+1;
  33. const ll R=1e4;
  34. const ll to[7]={1,2,3,1,3,2,1};
  35. vector<ll>ans;
  36. ll mn=LLONG_MAX;
  37. void brute(vector<ll>a){
  38. //    vector<ll>b=a;
  39. //    cout<<b.back()<<endl;
  40.     string man=to_string(a.back());
  41.     if(len(man)>=2){
  42.         for(ll i=0;i<len(man)-1;i++){
  43.             string cs=man.substr(0,i+1);
  44.             ll f=stoll(cs);
  45. //            cout<<cs<<endl;
  46.             cs=man.substr(i+1,len(man)-i-1);
  47.             ll s=stoll(cs);
  48.             if(s!=f){
  49. //                ll need=
  50.                 a.p_b(abs(f-s));
  51.                 brute(a);
  52.                 a.pop_back();
  53.             }
  54.         }
  55.     }
  56.     if(a.back()<mn){
  57.         ans=a;
  58.         mn=a.back();
  59.     }
  60. //    cout<<sz(a)<<endl;
  61. //    for(auto u : a){
  62. //        cout<<u<<" ";
  63. //    }
  64. //    cout<<"Bruting"<<endl;
  65. }
  66.  
  67. string cut (string s)
  68. {
  69.     int i = 0;
  70.     int j = len(s) - 1;
  71.     string a = (s.substr(0, 1));
  72.     string b = (s.substr(len(s) - 1, len(s)));
  73.     while (i + 1 != j)
  74.     {
  75.         ll q = stoll(a);
  76.         ll w = stoll(b);
  77.         if (q == w)
  78.         {
  79.             i++;
  80.             a += s[i];
  81.             reverse(all(b));
  82.         }
  83.         else if (q > w)
  84.         {
  85.             j--;
  86.             reverse(all(b));
  87.             b += s[j];
  88.             reverse(all(b));
  89.         }
  90.         else if (q < w)
  91.         {
  92.             i++;
  93.             a[i] += s[i];
  94.         }
  95.     }
  96.     if (a == b)
  97.         return s;
  98.     else
  99.         return to_string(abs(stoll(a) - stoll(b)));
  100. }
  101.  
  102. vector<ll> solve(ll n){
  103.     string s = to_string(n);
  104.     vector <ll> anss;
  105.     anss.push_back(stoll(s));
  106.     while (len(s) != 1)
  107.     {
  108.         int temp_len = len(s);
  109.         s = cut(s);
  110.         if (len(s) == temp_len)
  111.         {
  112.             return anss;
  113.         }
  114.         anss.push_back(stoll(s));
  115.     }
  116.     cout<<sz(anss)<<endl;
  117.     return anss;
  118. }
  119. void check(){
  120.     auto bind_f=bind(uniform_int_distribution<ll>(1,1e12),mt19937(time(0)));
  121.     while(1){
  122.         ll n=bind_f();
  123.         vector<ll>pog;
  124.         pog.p_b(n);
  125.         brute(pog);
  126.         vector<ll>lya=ans;
  127.         vector<ll>t=solve(n);
  128.         if(t.back()>lya.back()){
  129.             cout<<"Wrong answer"<<" "<<n<<endl;
  130.             cout<<"Your answer :"<<t.back()<<endl;
  131.             cout<<"Jury's answer :"<<lya.back()<<endl;
  132.  
  133.             system("pause");
  134.  
  135.             break;
  136.         }
  137.         else{
  138.             cout<<"Ok's"<<endl;
  139.         }
  140.         ans.clear();
  141.         mn=LLONG_MAX;
  142.     }
  143. }
  144. signed main(){
  145.     fast_io;
  146. //    ll t;
  147. //    cin>>t;
  148. //    while(t--){
  149. //        ll n;
  150. //        cin>>n;
  151. //        mn=LLONG_MAX;
  152. //        vector<ll>b;
  153. //        b.p_b(n);
  154. //        brute(b);
  155. //        cout<<sz(ans)<<" ";
  156. //        for(auto u : ans) cout<<u<<" ";
  157. //        cout<<endl;
  158. //        ans.clear();
  159. //    }
  160.     check();
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement