POJ 1047 Round and Round We Go cycle number New Solution

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Title Description:

Given a string representation of the high precision number, determine whether it is recyclable. If the length of the string num is assumed to be n, then num is multiplied from 1 to N, and if each resulting result contains a character element that is the same as a, then it is recyclable.

Problem Solving Ideas:

At first glance, the solution to this problem is to calculate the result of Num multiplied by 1 to n by using the multiply of the high precision number, and then compare with Num. This method is relatively simple and can get the correct result, but the efficiency is not very good. For the number of loops, our most common is repeating decimal, and the solution of this problem is derived from this.

There are three theorems in the elementary number theory:

Euler's theorem: set A, m as integers, m>1, (a,m) = 1, then a^φ (m) ≡1 (mod m).
Number of integers: A, M is an integer, m>1, (a,m) =1,k is the smallest positive integer that makes the a^k≡1 (mod m), the number of times that K is called a pair of modulo m.
Number theorem: The number of times a pair of modulo m is set to K,n is a positive integer satisfying a^n≡1 (mod m), then K|n.

The proofs of these three theorems are introduced in the book of number theory, and they can be consulted on their own to learn more about them. Using the above theorem is:

1/7=0.142857142857 ...

The number of bits in the loop is 6, multiplying the 10^6

=>10^6/7=142857.142857142857 ...

= (10^6-1)/7=142857

=>999999/7=142857

For the other num, if the number of BITS is N, if num* (n+1) Gets a result of n 9, then this number is recyclable.

#include <iostream> #include <string>using namespace Std;int main () {string Num;bool flag = True;int I, n, C, t;w  Hile (cin >> num) {flag = True;n = Num.size () + 1;c = 0; t = 0;for (i = n-2; I >= 0; i--) {t = num[i]-' 0 '; if ((t * n + c)% = 9)  //Determine if the results are all 9{flag = False;break;} c = (T * n + C)/10;} if (flag) {cout << num << "is cyclic" << Endl;} Else{cout << num << "is not cyclic" << Endl;} return 0;}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.