結果

問題 No.3277 Forever Monotonic Number
コンテスト
ユーザー eQe
提出日時 2025-12-19 06:33:57
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 695 ms / 4,000 ms
コード長 4,215 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,391 ms
コンパイル使用メモリ 290,632 KB
実行使用メモリ 25,216 KB
最終ジャッジ日時 2025-12-19 06:34:09
合計ジャッジ時間 11,881 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include<atcoder/modint>
#endif
using namespace std;
#define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__)
#define FO(n) for(ll IJK=n;IJK-->0;)
#define fo(i,...) for(auto[i,i##stop,i##step]=for_range<ll>(0,__VA_ARGS__);i<i##stop;i+=i##step)
#define fe(a,e,...) for(auto&&__VA_OPT__([)e __VA_OPT__(,__VA_ARGS__]):a)
#define defpp template<ostream&o=cout>void pp(const auto&...a){[[maybe_unused]]const char*c="";((o<<c<<a,c=" "),...);o<<'\n';}void epp(const auto&...a){pp<cerr>(a...);}
#define entry defpp void main();void main2();}int main(){my::io();my::main();}namespace my{
#define use_ml998244353 using ml=atcoder::modint998244353;
namespace my{
auto&operator<<(ostream&o,const atcoder::modint998244353&x){return o<<(int)x.val();}
void io(){cin.tie(nullptr)->sync_with_stdio(0);cout<<fixed<<setprecision(15);}
using ll=long long;
template<class T>constexpr auto for_range(T s,T b){T a=0;if(s)swap(a,b);return array{a-s,b,1-s*2};}
void lin(auto&...a){(cin>>...>>a);}
constexpr ll size2(auto x){x|=1;ll r=0;while(x>0)x>>=1,++r;return r;}
constexpr ll size10(auto x){x|=1;ll r=0;while(x>0)x/=10,++r;return r;}
template<class T>constexpr ll maxsize10(){return size10(numeric_limits<T>::max());}
template<class...A>using pack_back_t=tuple_element_t<sizeof...(A)-1,tuple<A...>>;
}
namespace my{
template<class T,class F=less<>>struct multiset:std::multiset<T,F>{
  multiset()=default;
};
template<class T,class F=less<>>struct set:multiset<T,F>{
  set()=default;
  set(const vector<T>&a){fe(a,e)emplace(e);}
  template<class...A>auto emplace(A&&...a){
    T x(std::forward<A>(a)...);
    if(auto p=this->find(x);p!=this->end())return std::pair{p,0};
    return std::pair{multiset<T,F>::emplace(x),1};
  }
};
}
namespace my{
template<class V>struct vec;
template<int D,class T>struct hvec_helper{using type=vec<typename hvec_helper<D-1,T>::type>;};
template<class T>struct hvec_helper<0,T>{using type=T;};
template<int D,class T>using hvec=hvec_helper<D,T>::type;
template<class V>struct vec:vector<V>{
  using vector<V>::vector;
  ll size()const{return vector<V>::size();}
};
template<class...A>requires(sizeof...(A)>=2)vec(const A&...a)->vec<hvec<sizeof...(A)-2,pack_back_t<A...>>>;
template<class T=ll>auto iota(ll l,ll r=0){if(l>r)swap(l,r);vec<T>v(r-l);std::iota(v.begin(),v.end(),l);return v;}
}
namespace my{
template<class T>struct pow_linear:vec<T>{pow_linear(T a,ll n):vec<T>(n+1,1){fo(i,n)(*this)[i+1]=(*this)[i]*a;}};
template<class T>struct pow10_linear:pow_linear<T>{
  vec<T>repunit_table;
  pow10_linear(ll n):pow_linear<T>(10,n),repunit_table(n+2){fo(i,n+1)repunit_table[i+1]=repunit_table[i]*10+1;}
};
template<class T=ll>T pow10(ll n){static const auto v=pow10_linear<T>(maxsize10<T>()-1);assert(n<v.size());return v[n];}
template<class T>struct pow_doubling:vec<T>{
  pow_doubling(T a,ll n):vec<T>(n+1,a){fo(i,n)(*this)[i+1]=(*this)[i]*(*this)[i];}
};
template<class T>struct pow10_doubling:pow_doubling<T>{
  vec<T>repunit_table;
  pow10_doubling(ll n):pow_doubling<T>(10,n),repunit_table(n+2,1){fo(i,n+1)repunit_table[i+1]=((*this)[i]+1)*repunit_table[i];}
  T repunit(ll k)const{
    T res=0;
    fo(i,size2(k))if(k>>i&1)res=res*(*this)[i]+repunit_table[i];
    return res;
  }
};
template<class T=ll>constexpr T at10(auto x,auto i){return x/pow10<T>(i)%10;}
}
namespace my{
template<class T>T digit_sum(T x,ll b){T r=x;while(x)r-=(x/=b)*(b-1);return r;}
template<class T>T digit_sum10(T x){return digit_sum(x,10);}
template<class T>T slice10(T x,ll l,ll r){return x%pow10<T>(r)/pow10<T>(l);}
template<class T>T range10(T x,ll l,ll r){return slice10<T>(x,l,r)*pow10<T>(l);}
}
namespace my{entry
void main(){
  auto next_mono=[&](ll x){
    fo(i,size10(x)+1){
      if(at10(x,i)<9){
        ll y=range10(x,i+1,size10(x));
        fo(j,i+1)y+=(at10(x,i)+1)*pow10(j);
        return y;
      }
    }
    assert(0);
  };

  set<ll>monos(iota(10));
  for(ll x=11;x<1e15;x=next_mono(x))if(monos.contains(digit_sum10(x)))monos.emplace(x);

  use_ml998244353
  pow10_doubling<ml>p10(60);

  LL(T);
  FO(T){
    LL(N);
    ll k=*monos.lower_bound(N+1);
    ll q=(k-(N+1))/8,r=(k-(N+1))%8;
    pp(p10.repunit(N+1)+r*ml(10).pow(q)+p10.repunit(q)*8);
  }
}}
0