Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <map>
- using namespace std;
- using ll = long long;
- void cv(vector <auto> v){
- cout<<'\n';
- for (auto x: v){
- cout<<x<<' ';
- }cout<<'\n';
- }
- void cm(map <auto, auto> m){
- cout<<'\n';
- for (auto x: m) cout<<x.first<<' '<<x.second<<'\n';
- cout<<'\n';
- }
- string rl(string x){//real - without 1st zeroes
- int sz = x.size();
- int no_0 = sz - 1;
- for (int i = 0;i<sz;++i){
- if (x[i]!= '0'){
- no_0 = i;
- break;
- }
- }
- string y = x.substr(no_0);
- return y;
- }
- string pr2(string x){
- int rest = 0, dig_now, dig;
- reverse(x.begin(), x.end());
- int sz = x.size();
- string ans = "";
- for (int i = 0; i < sz; ++i){
- dig_now = (x[i] - '0') * 2 + rest;
- dig = dig_now % 10;
- ans += (dig + 48);
- rest = dig_now / 10;
- //cout<<"ans += "<<(dig + 48)<<" rest = "<<rest<<'\n';
- }
- if (rest != 0) ans += (rest + 48);
- reverse(ans.begin(), ans.end());
- //cout<<"ans = "<<ans<<'\n';
- return ans;
- }
- string dv2(string x){
- //взять 1-й кусокй, который >= 2
- int idx = 0;
- int rest = 0;
- int now_d;
- now_d = x[0] - '0';
- int sz = x.size();
- if (x[0] == '1'){
- now_d = now_d * 10 + (x[1] - '0');
- idx++;
- }
- string ans = "";
- ans += (now_d / 2 + 48);
- rest = (now_d % 2);
- while (idx < sz){
- idx++;
- now_d = rest * 10 + (x[idx] - '0');
- ans += (now_d / 2 + 48);
- rest = now_d % 2;
- }
- ans.pop_back();
- //cout<<"ans = "<<ans<<'\n';
- return ans;
- }
- string op1(string x){
- return pr2(x);
- }
- string op2(string x){
- int sz = x.size();
- if ( (x[sz-1] - '0') % 2 == 0){
- return dv2(x);
- } else{
- return "NO";
- }
- }
- string op3(string x){
- int sz = x.size();
- if (sz % 2 == 1){
- return "NO";
- } else{
- int len = sz / 2;
- string a,b;
- a = x.substr(0, len);
- b = x.substr(len);
- return a ;
- }
- }
- string op4(string x){
- int sz = x.size();
- if (sz % 2 == 1){
- return "NO";
- } else{
- int len = sz / 2;
- string a,b;
- a = x.substr(0, len);
- b = x.substr(len);
- return b;
- }
- }
- string op5(string x){
- int sz = x.size();
- if (sz % 2 == 1){
- return "NO";
- } else{
- int len = sz / 2;
- string a,b;
- a = x.substr(0, len);
- b = x.substr(len);
- return a + a;
- }
- }
- string op6(string x){
- int sz = x.size();
- if (sz % 2 == 1){
- return "NO";
- } else{
- int len = sz / 2;
- string a,b;
- a = x.substr(0, len);
- b = x.substr(len);
- return b + b;
- }
- }
- bool half_1(string x){
- int sz = x.size();
- bool A = (sz%2==0);
- if (A){
- string a,b;
- a = x.substr(0, sz / 2);
- b = x.substr(sz / 2);
- //cout<<"ab= "<<a<<' '<<b<<'\n';
- bool B = (a.find('1') >= 0 && a.find('1') < (int)a.size());
- bool C = (b.find('1') >= 0 && b.find('1') < (int)b.size());
- return B;
- }
- else{
- return false;
- }
- }
- bool half_2(string x){
- int sz = x.size();
- bool A = (sz%2==0);
- if (A){
- string a,b;
- a = x.substr(0, sz / 2);
- b = x.substr(sz / 2);
- //cout<<"ab= "<<a<<' '<<b<<'\n';
- bool B = (a.find('1') >= 0 && a.find('1') < (int)a.size());
- bool C = (b.find('1') >= 0 && b.find('1') < (int)b.size());
- return C;
- }
- else{
- return false;
- }
- }
- bool even(string x){
- int sz = x.size();
- char l = x[sz-1];
- int d = l - '0';
- return d % 2 == 0;
- }
- bool half(string x){
- int sz = x.size();
- bool A = (sz%2==0);
- return A;
- }
- int main()
- {
- //ВОПРОС: что сначала: делить или умножать на 2?
- string n;
- cin>>n;
- string path = "";
- while ( n != "1"){
- cout<<"n = "<<n<<'\n';
- if (n == "2" || n == "4"){
- while (n != "1"){
- n = op2(n);
- path += '2';
- cout<<"n = "<<n<<'\n';
- }
- }
- else if (half_1(n)){
- n = op3(n);
- path += '3';
- }
- else if (half_2(n)){
- n = op4(n);
- path += '4';
- }
- else if (half(n)){
- n = op3(n);
- path += '3';
- }
- else if (even(n)){
- n = op2(n);
- path += '2';
- }
- else if (!half(n)){
- while (!half(n)){
- n = op1(n);
- path += '1';
- cout<<"n = "<<n<<'\n';
- }
- }
- }
- cout<<(int)path.size()<<'\n';
- cout<<path<<'\n';
- }
- /* last variant - сначала делим - пример тупой реализации:
- 222
- n = 222
- n = 111
- n = 222
- n = 444
- n = 888
- n = 1776
- n = 1776
- n = 17
- 7
- 2111133
- */
- /*
- сначала умножаем
- 327198
- n = 327198
- n = 198
- n = 396
- n = 792
- n = 1584
- n = 1584
- n = 15
- 6
- 411133
- */
- /*
- сначла делим
- 327198
- n = 327198
- n = 198
- n = 99
- n = 9
- n = 18
- n = 18
- 5
- 42313
- */
- //132903291037983279838219032183263284948179089128218199219129099
Add Comment
Please, Sign In to add comment