Tifa's CP Library

:heavy_check_mark: src/test_cpverifier/library-checker/aplusb.stoi.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#include "../../code/fast/str2uint_mod.hpp"
#include "../../code/fast/u32tostr.hpp"

constexpr u64 MOD = u64(1e9) + 1;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  strn s, t;
  std::cin >> s >> t;
  char ans[16] = {0};
  tifa_libs::u32tostr(tifa_libs::str2uint_mod(s, MOD) + tifa_libs::str2uint_mod(t, MOD), ans);
  std::cout << ans;
  return 0;
}
#line 1 "src/test_cpverifier/library-checker/aplusb.stoi.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"

#line 1 "src/code/fast/str2uint_mod.hpp"



#line 1 "src/code/math/mul_mod_u.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};
inline const auto fn_is0 = [](auto x) { return x == 0; };


#line 5 "src/code/math/mul_mod_u.hpp"

namespace tifa_libs::math {

constexpr u64 mul_mod_u(u64 a, u64 b, u64 mod) {
  if (std::bit_width(a) + std::bit_width(b) <= 64) return a * b % mod;
  else return (u64)((u128)a * b % mod);
}

}  // namespace tifa_libs::math


#line 1 "src/code/fast/str2uint_si64.hpp"



#line 1 "src/code/bit/bswap.hpp"



namespace tifa_libs::bit {

// From GCC lib
template <class T>
constexpr T bswap(T x) {
  if constexpr (sizeof(T) == 2) return __builtin_bswap16(x);
  if constexpr (sizeof(T) == 4) return __builtin_bswap32(x);
  if constexpr (sizeof(T) == 8) return __builtin_bswap64(x);
  if constexpr (sizeof(T) == 16) return __builtin_bswap128(x);
  // if constexpr (sizeof(T) == 16) return (__builtin_bswap64(x >> 64) | (static_cast<T>(__builtin_bswap64(x)) << 64));
}

}  // namespace tifa_libs::bit


#line 6 "src/code/fast/str2uint_si64.hpp"

namespace tifa_libs {

constexpr u32 str2uint_si64(const char* const s) {
  u64 _ = *((u64*)(s));
  if constexpr (std::endian::native == std::endian::big) _ = bit::bswap(_);
  _ = (_ & 0x0F0F0F0F0F0F0F0F) * 2561 >> 8;
  _ = (_ & 0x00FF00FF00FF00FF) * 6553601 >> 16;
  _ = (_ & 0x0000FFFF0000FFFF) * 42949672960001 >> 32;
  return (u32)_;
}

}  // namespace tifa_libs


#line 6 "src/code/fast/str2uint_mod.hpp"

namespace tifa_libs {

constexpr u64 str2uint_mod(strnv s, u64 mod) {
  u32 n = (u32)s.size();
  if (!n) return 0;
  u64 ans = 0;
  for (u32 i = 0; i + 8 <= n; i += 8) ans = (math::mul_mod_u(ans, 100000000, mod) + str2uint_si64(s.data() + i)) % mod;
  for (auto _ = s.data() + (n & u32(-8)); _ < s.data() + n; ++_) ans = (math::mul_mod_u(ans, 10, mod) + (*_ & 15)) % mod;
  return ans;
}

}  // namespace tifa_libs


#line 1 "src/code/fast/u32tostr.hpp"



#line 5 "src/code/fast/u32tostr.hpp"

namespace tifa_libs {

constexpr u32 u32tostr_si16(u64 x, char *s) {
  if (x <= 9) {
    *s = (char)(x | 0x30);
    return 1;
  } else if (x <= 99) {
    u64 low = x;
    u64 ll = ((low * 103) >> 9) & 0x1E;
    low += ll * 3;
    ll = ((low & 0xF0) >> 4) | ((low & 0x0F) << 8);
    *(u16 *)s = (u16)(ll | 0x3030);
    return 2;
  }
  return 0;
}
constexpr u32 u32tostr_si32(u64 x, char *s) {
  u64 low = 0, ll = 0;
  u32 digits = 0;
  if (x <= 99) return u32tostr_si16(x, s);
  low = x;
  digits = (low > 999) ? 4 : 3;
  ll = ((low * 5243) >> 19) & 0xFF;
  low -= ll * 100;
  low = (low << 16) | ll;
  ll = ((low * 103) >> 9) & 0x1E001E;
  low += ll * 3;
  ll = ((low & 0x00F000F0) << 28) | (low & 0x000F000F) << 40;
  ll |= 0x3030303000000000;
  u8 *p = (u8 *)&ll;
  if (digits == 4) *(u32 *)s = *(u32 *)(&p[4]);
  else {
    *(u16 *)s = *(u16 *)(&p[5]);
    *(((u8 *)s) + 2) = p[7];
  }

  return digits;
}

constexpr u32 u32tostr(u64 x, char *s) {
  u64 low = 0, ll = 0;
  u32 digits = 0;
  if (x <= 9999) return u32tostr_si32(x, s);
  if (x < 100000000) {
    if ((low = x) > 999999) digits = (low > 9999999) ? 8 : 7;
    else digits = (low > 99999) ? 6 : 5;
  } else {
    u64 high = (x * 0x55E63B89) >> 57;
    low = x - (high * 100000000);
    digits = u32tostr_si16(high, s);
    digits += 8;
  }

  ll = (low * 109951163) >> 40;
  (low -= ll * 10000) |= ll << 32;
  ll = ((low * 5243) >> 19) & 0x000000FF000000FF;
  low -= ll * 100;
  low = (low << 16) | ll;
  ll = ((low * 103) >> 9) & 0x001E001E001E001E;
  low += ll * 3;
  ll = ((low & 0x00F000F000F000F0) >> 4) | (low & 0x000F000F000F000F) << 8;
  ll = (ll >> 32) | (ll << 32) | 0x3030303030303030;

  if (digits >= 8) memcpy(s + digits - 8, &ll, 8);
  else {
    u32 d = digits;
    char *s1 = s, *pll = &(((char *)&ll)[8 - digits]);
    if (d >= 4) {
      memcpy(s1, pll, 4);
      s1 += 4, pll += 4, d -= 4;
    }
    if (d >= 2) {
      memcpy(s1, pll, 2);
      s1 += 2, pll += 2, d -= 2;
    }
    if (d > 0) *(u8 *)s1 = *(u8 *)pll;
  }
  return digits;
}

}  // namespace tifa_libs


#line 5 "src/test_cpverifier/library-checker/aplusb.stoi.test.cpp"

constexpr u64 MOD = u64(1e9) + 1;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  strn s, t;
  std::cin >> s >> t;
  char ans[16] = {0};
  tifa_libs::u32tostr(tifa_libs::str2uint_mod(s, MOD) + tifa_libs::str2uint_mod(t, MOD), ans);
  std::cout << ans;
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++-12 example_00 :heavy_check_mark: AC 8 ms 4 MB
g++-12 example_01 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_00 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_01 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_02 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_03 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_04 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_05 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_06 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_07 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_08 :heavy_check_mark: AC 7 ms 4 MB
g++-12 random_09 :heavy_check_mark: AC 7 ms 4 MB
Back to top page