Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <int64_t m, int64_t n>
- struct nod;
- template <int64_t n>
- struct nod<0, n>
- {
- static const int64_t value = n;
- };
- template <int64_t m>
- struct nod<m, 0>
- {
- static const int64_t value = m;
- };
- template <int64_t m>
- struct nod<m, m>
- {
- static const int64_t value = m;
- };
- template <int64_t m>
- struct nod<m, 1>
- {
- static const int64_t value = 1;
- };
- template <int64_t n>
- struct nod<1, n>
- {
- static const int64_t value = 1;
- };
- template <bool m_odd, bool n_odd, int64_t m, int64_t n>
- struct nod_calc;
- template <int64_t m, int64_t n>
- struct nod
- {
- static const bool m_odd = m & 1;
- static const bool n_odd = n & 1;
- static const int64_t value = nod_calc<m_odd, n_odd, m, n>::value;
- };
- // Если m, n чётные, то НОД(m, n) = 2*НОД(m/2, n/2);
- template <int64_t m, int64_t n>
- struct nod_calc<false, false, m, n>
- {
- static const int64_t value = 2 * nod<m/2, n/2>::value;
- };
- // Если m чётное, n нечётное, то НОД(m, n) = НОД(m/2, n);
- template <int64_t m, int64_t n>
- struct nod_calc<false, true, m, n>
- {
- static const int64_t value = nod<m/2, n>::value;
- };
- // Если n чётное, m нечётное, то НОД(m, n) = НОД(m, n/2);
- template <int64_t m, int64_t n>
- struct nod_calc<true, false, m, n>
- {
- static const int64_t value = nod<m, n/2>::value;
- };
- // Если m, n нечётные, то НОД(m, n) = НОД(n, |m - n|).
- template <int64_t m, int64_t n>
- struct nod_calc<true, true, m, n>
- {
- static const int64_t mod = n>m ? n-m : m-n;
- static const int64_t value = nod<n, mod>::value;
- };
- template <bool, class R>
- struct reduce_accurate;
- template <bool, class R>
- struct reduce_inaccurate
- {
- typedef rational_t<(R::a >> 1), (R::b >> 1)> type_;
- typedef typename reduce_accurate<require_reduce<type_>::value, type_>::type type;
- };
- template <class R>
- struct reduce_inaccurate<false, R>
- {
- typedef R type;
- };
- template <bool, class R>
- struct reduce_accurate
- {
- const static int64_t new_a = R::a / nod<R::a, R::b>::value;
- const static int64_t new_b = R::b / nod<R::a, R::b>::value;
- typedef rational_t<new_a, new_b> new_type;
- typedef typename reduce_accurate<false, new_type>::type type;
- };
- template <class R>
- struct reduce_accurate<false, R>
- {
- typedef typename reduce_inaccurate<require_reduce<R>::value, R>::type type;
- };
- template <class R>
- struct reduce
- {
- typedef typename reduce_accurate<require_reduce<R>::value, R>::type type;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement