Problem:
Problem D:
Revenge of Champernowne Constant
Champernowne constant is an irrational number. Its decimal representation starts with "0.", followed by concatenation of all positive integers in the increasing order.
You will be given a sequence
S
which consists of decimal digits. Your task is to write a program which computes the position of the first occurrence of S in Champernowne constant after the decimal point.
Input
The input has multiple test cases. Each line of the input has one digit sequence. The input is terminated by a line consisting only of
#
.
It is guaranteed that each sequence has at least one digit and its length is less than or equal to 100.
Output
For each sequence, output one decimal integer described above. You can assume each output value is less than 10
16
.
Sample Input
45678
67891011
21
314159265358979
#
Output for the Sample Input
4
6
15
2012778692735799

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define all(A) A.begin(),A.end()

ll f(ll N) {
    if (N > 1e16)return 1e18;
    ll res = 0;
    ll d = 1;
    rep(i, 16) {
        ll L = d;
        d *= 10;
        ll R = d - 1;
        ll len = min(N, R) - L + 1;
        if (len < 0)continue;
        res += (i + 1) * len;
        if (res > 1e16)return 1e18;
    }
    return res;
}

void solve(string S) {
    if (S.size() == 1 && S[0] != '0') {
        cout << S[0] - '0' << endl;
        return;
    }
    ll an = 1e18;
    if (S.size() < 17) {
        if (S[0] == '0') {
            string T = "1" + S;
            an = min(an, f(stoll(T) - 1) + 2);
        }
        else {
            an = min(an, f(stoll(S) - 1) + 1);
        }
    }
    ll SN = S.size();
    rep(i, SN)rep(j, i + 1) {
        if (i - j + 1 > 17)continue;
        if (S[j] == '0')continue;
        ll N = stoll(S.substr(j, i - j + 1));
        string PR = "",AF="";
        ll NN = N;
        bool OK = 1;
        while (PR.size() < j) {
            NN--;
            if (NN <= 0) {
                OK = 0;
                break;
            }
            PR = to_string(NN) + PR;
        }
        if (!OK)break;
        NN = N;
        while (AF.size() < SN - i - 1) {
            NN++;
            AF = AF + to_string(NN);
        }
        AF = AF.substr(0, SN - i - 1);
        PR = PR.substr(ll(PR.size() - j), j);
        string C = PR + S.substr(j, i - j + 1) + AF;
        if (C == S && OK)an = min(an, f(N - 1) - j + 1);
    }
    
    rep(i, SN - 1) {
        string P = S.substr(0, i + 1);
        string Q = S.substr(i + 1, SN - i - 1);
        if (Q[0] == '0')continue;
        ll m = min(P.size(), Q.size());
        if (P.size() > 16)continue;
        rep(j, m + 1) {
            string U = Q.substr(0, ll(Q.size())-j) + to_string(stoll(P) + 1);
            if (U.size() > 16)continue;
            if (U[0] == '0')continue;
            string PR = "", AF = "";
            ll N = stoll(U);
            bool OK = 1;
            while (PR.size() < i + 1) {
                N--;
                if (N == 0) {
                    OK = 0;
                    break;
                }
                PR = to_string(N) + PR;
            }
            PR = PR.substr(ll(PR.size()) - (i + 1), (i + 1))+U;
            N = stoll(U);
            while (PR.size() < SN) {
                N++;
                PR = PR + to_string(N);
            }
            PR = PR.substr(0, SN);
            if (PR != S)continue;
            an = min(an,f(N - 1) - i);
        }
    }
    cout << an << endl;
}

int main() {
    string S;
    while (cin >> S, S != "#")solve(S);
}
accuracy: 1
time_ms: 30
memory_kb: 3376
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define all(A) A.begin(),A.end()

ll f(ll N) {
    if (N > 1e16)return 1e18;
    ll res = 0;
    ll d = 1;
    rep(i, 16) {
        ll L = d;
        d *= 10;
        ll R = d - 1;
        ll len = min(N, R) - L + 1;
        if (len < 0)continue;
        res += (i + 1) * len;
        if (res > 1e16)return 1e18;
    }
    return res;
}

void solve(string S) {
    if (S.size() == 1 && S[0] != '0') {
        cout << S[0] - '0' << endl;
        return;
    }
    ll an = 1e18;
    if (S.size() < 17) {
        if (S[0] == '0') {
            string T = "1" + S;
            an = min(an, f(stoll(T) - 1) + 2);
        }
        else {
            an = min(an, f(stoll(S) - 1) + 1);
        }
    }
    ll SN = S.size();
    rep(i, SN)rep(j, i + 1) {
        if (i - j + 1 > 17)continue;
        if (S[j] == '0')continue;
        ll N = stoll(S.substr(j, i - j + 1));
        string PR = "";
        ll NN = N;
        bool OK = 1;
        while (PR.size() < j) {
            NN--;
            if (NN <= 0) {
                OK = 0;
                break;
            }
            PR = to_string(NN) + PR;
        }
        if (!OK)break;
        string AF = "";
        NN = N;
        while (AF.size() < SN - i - 1) {
            NN++;
            AF = AF + to_string(NN);
        }
        AF = AF.substr(0, SN - i - 1);
        PR = PR.substr(ll(PR.size() - j), j);
        string C = PR + S.substr(j, i - j + 1) + AF;
        if (C == S && OK) {
            ll res = f(N - 1) - j + 1;
            an = min(an, res);
        }
    }

    rep(i, SN - 1) {
        string P = S.substr(0, i + 1);
        string Q = S.substr(i + 1, SN - i - 1);
        if (Q[0] == '0')continue;
        ll m = min(P.size(), Q.size());
        if (P.size() > 16)continue;
        rep(j, m + 1) {
            string U = Q.substr(0, ll(Q.size())-j) + to_string(stoll(P) + 1);
            if (U.size() > 16)continue;
            if (U[0] == '0')continue;
            string PR = "", AF = "";
            ll N = stoll(U);
            bool OK = 1;
            while (PR.size() < i + 1) {
                N--;
                if (N == 0) {
                    OK = 0;
                    break;
                }
                PR = to_string(N) + PR;
            }
            PR = PR.substr(ll(PR.size()) - (i + 1), (i + 1));
            PR = PR + U;
            N = stoll(U);
            while (PR.size() < SN) {
                N++;
                PR = PR + to_string(N);
            }
            PR = PR.substr(0, SN);
            if (PR != S)continue;
            ll res = f(N - 1) - i;
            // cout << res << endl;
            an = min(an, res);

            // cout << U << " " << P << " " << Q << " " << PR << endl;
        }
    }

    cout << an << endl;
    // cout << "END" << endl;

}

int main() {
    string S;
    while (cin >> S, S != "#") {
        solve(S);
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3256
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

const long long INF = (3LL << 59);
const int LIMIT = 16;

// number of digits for 1, 2, ..., r-1
long long calc(long long r) {
	int digit = 1;
	long long l = 1;
	long long answer = 0;
	while (l < r) {
		answer += digit * min(r - l, 9 * l);
		digit += 1;
		l *= 10;
	}
	return answer;
}

// 1st pattern: can be divided into two or less sections
long long calc1(string S) {
	if (S == "01") {
		return 10; // 1234567891[01]112...
	}
	int N = S.size();
	vector<long long> pw(LIMIT + 1);
	pw[0] = 1;
	for (int i = 1; i <= LIMIT; i++) {
		pw[i] = pw[i - 1] * 10;
	}
	long long res = INF;
	if (S[0] != '0' && S.size() <= 16) {
		res = calc(stoll(S));
	}
	if (S[0] == '0' && S.size() <= 15) {
		res = calc(stoll("1" + S)) + 1;
	}
	for (int i = 1; i < N; i++) {
		if (i <= LIMIT && N - i <= LIMIT && (i == N || S[i] != '0')) {
			long long zl = 0, zr = 0;
			for (int j = 0; j < i; j++) {
				zl = zl * 10 + int(S[j] - '0');
			}
			for (int j = i; j < N; j++) {
				zr = zr * 10 + int(S[j] - '0');
			}
			long long suffix = (zl + 1) % pw[i];
			for (int j = 0; j <= i && j <= N - i; j++) {
				long long sl = zr % pw[j];
				long long sr = suffix / pw[i - j];
				if (sl == sr && N - j <= LIMIT) {
					long long val = zr * pw[i - j] + suffix % pw[i - j];
					res = min(res, calc(val) - i);
				}
			}
		}
	}
	return res;
}

// 2nd pattern: appears a number as a substring
long long calc2(string S) {
	int N = S.size();
	long long res = INF;
	for (int i = 0; i < N && i <= LIMIT; i++) {
		for (int j = i + 1; j <= N && j - i <= LIMIT; j++) {
			if (S[i] != '0') {
				long long x = stoll(S.substr(i, j - i));
				string sl, sr;
				for (long long k = x - 1; k >= 1 && sl.size() < S.size(); k--) {
					string sub = to_string(k);
					reverse(sub.begin(), sub.end());
					sl += sub;
				}
				reverse(sl.begin(), sl.end());
				for (long long k = x; sr.size() < S.size(); k++) {
					sr += to_string(k);
				}
				string sx = sl + sr;
				int pos = sx.find(S);
				if (pos != string::npos) {
					res = min(res, calc(x) - int(sl.size()) + pos);
				}
			}
		}
	}
	return res;
}

// function to solve problem
long long solve(string S) {
	long long res1 = calc1(S);
	long long res2 = calc2(S);
	return min(res1, res2);
}

int main() {
	while (true) {
		string S;
		cin >> S;
		if (S == "#") {
			break;
		}
		long long answer = solve(S);
		cout << answer + 1 << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3480
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=205;
const ll INF=1LL<<62;

string add(string a,string b){
    if(si(a)<si(b)) swap(a,b);
    
    for(int i=0;i<si(b);i++){
        a[si(a)-1-i]+=int(b[si(b)-1-i]-'0');
        if(a[si(a)-1-i]>'9'&&si(a)-1-i){
            a[si(a)-1-i]-=10;
            a[si(a)-1-i-1]++;
        }
    }
    for(int i=si(a)-1;i>=1;i--){
        if(a[i]>'9'){
            a[i]-=10;
            a[i-1]++;
        }
    }
    string res;
    
    if(a[0]>'9'){
        a[0]-=10;
        res+='1';
    }
    res+=a;
    
    for(int i=si(res)-1;i>=0;i--){
        if(res[i]>'9'){
            res[i]-=10;
            res[i-1]++;
        }
    }
    
    return res;
}

string sub(string a,string b){
    if(si(a)<si(b)) return "";
    if(si(a)==si(b)&&a<=b) return "";
    
    for(int i=0;i<si(b);i++){
        if(a[si(a)-1-i]<b[si(b)-1-i]){
            a[si(a)-1-i-1]--;
            a[si(a)-1-i]+=10;
        }
        a[si(a)-1-i]-=int(b[si(b)-1-i]-'0');
    }
    
    for(int i=si(a)-1;i>=0;i--){
        if(a[i]<'0'){
            a[i-1]--;
            a[i]+=10;
        }
    }
    int i=0;
    while(a[i]=='0') i++;
    
    return a.substr(i);
}

ll cntnum(ll x){
    ll res=0;
    ll y=1,keta=0;
    while(1){
        if(x<y){
            res+=(x-y/10+1)*keta;
            return res;
        }else{
            res+=(y-y/10)*keta;
            y*=10;
            keta++;
        }
    }
}

string X;
string Y[20];
ll mae[20];

void make(){
    for(int t=1;t<=1030;t++){
        X+=to_string(t);
    }
    ll A=1000;
    int t=0;
    while(A<=10000000000000000LL){
        for(ll i=A-100;i<=A+100;i++){
            Y[t]+=to_string(i);
        }
        mae[t]=cntnum(A-101);
        A*=10;
        t++;
    }
}

void solve(){
    string S;cin>>S;
    if(S=="#") exit(0);
    for(int i=0;i+si(S)-1<si(X);i++){
        string T=X.substr(i,si(S));
        if(S==T){
            cout<<i+1<<"\n";
            return;
        }
    }
    
    ll ans=INF;
    for(int t=0;t<20;t++){
        for(int i=0;i+si(S)-1<si(Y[t]);i++){
            string T=Y[t].substr(i,si(S));
            if(S==T){
                chmin(ans,mae[t]+i+1);
            }
        }
    }
    
    bool zero=true;
    for(char c:S) if(c!='0') zero=false;
    
    if(zero){
        string C="1"+S;
        cout<<cntnum(stoll(C)-1)+2<<"\n";
        return;
    }
    
    for(int len=4;len<=16;len++){
        if(len>si(S)) continue;
        for(int i=1;i<=len;i++){
            if(i>si(S)) continue;
            if(i+len<=si(S)){
                if(S[i]=='0') continue;
                string A=S.substr(i,len);
                string B=sub(A,"1");
                if(si(B)!=si(A)) continue;
                B=B.substr(si(B)-i,i);
                string C=S.substr(0,i);
                if(B!=C) continue;
                bool ok=true;
                for(int j=i+len;j<si(S);j+=len){
                    A=add(A,"1");
                    if(si(A)!=len){
                        ok=false;
                        break;
                    }
                    if(j+len<=si(S)){
                        string B=S.substr(j,len);
                        if(A!=B) ok=false;
                        if(!ok) break;
                    }else{
                        A=A.substr(0,si(S)-j);
                        string B=S.substr(j);
                        if(A!=B) ok=false;
                        if(!ok) break;
                    }
                }
                if(ok){
                    A=S.substr(i,len);
                    chmin(ans,cntnum(stoll(A)-1)+1-i);
                }
            }else{
                string A=S.substr(0,i);
                A=add(A,"1");
                if(si(A)!=i) A=A.substr(1);
                int l=max(len,0),r=min(len+i,si(S));
                if(l<r){
                    string AA=A;
                    A=A.substr(l-len,r-l);
                    string B=S.substr(l,r-l);
                    if(A!=B) continue;
                    string C=S.substr(i,l-i)+AA;
                    if(C[0]=='0') continue;
                    chmin(ans,cntnum(stoll(C)-1)+1-i);
                }else{
                    string C=S.substr(i)+A;
                    if(C[0]=='0') continue;
                    chmin(ans,cntnum(stoll(C)-1)+1-i);
                }
            }
        }
    }
    
    cout<<ans<<"\n";
    return;
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    make();
    
    while(1){
        solve();
    }
}
accuracy: 1
time_ms: 30
memory_kb: 3544
final_rank: 12

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const LL INF=1LL<<60;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
	if(vec_n==-1)vec_n=vec_s.size();
	for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
	int n=v1.size();
	for(int i=0;i<n;i++){
		if(i)os<<" ";
		os<<v1[i];
	}
	return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	os<<p.first<<" "<<p.second;
	return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
	int n=v1.size();
	for(int i=0;i<n;i++)is>>v1[i];
	return is;
}
template<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){
	is>>p.first>>p.second;
	return is;
}

namespace sol{

	LL str2int(string& sa,int x,int y){
		int i;
		LL s=0;
		for(i=x;i<y;i++){
			s*=10;
			s+=sa[i]-'0';
		}
		return s;
	}

	LL leng(LL a){
		LL i,j;
		LL s=0;
		for(i=1,j=1;i<a;i*=10,j++){
			s+=j*(min(a,i*10)-i);
		}
		return s;
	}

	void solve(){
		const int N=16;
		int n,m;
		LL i,j,k,l;
		LL a,b,c,d;
		string sa,sb,sc;
		while(cin>>sa){
			if(sa=="#")return;
			LL s=INF;
			n=sa.size();
			for(i=0;i<n;i++){
				for(j=i+1;j<=n && j<=i+N;j++){
					a=str2int(sa,i,j);
					if(a==0)continue;
					b=max(a-i,1LL);
					c=leng(b);
					sb="";
					for(k=b;k<a;k++){
						sb+=to_string(k);
					}
					b=(LL)sb.size()-i;
					c+=b;
					if(b<sb.size())sb=sb.substr(b);
					for(k=a;sb.size()<sa.size();k++){
						sb+=to_string(k);
					}
					sb=sb.substr(0,sa.size());
					if(sa==sb)s=min(s,c);
				}
			}
			
			if(sa.size()<=N){
				a=str2int(sa,0,sa.size());
				if(sa[0]=='0'){
					a+=pow(10LL,sa.size());
					s=min(s,leng(a)+1);
				}else s=min(s,leng(a));
			}

			for(i=1;i<n && i<=N;i++){
				if(N<=n-i)continue;
				a=str2int(sa,0,i);
				a++;//suffix
				for(j=0,c=1;j<i;j++)c*=10;
				a%=c;
				if(sa[i]=='0')continue;
				b=str2int(sa,i,n);//prefix
				for(j=0,k=1;j<=i;j++,k*=10){
					d=b*k+a%k;
					if((1LL<<53)<d)break;
					if(d-1<c/10)continue;
					if(d%c==a)s=min(s,leng(d)-i);
				}
			}
			cout<<s+1<<endl;
		}
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	sol::solve();
}
accuracy: 1
time_ms: 90
memory_kb: 3860
final_rank: 13

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


ll POW[17];
string line;

string get_string(ll num){

	string ret;
	stack<ll> S;

	while(true){
		S.push(num%10);
		num /= 10;
		if(num == 0)break;
	}

	while(!S.empty()){
		ret.append(to_string(S.top()));
		S.pop();
	}

	return ret;
}

ll getNUM(string tmp_str){

	ll ret = 0;

	for(int i = 0; i < tmp_str.length(); i++){
		ret = 10*ret+(tmp_str[i]-'0');
	}

	return ret;
}

ll get_pos(ll num){

	ll ret = 0;
	ll tmp_num = 0;

	for(int digit = 1; digit <= 16; digit++){

		if(tmp_num+9*POW[digit-1] >= num){
			ret += (num-POW[digit-1])*digit;
			break;
		}
		tmp_num += 9*POW[digit-1];
		ret += digit*9*POW[digit-1];
	}
	return ret;
}

bool is_match(string A,string B,int left,int right){

	for(int i = left; i <= right; i++){
		if(A[i] != B[i]){
			return false;
		}
	}
	return true;
}

bool is_all_9(string tmp_str){

	for(int i = 0; i < tmp_str.length(); i++){
		if(tmp_str[i] != '9'){
			return false;
		}
	}

	return true;
}

void func(){

	int LENGTH = line.length();
	ll ans = HUGE_NUM;

	if(getNUM(line) == 0){

		ans = get_pos(getNUM("1"+line))+2;
		printf("%lld\n",ans);
		return;
	}

	ll tmp_num;
	int tmp_length;

	for(int digit = 1; digit <= min(LENGTH,16); digit++){
		for(int start_pos = 0; start_pos < digit && start_pos+digit <= LENGTH; start_pos++){

			if(line[start_pos] == '0')continue;

			string tmp_str = line.substr(start_pos,digit);
			tmp_num = getNUM(tmp_str);

			if(start_pos > 0){
				string left_str = get_string(tmp_num-1);
				tmp_str = left_str+tmp_str;

				tmp_str = tmp_str.substr(tmp_str.length()-(start_pos+digit));
				if(!is_match(line,tmp_str,0,start_pos+digit-1)){
					continue;
				}
			}
			tmp_length = tmp_str.length();

			for(int diff = 1; tmp_str.length() < LENGTH; diff++){
				tmp_str += get_string(tmp_num+diff);
			}
			tmp_str = tmp_str.substr(0,LENGTH);

			if(is_match(line,tmp_str,0,LENGTH-1)){
				ans = min(ans,get_pos(tmp_num)-start_pos+1);
			}
		}
		if(ans != HUGE_NUM)break;
	}

	if(LENGTH >= 31){
		printf("%lld\n",ans);
		return;
	}

	int min_digit,max_digit = 16;

	for(int left_right = 0; left_right <= LENGTH-2; left_right++){

		string left_str = line.substr(0,left_right+1);
		string right_str = line.substr(left_right+1);

		if(right_str[0] == '0')continue;

		min_digit = max(left_str.length(),right_str.length())+1;

		for(int digit = min_digit; digit <= max_digit && left_str.length()+right_str.length() >= digit; digit++){

			string tmp_diff = left_str.substr(left_str.length()-(digit-right_str.length()));
			string line_tail = tmp_diff;
			tmp_num = getNUM(tmp_diff);
			tmp_diff = get_string(tmp_num+1);
			while(tmp_diff.length() < (digit-right_str.length())){
				tmp_diff = "0"+tmp_diff;
			}
			tmp_diff = tmp_diff.substr(tmp_diff.length()-(digit-right_str.length()));

			string tmp_right = right_str+tmp_diff;
			string tmp_left = get_string(getNUM(tmp_right)-1);
			tmp_left = tmp_left.substr(0,tmp_left.length()-left_str.length())+left_str;

			if(getNUM(tmp_right)-getNUM(tmp_left) == 1){
				ans = min(ans,get_pos(getNUM(tmp_right))-left_right);
				break;
			}
		}
	}

	printf("%lld\n",ans);
}

int main(){

	POW[0] = 1;

	for(int i = 1; i <= 16; i++){
		POW[i] = POW[i-1]*10;
	}

	while(true){

		getline(cin,line);

		if(line[0] == '#')break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3236
final_rank: 8

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


ll POW[17];
string line;

//数字を文字列に変換する関数
string get_string(ll num){

	string ret;
	stack<ll> S;

	while(true){
		S.push(num%10);
		num /= 10;
		if(num == 0)break;
	}

	while(!S.empty()){
		ret.append(to_string(S.top()));
		S.pop();
	}

	return ret;
}

//文字列を数値に変換する関数
ll getNUM(string tmp_str){

	ll ret = 0;

	for(int i = 0; i < tmp_str.length(); i++){
		ret = 10*ret+(tmp_str[i]-'0');
	}

	return ret;
}

//numが何桁目に出現するのかを取得する関数
ll get_pos(ll num){

	ll ret = 0;
	ll tmp_num = 0;//数字の累計

	for(int digit = 1; digit <= 16; digit++){ //数字の桁数

		if(tmp_num+9*POW[digit-1] >= num){ //numがdigit桁の場合
			ret += (num-POW[digit-1])*digit;
			break;
		}
		tmp_num += 9*POW[digit-1];
		ret += digit*9*POW[digit-1]; //digit桁の数は、9*POW[digit-1]個ある
	}
	return ret;
}

bool is_match(string A,string B,int left,int right){

	for(int i = left; i <= right; i++){
		if(A[i] != B[i]){
			return false;
		}
	}
	return true;
}

bool is_all_9(string tmp_str){

	for(int i = 0; i < tmp_str.length(); i++){
		if(tmp_str[i] != '9'){
			return false;
		}
	}

	return true;
}

void func(){

	int LENGTH = line.length();
	ll ans = HUGE_NUM;

	if(getNUM(line) == 0){ //全ての桁が0の場合

		ans = get_pos(getNUM("1"+line))+2;
		printf("%lld\n",ans);
		return;
	}

	ll tmp_num;
	int tmp_length;

	//★任意の数字が文字列中に完全に含まれる場合
	for(int digit = 1; digit <= min(LENGTH,16); digit++){
		for(int start_pos = 0; start_pos < digit && start_pos+digit <= LENGTH; start_pos++){ //1番左の数字を調べる

			if(line[start_pos] == '0')continue;

			string tmp_str = line.substr(start_pos,digit);
			tmp_num = getNUM(tmp_str);

			if(start_pos > 0){ //開始桁が0でないなら、左に数字を足す
				string left_str = get_string(tmp_num-1);
				tmp_str = left_str+tmp_str;
				//末尾start_pos+digit桁を切り出す
				tmp_str = tmp_str.substr(tmp_str.length()-(start_pos+digit));
				if(!is_match(line,tmp_str,0,start_pos+digit-1)){ //ここまででlinetと不一致ならcontinue
					continue;
				}
			}
			tmp_length = tmp_str.length();

			//lineの長さ以上になるまで、右に数字を付け足す
			for(int diff = 1; tmp_str.length() < LENGTH; diff++){
				tmp_str += get_string(tmp_num+diff);
			}
			tmp_str = tmp_str.substr(0,LENGTH); //先頭からLENGTH文字切り出す

			if(is_match(line,tmp_str,0,LENGTH-1)){
				ans = min(ans,get_pos(tmp_num)-start_pos+1);
				//★★start_posによっては、同じ桁のより良い回が見つかる可能性があるため、breakしない★
			}
		}
		if(ans != HUGE_NUM)break; //最小の場所を探しているので、桁が小さいものほど適しているはず
	}

	if(LENGTH >= 31){ //少なくとも片方が,長さ16になるのでreturn
		printf("%lld\n",ans);
		return;
	}

	//printf("mid:%lld\n",ans);

	int min_digit,max_digit = 16;

	//★任意の数字が文字列中に部分的にしか存在していない場合
	for(int left_right = 0; left_right <= LENGTH-2; left_right++){ //左側の数字の、右端のループ
	//for(int left_right = 5; left_right <= 5; left_right++){ //左側の数字の、右端のループ

		string left_str = line.substr(0,left_right+1);
		string right_str = line.substr(left_right+1);

		if(right_str[0] == '0')continue;

		min_digit = max(left_str.length(),right_str.length())+1;

		//printf("\nleft:%s right:%s min_digit:%d\n",left_str.c_str(),right_str.c_str(),min_digit);

		for(int digit = min_digit; digit <= max_digit && left_str.length()+right_str.length() >= digit; digit++){ //右側の数字の桁数のループ

			string tmp_diff = left_str.substr(left_str.length()-(digit-right_str.length())); //不足分を、left_strの末尾から切り出す
			//printf("tmp_diff:%s\n",tmp_diff.c_str());
			string line_tail = tmp_diff;
			tmp_num = getNUM(tmp_diff); //数値化
			//printf("tmp_num:%lld\n",tmp_num);
			tmp_diff = get_string(tmp_num+1);
			while(tmp_diff.length() < (digit-right_str.length())){ //桁数が足りないなら、0を付け足す
				tmp_diff = "0"+tmp_diff;
			}
			//printf("tmp_diff:%s\n",tmp_diff.c_str());
			tmp_diff = tmp_diff.substr(tmp_diff.length()-(digit-right_str.length())); //桁の繰り上がりがあり得るので、末尾を取り出す
			//printf("末尾切り出し tmp_diff:%s\n",tmp_diff.c_str());

			string tmp_right = right_str+tmp_diff;
			//printf("tmp_right:%s\n",tmp_right.c_str());
			string tmp_left = get_string(getNUM(tmp_right)-1); //右の文字を-1したもの
			//printf("tmp_left:%s\n",tmp_left.c_str());
			tmp_left = tmp_left.substr(0,tmp_left.length()-left_str.length())+left_str;
			//printf("final tmp_left:%s\n",tmp_left.c_str());

			if(getNUM(tmp_right)-getNUM(tmp_left) == 1){
				//printf("Match! tmp_left:%s,tmp_right:%s\n",tmp_left.c_str(),tmp_right.c_str());
				ans = min(ans,get_pos(getNUM(tmp_right))-left_right);
				break; //lineのある分割では、より桁が小さい場合の解が適切
			}
		}
	}

	printf("%lld\n",ans);
}

int main(){

	POW[0] = 1;

	for(int i = 1; i <= 16; i++){
		POW[i] = POW[i-1]*10;
	}

	while(true){

		getline(cin,line);

		if(line[0] == '#')break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3172
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
long long search(long long Z){
	string W=to_string(Z);
	long long H1=9,H2=1,H3=1;
	for(int i=0;i<(int)W.size()-1;i++){H2+=H1*(i+1);H1*=10;H3*=10;}
	H1/=10;
	long long K=H2+(Z-H3)*W.size();
	return K;
}
long long solve(string S){
	string T="-";
	for(int i=1;i<=10009;i++)T+=to_string(i);
	for(int i=0;i<=(int)(T.size()-S.size());i++){
		if(T.substr(i,S.size())==S)return i;
	}
	long long minx=1LL<<62;
	for(int i=5;i<=min((int)S.size(),17);i++){
		for(int j=0;j<i;j++){
			if(S[j]=='0')continue;
			string Y=S.substr(j,S.size()-j);
			if((int)Y.size()>i)Y=Y.substr(0,i);
			if((int)Y.size()<i){
				while((int)Y.size()<i){Y+=S[j-(i-Y.size())];}
				int R=j-1,D=Y.size()-1;
				while(R>=0 && D>=0){Y[D]=S[R];R--;D--;}
				long long J=stoll(Y);J++;Y=to_string(J);
			}
			string R="";int J=0;
			for(int k=-30;k<=30;k++){if(k==0)J=R.size();R+=to_string(stoll(Y)+k);}
			R=R.substr(J-j,S.size());
			if(R!=S)continue;
			minx=min(minx,search(stoll(Y))-j);
		}
	}
	return minx;
}
int main(){
	while(true){
		string U;cin>>U;if(U=="#")break;
		long long ret=1LL<<62;
		if(U[0]=='0'){
			for(int k=0;k<17;k++){
				for(int i=1;i<=9;i++){
					string ADD="";ADD+=(char)('0'+i);
					for(int j=0;j<k;j++)ADD+="0";
					ret=min(ret,solve(ADD+U)+1);
				}
			}
		}
		else ret=solve(U);
		cout<<ret<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 3370
memory_kb: 3228
final_rank: 14

code_snippet: #include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char str[110];
int b[110];
long long pow10[17];
char to[110];
char rg[210]="1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162";
int main(){
	pow10[0]=1;
	for(int i=1;i<17;i++)pow10[i]=pow10[i-1]*10;
	while(1){
		scanf("%s",str);
		if(str[0]=='#')break;
		long long ret=9999999999999999LL;
		int n=strlen(str);
		bool OK=true;
		for(int i=0;i<n;i++)if(rg[i]!=str[i])OK=false;
		if(OK){
			printf("1\n");continue;
		}
		bool oz=true;
		for(int i=0;i<n;i++)if(str[i]!='0')oz=false;
		if(oz){
			long long at=2;
			for(int i=1;i<=n;i++)at+=(pow10[i]-pow10[i-1])*i;
			printf("%lld\n",at);continue;
		}
		for(int i=1;i<=n;i++){
			for(int j=0;j<i;j++){
				if(str[j]=='0')continue;
				long long fi=0;
				for(int k=0;k<i-j;k++){
					fi*=10;
					fi+=str[j+k]-'0';
				}
				long long la=0;
				for(int k=0;k<j;k++){
					la*=10;
					la+=str[k]-'0';
				}
				long long s=la+1;
				long long t=fi;
			//	printf("%lld %lld\n",s,t);
				while(s%10==0)s/=10;
				while(t%10==0)t/=10;
				long long f=fi*pow10[j]+la;
				if(s==1&&(j==0||la)){
					if(t==1){
						f=pow10[i]-1;
					}else{
						f=(fi-1)*pow10[j]+la;
					}
				}
				f++;
				long long F=f;
				bool ok=true;
				int at=j;
				while(at<n){
					sprintf(to,"%lld",f);
					int len=strlen(to);
					for(int k=0;k<len;k++){
						if(at>=n)break;
						if(to[k]!=str[at])ok=false;
						at++;
					}
					f++;
				}
				if(ok){
					long long at=-j;
					for(int k=1;k<i;k++){
						at+=(pow10[k]-pow10[k-1])*k;
					}
					at+=(F-pow10[i-1])*i;
					if(at<0)continue;
					ret=min(ret,at+1);
				}
			}
		}
		printf("%lld\n",ret);
	}
}
accuracy: 1
time_ms: 50
memory_kb: 984
final_rank: 1

code_snippet: #include<cstdio>
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>

using namespace std;

string get(long long start){
	string res="";
	for(int i=0;;i++){
		stringstream ss;
		ss<<start+i;
		string add;
		ss>>add;
		res+=add;
		if(res.size()>140) break;
	}
	return res;
}

long long tens[18];

long long count(long long x){
	if(x<10) return x;
	int dig=lower_bound(tens,tens+18,x)-tens;
	dig--;
	long long res=0;
	for(int i=0;i<dig;i++){
		res+=tens[i]*9*(i+1);
	}
	long long rem=x-tens[dig];
	res+=rem*(dig+1);
//	cout<<x<<" "<<res<<"\n";
	return res+1;
}

long long solve2(int n){
	long long res=0;
	for(int i=0;i<n;i++) res+=tens[i]*9*(i+1);
	res+=2;
	return res;
}

bool all_zero(string str){
	for(int i=0;i<str.size();i++) if(str[i]!='0') return false;
	return true;
}

long long solve(string str){
	if(str=="1") return 1;
	int N=str.size();
	int n=min(N,33);
	long long res=-1;
	for(int i=0;i<n;i++) for(int j=i+1;j<=min(n,i+16);j++){
		long long cur=0;
		for(int k=i;k<j;k++){
			cur*=10;
			cur+=(str[k]-'0');
		}
	//	if(i==0&&j==n) cout<<"cur="<<cur<<"\n";
		if(cur-1<=0) continue;
		string cand=get(cur-1);
		int id=-1;
	//	cout<<cand<<"\n";
		for(int m=0;m+str.size()<cand.size();m++){
			bool ok=true;
			for(int p=0;p<str.size();p++){
				if(cand[m+p]!=str[p]){
					ok=false;
					break;
				}
			}
			if(ok){
				id=m;
				break;
			}
		}
		if(id==-1) continue;
		long long tmp=count(cur-1);
		tmp+=id;
		if(res==-1||res>tmp) res=tmp;
	}
	if(str.size()<=34){
	//	cout<<"str="<<str<<"\n";
		string firstHalf,secondHalf;
		for(int i=1;i+1<str.size();i++){
			firstHalf="";secondHalf="";
			for(int j=0;j<i;j++) firstHalf+=str[j];
			for(int j=i;j<str.size();j++) secondHalf+=str[j];
			string nxt=firstHalf;
		//	cout<<"i="<<i<<"fi="<<firstHalf<<"se="<<secondHalf<<"\n";
			bool flg=false;
			for(int j=firstHalf.size()-1;j>=0;j--){
				if(firstHalf[j]!='9'){
					flg=true;
					nxt[j]++;
					for(int k=j+1;k<firstHalf.size();k++) nxt[k]='0';
					break;
				}
			}
			if(flg==false){
				nxt="";
				for(int k=0;k<firstHalf.size();k++) nxt+='0';
			}
	//		cout<<"cand:"<<firstHalf<<" "<<nxt<<" "<<secondHalf<<"\n";
			int m=min(nxt.size(),secondHalf.size());
			for(int j=m;j>=0;j--){
				string str1=nxt.substr(0,j);
				string str2=secondHalf.substr(secondHalf.size()-j,j);
				if(str1!=str2) continue;
				string cur=secondHalf+nxt.substr(j,1000);
				if(cur.size()>=17) break;
				long long t;
				stringstream ss;
				ss<<cur;
				ss>>t;
				string a=get(t-1);
				int id=-1;
		//		cout<<t<<"\n";
				for(int k=0;k+str.size()<a.size();k++){
					bool ok=true;
					for(int m=0;m<str.size();m++){
						if(str[m]!=a[m+k]){
							ok=false;
							break;
						}
					}
					if(ok){
						id=k;
						break;
					}
				}
				if(id!=-1){
		//			cout<<t<<" "<<id<<"\n";
					long long a=count(t-1);
					if(res==-1||res>a+id) res=a+id;
				}
			}
		}
	}
	if(all_zero(str)){
		long long tmp=solve2(str.size());
		if(res==-1||res>tmp) res=tmp;
	}
	return res;
}

int main(){
	tens[0]=1;
	for(int i=1;i<18;i++) tens[i]=tens[i-1]*10;
	string str;
	while(true){
		cin>>str;
		if(str=="#") break;
		long long ans=solve(str);
		cout<<ans<<"\n";
	}
	return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 1240
final_rank: 5

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long lli;

const lli W = 17;
lli ten[W];
lli begin[W];

lli getA(string a, string b) {
  int n = a.size();
  for(int i = n-1, t = 1; i >= 0; --i) {
    if(a[i] == 'X') break;
    int d = (a[i]-'0') + t;
    a[i] = '0' + d % 10;
    t = d / 10;
  }
  for(int i = n-1; i >= 0; --i) {
    if(a[i] == 'X' && b[i] == 'X') {
      if(i == 0) a[i] = '1';
      else a[i] = '0';
    } else if(a[i] == 'X') {
      a[i] = b[i];
    } else if(b[i] == 'X') {
      //a[i] = a[i];
    } else if(a[i] == b[i]) {
      //a[i] = a[i];
    } else {
      return -1LL;
    }
  }
  return atoll(a.c_str()) - 1LL;
}

string toString(lli x) {
  stringstream ss;
  ss << x;
  string res;
  ss >> res;
  return res;
}

lli getPos(lli x) {
  if(x == 0) return 0;
  lli digits = 0;
  lli t = x;
  while(t) {
    ++digits;
    t /= 10;
  }
  lli dist = x - ten[digits-1];
  return begin[digits-1] + dist * digits;
}

int main() {
  // init
  ten[0] = 1;
  for(int i = 1; i < W; ++i) {
    ten[i] = ten[i-1] * 10LL;
  }
  begin[0] = 1;
  for(int i = 1; i < W; ++i) {
    begin[i] = begin[i-1] + i * (ten[i] - ten[i-1]);
  }

  string s;
  while(cin >> s && s != "#") {
    int N = s.size();
    vector<lli> as;
    vector<lli> bs;
    for(int m = 1; m <= N; ++m) {
      for(int L = m; L < W; ++L) {
        as.push_back(ten[L]-1LL);
        bs.push_back(L-m);

        string a = s.substr(0, m);
        string b = s.substr(m, min((int)s.size()-m, L));
        while(a.size() < L) a.insert(0, "X");
        while(b.size() < L) b += 'X';
        if(a[0] == '0' || b[0] == '0') continue;
        lli A = getA(a, b);
        if(A != -1LL) {
          as.push_back(A);
          bs.push_back(L-m);
        }
      }
    }

    lli res = 10000000000000001LL;
    for(int i = 0; i < as.size(); ++i) {
      string t;
      lli na = as[i];
      while((int)t.size()-bs[i] < (int)s.size()) {
        t += toString(na++);
      }
      if(t.substr(bs[i], s.size()) == s) {
        res = min(res, getPos(as[i]) + bs[i]);
      }
    }
    cout << res << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 1256
final_rank: 4

code_snippet: #include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;

const ll INF=(1LL<<63)-1;

ll to_ll(const string &s){
	ll a;
	sscanf(s.c_str(),"%lld",&a);
	return a;
}

string to_string(ll a){
	char s[32];
	sprintf(s,"%lld",a);
	return s;
}

ll calc_index(ll a){
	ll res=0,ten=1,len=1;
	while(1){
		if(a<10*ten){
			res+=(a-ten)*len;
			return res;
		}
		res+=9*len*ten;
		ten*=10;
		len++;
	}
}

int main(){
	for(string s;cin>>s,s[0]!='#';){
		int n=s.length();

		if(count(s.begin(),s.end(),'0')==s.length()){ // s="000...0" のときは例外
			cout<<calc_index(to_ll('1'+s))+2<<endl;
			continue;
		}

		ll ans=INF;
		int ofset;
		// 注目する数が s に完全に含まれるケース ( 341235123612 など )
		rep(i,n) for(int j=i+1;j<=n;j++) {
			string t=s.substr(i,j-i);
			if(t[0]=='0' || t.length()>16) continue;

			ll a=to_ll(t);
			int len=0;
			string r=t;
			for(int k=1;k<30;k++){
				if(a-k>0){
					r=to_string(a-k)+r;
					len+=to_string(a-k).length();
				}
				r=r+to_string(a+k);
			}
			if(len-i>=0 && r.substr(len-i,s.length())==s && ans>a) ans=a, ofset=i;
		}

		// そうでないケース 3456712345
		for(int i=1;i<n;i++){
			string t=s.substr(0,i),r=s.substr(i);
			if(r[0]=='0' || t.length()>16 || r.length()>16) continue;

			t=to_string(to_ll(t)+1);
			if(t[0]=='1' && count(t.begin()+1,t.end(),'0')==t.length()-1) t=t.substr(1);

			rep(d,min(t.length(),r.length())+1){
				if(r.substr(r.length()-d,d)==t.substr(0,d) && t.length()+r.length()-d<=16){
					ll a=to_ll(r+t.substr(d));
					if(ans>a) ans=a, ofset=i;
				}
			}
		}

		cout<<calc_index(ans)-ofset+1<<endl;
	}

	return 0;
}
accuracy: 1
time_ms: 740
memory_kb: 1288
final_rank: 6

code_snippet: #include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#pragma comment(linker, "/STACK:400000000")

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(it=X.begin();it!=X.end();it++)
#define iss istringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<n;i++)
#define pi pair<int,int>
#define pb push_back
#define sec second
#define sst stringstream
#define sz size()
#define vc vector
typedef vc<int> vi;
typedef vc<ll> vl;
typedef vc<string> vs;

string s;
int n;

ll toLL(string s){
	if(s=="")return 0;
	iss is(s);
	ll x;
	is>>x;
	return x;
}

string toStr(ll x){
	sst ss;
	ss<<x;
	return ss.str();
}

int main(){
	while(cin>>s && s!="#"){
		ll ans=0;
		n=s.sz;
		int allzero=1;
		rep(i,n)if(s[i]!='0')allzero=0;
		if(allzero)s="1"+s,n++,ans++;
		
		ll firstnum=INF*INF,pos=-1;
		
		rep(a,16)rep2(b,1,n+1){
			if(a+b>16)continue;
			if(a==0 && s[0]=='0')continue;
			if(b<n && s[b]=='0')continue;
			string pref=s.substr(0,b);
			string rest=s.substr(b);
			if(rest.sz>=a){
				rep(i,2){
					ll head=toLL(rest.substr(0,a))-i;
					if(head<0)continue;
					int oneless=0;
					if(toStr(head).sz == a-1) oneless=1;
					ll num=toLL(toStr(head)+pref);
					ll num0=num;
					//cout<<a<<" "<<b<<" "<<i<<" "<<head<<" "<<num<<endl;
					while(rest.sz>0){
						num++;
						string t=toStr(num);
						rep(i,min(rest.sz,t.sz)){
							if(rest[i] != t[i]){
								goto out;
							}
						}
						if(rest.sz<t.sz)rest="";
						else rest=rest.substr(t.sz);
					}
					out:;
					//cout<<"["<<rest<<"]"<<endl;
					if(rest==""){
						if(num0 < firstnum || num0 == firstnum && a - oneless < pos){
							//cout<<a<<" "<<b<<" "<<i<<" "<<head<<" "<<num<<endl;
							firstnum=num0;
							pos=a - oneless;
						}
					}
				}
			}
		}
		//cout<<firstnum<<" "<<pos<<endl;
		ans+=pos;
		ll p10=10,dig=1;
		while(1){
			if(firstnum-1<=p10-1){
				ans+=dig*(firstnum-1 - p10/10+1);
				break;
			}
			ans+=dig*(p10-1 - p10/10+1);
			p10*=10;
			dig++;
		}
		cout<<ans+1<<endl;
	}
}
accuracy: 1
time_ms: 60
memory_kb: 1232
final_rank: 3

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n;
char input[1000];
char temp[1000];
char temp2[1000];
char temp3[1000];
char champernowne[1000000];

void PrintChampernowne(int index, int len) {
  REP(i, len) {
    putchar(champernowne[index + i - 1]);
  }
  puts("");
}

ll Convert(pair<ll, ll> ans) {
  //cout << ans.first << " " << ans.second << endl;
  ll ret = ans.second + 1;
  ll base = 1;
  ll cnt = 1;
  ans.first--;
  while (true) {
    if (base * 10 <= ans.first) {
      ret += cnt * base * 9;
    } else {
      ret += cnt * (ans.first - base + 1);
      break;
    }
    cnt++;
    base *= 10;
  }
  return ret;
}

void Plus(char *str) {
  (*str)++;
  if (*str > '9') { *str = '0'; Plus(str - 1); }
}

int main() {
  FOREQ(i, 1, 10000) {
    sprintf(champernowne + strlen(champernowne), "%d", i);
  }
  while (scanf("%s", input), input[0] != '#') {
    pair<ll, ll> ans(1LL << 60, 1LL << 60);
    n = strlen(input);
    // one
    if (n < 17) {
      if (input[0] == '0') {
        sprintf(temp, "1%s", input);
        ans = min(ans, make_pair(atoll(temp), 1LL));
      } else {
        ans = min(ans, make_pair(atoll(input), 0LL));
      }
    }
    // two
    if (n < 34) {
      FOREQ(i, 1, n) {
        if (input[i] == '0') { continue; }
        int start = 100;
        MEMSET(temp2, '0');
        memcpy(temp2 + start, input, i);
        temp2[start + i] = 0;
        sprintf(temp, "%s", temp2 + start);
        Plus(temp2 + start + i - 1);
        if (temp2[start - 1] == '1') { start--; }
        if ((int)strlen(temp) >= n) { continue; }
        FOREQ(j, 0, i + 1) {
          if (i + j > n) { continue; }
          memcpy(temp2 + start - (n - i - j), input + i, n - i - j);
          sprintf(temp + i, "%s", temp2 + start - (n - i - j));
          temp[n] = '\0';
          if (strcmp(input, temp) == 0) {
            ll v = atoll(temp2 + start - (n - i - j)) - 1;
            sprintf(temp3, "%lld", v);
            ll len = strlen(temp3);
            if (len >= 17) { continue; }
            ll offset = len - i;
            //cout << i << " " << j << " " << v << " " << offset << endl;
            ans = min(ans, make_pair(v, offset));
          }
        }
      }
    }
    // more than
    FOREQ(i, 1, n) {
      FOREQ(j, 1, n) {
        memcpy(temp, input + j, i);
        temp[i] = '\0';
        ll start = atoll(temp) - 1;
        if (start <= 0) { continue; }
        ll v = start;
        sprintf(temp2, "%lld", v);
        int len = strlen(temp2);
        if (len < j) { continue; }
        ll offset = len - j;
        sprintf(temp, "%s", temp2 + offset);
        int cnt = 1;
        while ((int)strlen(temp) < n) {
          v++;
          cnt++;
          sprintf(temp + strlen(temp), "%lld", v);
        }
        if (cnt < 3) { continue; }
        temp[n] = 0;
        if (strcmp(input, temp) == 0) {
          ans = min(ans, make_pair(start, offset));
        }
      }
    }
    // edge
    FOREQ(i, 1, 17) {
      REP(offset, i) {
        ll start = 1;
        REP(j, i) { start *= 10; }
        start--;
        REP(j, i - offset) {
          temp[j] = '9';
        }
        temp[i - offset] = '\0';
        ll v = start;
        while ((int)strlen(temp) < n) {
          v++;
          sprintf(temp + strlen(temp), "%lld", v);
        }
        temp[n] = 0;
        if (strcmp(temp, input) == 0) {
          ans = min(ans, make_pair(start, (ll)offset));
        }
      }
    }
    //PrintChampernowne(2887, 10);
    printf("%lld\n", Convert(ans));
    //cout << input << endl;
    //PrintChampernowne(Convert(ans), n);
  }
}
accuracy: 1
time_ms: 210
memory_kb: 940
final_rank: 2