Problem:
桁和 (Digit Sum)
問題文
JOI 君は初めに
1
以上
N
以下のある整数を持っていた．JOI 君は以下の操作を
0
回以上行ったところ，持っている整数が
N
になった．
持っている整数を十進法で表したときの各桁の和を，持っている整数に足す．
N
が与えられるので，JOI 君が初めに持っていた可能性のある整数の個数を求めるプログラムを作成せよ．
制約
1 ≦ N ≦ 1 000 000
．
N
は整数である．
入力
入力は以下の形式で標準入力から与えられる．
N
出力
JOI 君が初めに持っていた可能性のある整数の個数を
1
行で出力せよ．
入出力例
入力例 1
13
出力例 1
4
例えば JOI 君が初めに整数
5
を持っており，
3
回操作を行った場合
5 → 10 → 11 → 13
と変化する．JOI 君が初めに持っていた可能性のある整数は
5,10,11,13
の
4
個のみである．
入力例 2
20
出力例 2
1
入力例 3
2019
出力例 3
449
情報オリンピック日本委員会
作 『第 19 回日本情報オリンピック JOI 2019/2020 一次予選競技課題』

Submitted solutions:

code_snippet: #include <iostream>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <unordered_map>
#include <algorithm>
#include <numeric>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int f(int x){
  int res=x;
  while(x>0){
    res+=x%10;
    x/=10;
  }
  return res;
}

int main(){
  int n;
  cin >> n;
  vector<vector<int>> rev(n+1);
  for(int x=1;x<=n;x++){
    int y=f(x);
    if (y<=n){
      rev[y].push_back(x);
    }
  }
  vector<bool> visited(n+1,false);
  queue<int> que;
  visited[n]=true;
  que.push(n);

  int cnt=0;
  while(!que.empty()){
    int cur=que.front();
    que.pop();
    cnt++;
    for(int prev:rev[cur]){
      if(!visited[prev]){
        visited[prev]=true;
        que.push(prev);
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 54936
final_rank: 15

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll N;
  cin >> N;
  set<ll> s;
  s.insert(N);
  rrep(i, 1, N) {
    ll t = i;
    ll sum = 0;
    while (t > 0) {
      sum += t % 10;
      t /= 10;
    }
    if (s.count(i+sum)) {
      s.insert(i);
    }
  }
  print(len(s));
  return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 34188
final_rank: 20

code_snippet: #include <stdio.h>

long int getext(long int inval){
  long int i;
  int chkdig = 0;
  long int chkdif;
  long int extval;

  /* 入力値の桁数から下に検索する範囲を算出 */
  i = inval;
  while(i){
	i = i / 10;
	chkdig++;
  }
  /* 入力値桁数x9 */
  chkdif = chkdig * 9;
  return inval - chkdif;
}


long int fact(long int chkval, long int cnt){
  /* 変数宣言 */
  long int i;
  long int extval;
  long int j;
  long int dig;
  long int sum;

  i = chkval;

  /* 入力値の桁数から下に検索する範囲を算出 */
  extval = getext(chkval);

  /* 入力値から0までの間で調査 */
  while(i > extval){
	  i = i - 1;

    /*i+iの各桁の和を取得*/
    j = i;
    sum = j;
    while(j){
      dig = j % 10;
      sum = sum + dig;
      j = j / 10;
    }

    if(sum == chkval){   /* 和と入力値が一致する場合 */
      cnt = cnt + 1;     /* カウントを加算 */
      cnt = fact(i,cnt); /* 再起関数再呼出 */
    }

  }
  return cnt;
}

int main() {
  /* 変数宣言 */
  long int inval = 0;      /* 入力値 */
  long int cnt = 1;        /* 対象値件数 */

  scanf("%ld",&inval);     /* 入力値取得 */
  cnt = fact(inval, cnt);  /* 再起関数呼出 */
  printf("%ld\n", cnt);    /* 結果出力 */
  
  return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 4452
final_rank: 13

code_snippet: #include<stdio.h>
int dSum(int n){
	if(n < 10) return n;
	return dSum(n/10) + n%10;
}
int main(void)
{
	int n,sum;
	scanf("%d",&n);
	int ans[1000002] = {0};

	for(int i=0; i<n; i++){
	    ans[i] += 1;
		sum = dSum(i);
		if(i+sum <= n) ans[i+sum] += ans[i];
	}
    ans[n] += 1;
	printf("%d\n",ans[n]);
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6664
final_rank: 1

code_snippet: #include <stdio.h>

long int fact(long int inval, long int cnt){
  /* 変数宣言 */
  long int i;
  long int j;
  long int dig;
  long int sum;
  long int chkval;

  i = inval;
  chkval = inval;

  /* 入力値から0までの間で調査 */
  while(i > 0){
	  i = i -1;

    /* 入力値より60小さくなった時点でもう
       候補はないため、処理を抜ける */
    if (i < chkval - 60){
      break;
    }

    /*i+iの各桁の和を取得*/
    j = i;
    sum = j;
    while(j){
      dig = j % 10;
      sum = sum + dig;
      j = j / 10;
    }

    if(sum == chkval){   /* 和と入力値が一致する場合 */
      cnt = cnt + 1;     /* カウントを加算 */
      cnt = fact(i,cnt); /* 再起関数再呼出 */
    }

  }
  return cnt;
}

int main() {
  /* 変数宣言 */
  long int inval = 0;      /* 入力値 */
  long int cnt = 1;        /* 対象値件数 */

  scanf("%ld",&inval);     /* 入力値取得 */
  cnt = fact(inval, cnt);  /* 再起関数呼出 */
  printf("%ld\n", cnt);    /* 結果出力 */
  
  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 4480
final_rank: 14

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr int MOD = 1000000007;
constexpr int MOD_N = 998244353;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using ll = int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void setp(int n) {
    std::cout << std::fixed << std::setprecision(n);
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 3 "a.cpp"

int main(void) {
    int n;
    cin >> n;
    vector<int> dp(n + 1, 1);
    rep(i, n) {
        string s = to_string(i);
        int x = 0;
        for (auto c : s) x += stoi(string(1, c));
        if (i + x <= n)
            dp[i + x] += dp[i];
    }
    co(dp[n]);

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 6972
final_rank: 7

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


void solve(){
	int n;
	cin >> n;
	vector<bool> ok(n + 1, false);
	ok[n] = true;
	int ans = 0;

	auto nex=[&](int x){
		if(x <= 0) return -1;
		int ret = x;
		int y = x;
		while(y > 0){
			ret += y % 10;
			y /= 10;
		}
		return ret;
	};

	for(int i = n; i >= 1; i--){
		if(!ok[i]) continue;
		ans++;
		for(int j = 1; j <= 54; j++){
			if(nex(i - j) == i) ok[i - j] = true;
		}
	}
	cout << ans << "\n";

}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    t = 1;
    // cin >> t;
    while(t--) solve();
}
accuracy: 1
time_ms: 200
memory_kb: 3584
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VS = vector<string>;
using P = pair<ll,ll>;
using VP = vector<P>;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define out(x) cout << x << endl
#define dout(x) cout << fixed << setprecision(3) << x << endl
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)(x.size())
#define re0 return 0
#define pcnt __builtin_popcount
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//const int INF = 1e9;
const ll INF = 1e18;
//using mint = modint1000000007;
//using mint = modint998244353;
int di[4] = {1,0,-1,0};
int dj[4] = {0,1,0,-1};

set<ll> st;

ll ds(ll n){
    ll res = 0;
    while(n != 0){
        res += n%10;
        n /= 10;
    }
    return res;
}

void rec(ll n){
    if(n <= 0) return;
    if(st.count(n)) return;
    st.insert(n);
    for(int i = 1;i <= 60;i++){
        if(n-i <= 0) continue;
        if(i == ds(n-i)) rec(n-i);
    }
    return;
}

int main(){
    int n;
    cin >> n;
    rec(n);
    out(sz(st));
}
accuracy: 1
time_ms: 270
memory_kb: 36016
final_rank: 19

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0674.cc: 桁和 (Digit Sum)
 */

#include<cstdio>
#include<queue>
#include<algorithm>
 
using namespace std;

/* constant */

/* typedef */

typedef queue<int> qi;

/* global variables */

/* subroutines */

inline int dsum(int n) {
  int sum = 0;
  while (n > 0) sum += n % 10, n /= 10;
  return sum;
}

inline int maxdsum(int n) {
  int sum = 0;
  while (n >= 10) sum += 9, n /= 10;
  return sum + n - 1;
}

/* main */

int main() {
  //freopen("input.txt", "r", stdin);
  //freopen("output.txt", "w", stdout);

  int n;
  scanf("%d", &n);

  qi q;
  q.push(n);
  int cnt = 0;

  while (! q.empty()) {
    int u = q.front(); q.pop();
    cnt++;

    int mxd = min(u, max(dsum(u), maxdsum(u)));
    for (int d = 1; d <= mxd; d++) {
      int v = u - d;
      if (dsum(v) == d) q.push(v);
    }
  }

  printf("%d\n", cnt);
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 2968
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <iomanip>
#include <limits>
#include <bitset>
// #include <atcoder/all>

using namespace std;
using ll = long long;

#define _overload(_1, _2, _3, name, ...) name
#define rep1(n) rep3(____, 0, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) for (ll i=(a); i<(ll)(b); i++)
#define rep(...) _overload(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep(i, a, b) for(ll i = a; i >= b; i--)
#define fore(i, a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define pii pair<int, int>
#define pli pair<ll, int>
#define pll pair<ll, ll>

#define UNIQUIFY(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define out(x) {cout << x << "\n";}
#define dbg(x) {cerr << #x << ": " << x << "\n";}
#define inc(x, l, r) ((l) <= (x) and (x) < (r))

const int INFI = 1 << 30;
const ll INFL = 1LL << 60;

// const ll MOD = 998244353;
// const ll MOD = 1000000007; // 1e9 + 7
// using mint = atcoder::modint998244353;
// using mint = atcoder::modint1000000007;

template <typename T>
bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; }
template <typename T>
bool chmin(T& a, const T& b){ if(b < a){ a = b; return 1; } return 0; }

template<class... T>
constexpr auto min(T... a) {
    return min(initializer_list<common_type_t<T...>>{a...});
}
template<class... T>
constexpr auto max(T... a) {
    return max(initializer_list<common_type_t<T...>>{a...});
}

template<typename T>
ostream& operator << (ostream& os, vector<T> &v) {
    int sz = (int)v.size();
    for (int i = 0; i < sz; i++) {
        os << v[i] << (i == sz - 1 ? "" : " ");
    }
    return os;
}

int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
int dy[] = {-1, 0, 1, 0, 1, -1, -1, 1};

// using Graph = vector<vector<int> >;



int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin >> N;

    auto dgt_sum = [](int m) {
        int res = 0;
        while (m) {
            res += m % 10;
            m /= 10;
        }
        return res;
    };

    vector<int> dp(N+1, 1);
    rep(i, 1, N) {
        if (i + dgt_sum(i) <= N) {
            dp[i+dgt_sum(i)] += dp[i];
        }
    }

    cout << dp[N] << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 6952
final_rank: 2

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<int,int> P;

int dp[1000010];
int s(ll x){
	if(x<10) return x;
	return s(x/10)+x%10;
}

int main(void){
	int i;

	cin.tie(0);  ios_base::sync_with_stdio(false);

	int N;
	cin >> N;

	dp[N]=1;
	for(i=N-1;i>=1;i--){
		int m=i+s(i);
		if(m<=N && dp[m]==1) dp[i]=1;
	}

	int ans=0;
	for(i=1;i<=N;i++) ans+=dp[i];

	cout << ans << endl;

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7048
final_rank: 3

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<int,int> P;

ll s(ll x){
	if(x<10) return x;
	return s(x/10)+x%10;
}


ll par[1000010],rk[1000010],sz[1000010];

void init(ll n){
	REP(i,n+1){
		par[i]=i;
		rk[i]=0;
		sz[i]=1;
	}
}

ll find(ll x){
	if(par[x]==x){
	   return x;
	}else{
	  return par[x]=find(par[x]);
	}
}

void unite(ll x,ll y){
	x=find(x); y=find(y);
	if(x==y) return;
	if(rk[x]<rk[y]){
		par[x]=y;
		sz[y]=sz[x]+sz[y];
	}else{
		par[y]=x;
		sz[x]=sz[x]+sz[y];
		if(rk[x]==rk[y]) rk[x]++;
	}
}

bool same(ll x,ll y){

	return find(x)==find(y);
}

ll size(ll x){

	return sz[find(x)];
}

int main(void){

	cin.tie(0);  ios_base::sync_with_stdio(false);

	ll N;
	cin >> N;

	init(N);
	for(ll i=1;i<N;i++) if(i+s(i)<=N) unite(i,i+s(i));

	cout << size(N) << endl;

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

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<int,int> P;

int s(int x){
	if(x<10) return x;
	return s(x/10)+x%10;
}


ll par[1000010],rk[1000010],sz[1000010];

void init(ll n){
	REP(i,n+1){
		par[i]=i;
		rk[i]=0;
		sz[i]=1;
	}
}

ll find(ll x){
	if(par[x]==x){
	   return x;
	}else{
	  return par[x]=find(par[x]);
	}
}

void unite(ll x,ll y){
	x=find(x); y=find(y);
	if(x==y) return;
	if(rk[x]<rk[y]){
		par[x]=y;
		sz[y]=sz[x]+sz[y];
	}else{
		par[y]=x;
		sz[x]=sz[x]+sz[y];
		if(rk[x]==rk[y]) rk[x]++;
	}
}

bool same(ll x,ll y){

	return find(x)==find(y);
}

ll size(ll x){

	return sz[find(x)];
}

int main(void){
	int i,j;

	cin.tie(0);  ios_base::sync_with_stdio(false);

	ll N;
	cin >> N;

	init(N);
	for(i=1;i<N;i++) unite(i,i+s(i));

	cout << size(N) << endl;

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 26584
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0;i<int(n);i++)
typedef long long ll;
typedef pair<int,int> P;

int s(int x){
	if(x<10) return x;
	return s(x/10)+x%10;
}


ll par[1000010],rk[1000010],sz[1000010];

void init(ll n){
	REP(i,n+1){
		par[i]=i;
		rk[i]=0;
		sz[i]=1;
	}
}

ll find(ll x){
	if(par[x]==x){
	   return x;
	}else{
	  return par[x]=find(par[x]);
	}
}

void unite(ll x,ll y){
	x=find(x); y=find(y);
	if(x==y) return;
	if(rk[x]<rk[y]){
		par[x]=y;
		sz[y]=sz[x]+sz[y];
	}else{
		par[y]=x;
		sz[x]=sz[x]+sz[y];
		if(rk[x]==rk[y]) rk[x]++;
	}
}

bool same(ll x,ll y){

	return find(x)==find(y);
}

ll size(ll x){

	return sz[find(x)];
}

int main(void){
	int i,j;

	cin.tie(0);  ios_base::sync_with_stdio(false);

	ll N;
	cin >> N;

	init(N);
	unite(N,N);
	for(i=1;i<N;i++){
		unite(i,i+s(i));
	}

	ll ans=0;
	for(i=1;i<=N;i++){
	    if(same(i,N)) ans++;
	}

	cout << ans << endl;

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 26560
final_rank: 10

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q) {
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q) {
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}
ll modPow(long long a, long long n, long long p) {
  if (n == 0) return 1; // 0乗にも対応する場合
  if (n == 1) return a % p;
  if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
  long long t = modPow(a, n / 2, p);
  return (t * t) % p;
}


int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll N;
    cin>>N;
    vvll G(N+1);
    for(ll i=1;i<=N;i++){
        ll j=i;
        ll n=i;
        rep(k,22){
            n+=j%10;
            j/=10;
        }
        if(n<=N)G[n].push_back(i);
    }
    vector<bool>seen(N+1,false);
    ll an=0;
    queue<ll> Q;
    Q.push(N);
    while(!Q.empty()){
        ll p=Q.front();
        Q.pop();
        if(seen[p])continue;
        seen[p]=1;
        an++;
        for(auto v:G[p]){
            if(!seen[v])Q.push(v);
        }
    }    
    cout<<an<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 54868
final_rank: 16

code_snippet: #include <iostream>
#include <set>
#include <queue>
#include <cmath>

using namespace std;

int main()
{
	int N; cin >> N;
	int I = ((int) log10((double) N) + 1) * 9;
	set<int> S;
	queue<int> Q;
	for (Q.push(N); !Q.empty(); Q.pop()) {
		auto n = Q.front();
		if (S.count(n) > 0) continue;
		S.insert(n);
		for (int i = max(1, n - I), k = i; i < n; ++i, k = i) {
			for (int j = i; j > 0; j /= 10) k += j % 10;
			if (k == n) Q.push(i);
		}
	}
	cout << S.size() << endl;

	return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 34524
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
#include <numeric>
#include <tuple>

using namespace std;

typedef long long ll;

constexpr int INF = 1 << 30;
constexpr ll INFLL = 1LL << 60;
constexpr ll MOD = 1'000'000'007;

vector<vector<int>> G;

int DFS(int v, int p) {
    int Ret = 1;
    for (int u : G[v]) {
        if (u == p) continue;
        Ret += DFS(u, v);
    }
    return Ret;
}

int main()
{
    int N;
    cin >> N;

    G.resize(N + 1);
    for (int n = 1; n < N; ++n) {
        int t = n;
        int s = 0;
        while (t) {
            s += t % 10;
            t /= 10;
        }
        s += n;
        if (s <= N) {
            G[s].emplace_back(n);
        }
    }
    cout << DFS(N, -1) << endl;

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 56512
final_rank: 17

code_snippet: #include<iostream>

using namespace std;

int DigitSum(int n)
{
	int res = 0;
	while (n != 0)
	{
		res += n % 10;
		n /= 10;
	}

	return res;
}

int main()
{
	int N;
	cin >> N;
	int* dp = new int[N + 1];
	fill(dp, dp + N + 1, 1);
	for (int i = 1; i <= N; i++)
	{
		if (i + DigitSum(i) <= N)
			dp[i + DigitSum(i)] += dp[i];
	}
	cout << dp[N] << endl;

	//system("pause");
}
accuracy: 1
time_ms: 10
memory_kb: 7256
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <stack>
using namespace std;

int N;
vector<int> v((1 << 20), 0);

int func(int x)
{
    stack<int> stk;
    for(int cnt = 1;;cnt++)
    {
        stk.push(x);
        v[x] = 1;
        if (x == N)
        {
            while (!stk.empty())
            {
                v[stk.top()] = 2;
                stk.pop();
            }
            return cnt;
        }

        int y = x;
        while (y > 0)
        {
            x += (y % 10);
            y /= 10;
        }

        if (x > N) break;
        if (v[x] == 2)
        {
            while (!stk.empty())
            {
                v[stk.top()] = 2;
                stk.pop();
            }
            return cnt;
        }
        if (v[x] == 1) break;
    }
    return 0;
}

int main() {
    cin >> N;
    int total = 0;
    for (int i = 1; i <= N; ++i)
    {
        if (v[i] > 0) continue;
        total += func(i);
    }
    cout << total << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 7272
final_rank: 5

code_snippet: #define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<sstream>
#include<regex>
#include<random>
using namespace std;
#define int long long
#define uint unsigned long long
const int mod=1e9+7;
const int Mod=998244353;
const int inf=mod*mod;
const double eps=1e-9;
int ans[1000001];
signed main(){
	int n;
	cin>>n;
	fill(ans,ans+n+1,1);
	for(int i=1;i<=n;i++){
		string s=to_string(i);
		int sum=accumulate(s.begin(),s.end(),0)-'0'*s.size()+i;
		if(sum<=n)
			ans[sum]+=ans[i];
	}
	cout<<ans[n]<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 11276
final_rank: 6