zoj 1073 Round and Round We Go

Round and Round We Go

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Problem

A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a ��cycle�� of the digits of the original number. That is, if you consider the number after the last digit to ��wrap around�� back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.

For example, the number 142857 is cyclic, as illustrated by the following table:

Write a program which will determine whether or not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, ��01�� is a two-digit number, distinct from ��1�� which is a one-digit number.)


Output

For each input integer, write a line in the output indicating whether or not it is cyclic.


Example

Input

142857
142856
142858
01
0588235294117647

Output

142857 is cyclic
142856 is not cyclic
142858 is not cyclic
01 is not cyclic
0588235294117647 is cyclic 

 

字符串相加即可

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <vector>
 5 #include <list>
 6 #include <string>
 7 #include <cstring>
 8 #include <cstdio>
 9 #include <algorithm>
10 #include <set>
11 #include <stack>
12 
13 using namespace std;
14 
15 bool isCyc(string s1, string s2)
16 {
17     if (s1.length() != s2.length())
18         return false;
19     for (int i = 0; i < s1.length(); i++)
20     {
21         string tmp = s1.substr(i, s1.length() - i) + s1.substr(0, i);
22         if (tmp == s2)
23             return true;
24     }
25 
26     return false;
27 }
28 
29 string add(string s1, string s2)
30 {
31     string res;
32     s1 = string(s1.rbegin(), s1.rend());
33     s2 = string(s2.rbegin(), s2.rend());
34     while (s1.length() < s2.length())
35         s1 += "0";
36     while (s2.length() < s1.length())
37         s2 += '0';
38     int carry = 0;
39     for (int i = 0; i < s1.length(); i++)
40     {
41         res += ((s1[i] - '0' + s2[i] - '0' + carry) % 10 + '0');
42         carry = (s1[i] - '0' + s2[i] - '0' + carry) / 10;
43     }
44     if (carry != 0)
45         res += "1";
46 
47     return string(res.rbegin(), res.rend());
48 }
49 
50 int main()
51 {
52     string s;
53     while (cin >> s)
54     {
55         string res = s;
56         int i;
57         for (i = 1; i < s.length(); i++)
58         {
59             res = add(res, s);
60             if (!isCyc(s, res))
61             {
62                 cout << s << " is not cyclic" << endl;
63                 break;
64             }
65         }
66         if (i == s.length())
67             cout << s << " is cyclic" << endl;
68     }
69 }

 

 


Source: Greater New York 2001

posted @ 2016-04-06 19:30  JackWang822  阅读(232)  评论(0编辑  收藏  举报