diff --git a/ReciprocalSearch2.cpp b/ReciprocalSearch2.cpp index 727ed88..0348cc3 100644 --- a/ReciprocalSearch2.cpp +++ b/ReciprocalSearch2.cpp @@ -17,8 +17,7 @@ inline bool check(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8 a[5] = i6; a[6] = i7; a[7] = i8; - a[8] = i9; - a[9] = i10; + a[8] = i9; for (int k = 8; k >= 0; --k) { @@ -26,6 +25,8 @@ inline bool check(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8 return false; } + a[9] = i10; + mpz_class s1 = 1; for (int k = 0; k < 10; ++k) { s1 *= a[k]; @@ -39,18 +40,17 @@ inline bool check(int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8 return s1 == s2; } +mpz_t q1, r1; inline int ceil(const mpq_class& m) { - const mpz_class num = m.get_num(); - const mpz_class den = m.get_den(); - const mpz_class quotient = num / den; - if (quotient >= 100) + mpz_tdiv_qr(q1, r1, m.get_num().get_mpz_t(), m.get_den().get_mpz_t()); + if (mpz_cmp_si(q1, 100) > 0) return 100; - int r = quotient.get_si(); - if ((num % den) != 0) - ++r; + int ret = mpz_get_si(q1); + if (mpz_cmp_si(r1, 0) != 0) + ++ret; - return r; + return ret; } int main() { @@ -58,6 +58,13 @@ int main() { int total = 0; + mpz_init(q1); + mpz_init(r1); + + mpz_t q, r; + mpz_init(q); + mpz_init(r); + mpq_class ii[101]; for (int i = 1; i <= 100; i++) { @@ -109,15 +116,11 @@ int main() { if (m9 <= 0) continue; const mpq_class last = 1 / m9; + mpz_tdiv_qr(q, r, last.get_num().get_mpz_t(), last.get_den().get_mpz_t()); + if (mpz_cmp_si(r, 0) != 0) continue; + if (mpz_cmp_si(q, 100) > 0) continue; - const mpz_class num = last.get_num(); - const mpz_class den = last.get_den(); - if ((num % den) != 0) continue; - - const mpz_class quotient = num / den; - if (quotient > 100) continue; - - int i10 = quotient.get_si(); + const int i10 = mpz_get_si(q); if (i10 <= i9) continue; if (check(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10)) { @@ -134,6 +137,12 @@ int main() { } } + mpz_clear(q); + mpz_clear(r); + + mpz_clear(q1); + mpz_clear(r1); + const auto end = std::chrono::steady_clock::now(); printf("%.3f seconds\n", std::chrono::duration_cast(end - start).count() / 1000.0); } \ No newline at end of file diff --git "a/\347\255\224\346\241\210.txt" "b/\347\255\224\346\241\210.txt" index 13f444e..0586d9d 100644 --- "a/\347\255\224\346\241\210.txt" +++ "b/\347\255\224\346\241\210.txt" @@ -69012,4 +69012,4 @@ 69012: 5 6 7 9 10 12 15 18 20 42 69013: 5 6 7 9 10 14 15 18 20 28 69014: 5 6 8 9 10 12 15 18 20 24 -1507.170 seconds +1241.224 seconds