Skip to content

Instantly share code, notes, and snippets.

@PreSoichiSumi
Created April 26, 2016 08:03
Show Gist options
  • Save PreSoichiSumi/1b609abff755a7c80338707e018a3cca to your computer and use it in GitHub Desktop.
Save PreSoichiSumi/1b609abff755a7c80338707e018a3cca to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#include <X11/Intrinsic.h>
#include <iostream>
#include <string>
using namespace std;
#define all(c) ((c).begin()), ((c).end())
#define dump(c) cerr << "> " << #c << " = " << (c) << endl;
#define iter(c) __typeof((c).begin())
#define tr(i, c) for (iter(c) i = (c).begin(); i != (c).end(); i++)
#define REP(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define mp make_pair
#define fst first
#define snd second
#define pb push_back
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1 << 29;
const double EPS = 1e-10;
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;
}
template<class T>
ll stoi(T &str){
ll ret;
stringstream ss;
ss << str;
ss >> ret;
return ret;
}
template<class T>
string toString(T i) {
stringstream ss;
ss << i;
return ss.str();
}
struct prepare {
prepare() {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(8);
ios_base::sync_with_stdio(false);
}
} _prepare;
ll getNumOb1Str(ll num){
ll counter=0;
string str=toString(num);
for(int i=0;i<str.size();i++){
if(str.at(i)=='1')counter++;
}
return counter;
}
template<class T>
T getNumOb1(T num){
T counter=0;
for(T i=1;i<=num;i++){
counter+=getNumOb1Str(i);
}
return counter;
}
template<class T>
T power(T x,T y){
int res=1;
for(T i=0;i<y;i++){
res*=x;
}
return res;
}
ll to[10];
ll perOne[10];
void prep(string &str){
//0-10^i までカウントしたときに現れる1の数
to[0]=0;
to[1]=1;
REP(i,2,str.size())to[i]=to[i-1]*10+power(10,i-1);
//ll perOne[10];
perOne[0]=0;
perOne[1]=1;
REP(i,2,str.size())perOne[i]=perOne[i-1]*10+power(10,i-1);
}
ll getNumFast(string str){
ll res=0;
if(str.size()==0)return 0;
int val = str.at(0) - '0';
res += to[str.size() - 1]
+ perOne[str.size() - 1] * (val - 1);
if (val > 1) {
res += power(10, (int)str.size() - 1);
} else if (val == 1) {
string sub = str.substr(1);
if (sub.size() != 0) {
res += stoi(sub)+1;
} else {
res += 1;
}
}
res+=getNumFast(str.substr(1));
return res;
}
int main() {
string s;
cin>>s;
prep(s);
// ll i=stoi(s);
ll ans=getNumFast(s);
cout<<ans<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment