【解题报告】poj1047_Round and Round We Go(循环数)
2014-01-05 10:26
309 查看
题目链接:http://poj.org/problem?id=1047
Round and Round We Go
Description
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:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
Input
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.
Sample Input
Sample Output
Source
Greater New York 2001
大意:
输入一个长度为 2 to 60 的数值,判断其是否有周期性。周期性的定义根据题目中的描述。
思路:
判断n是否为周期数的方法:len为n的长度(根据题意,001 的长度为3),若满足 n*(len+1) = 9...9 (len个9) 并且 n+1 为素数时,n为周期数。
证明:
1/7 = 0.142857 142857 .....
1/11 = 0.09090909..
1/13 = 0.076923 076923 .....
规律为,当分母为素数时,所得结果都为循环小数。
以第一个式子 1/7 = 0.142857 142857 ..... 为例,因为后面小数的循环周期为6,所以不妨把它两边同时乘以 10^6 ,所得新式子为: 10^6/7 = 142857.142857 142857 ....
又因为 1/7 = 0.142857 142857 ..... 所以在 10^6/7 = 142857.142857 142857 .... 的基础上给分子减去1,就舍去了小数点后面的数值: 10^6/7 - 1/7 = 142857
化简得:(10^6-1)/7 = 142857 于是得到: 99999/7 = 142857 所以:142857 * 7 = 99999 成立。
因此可推出判断 n 是否有周期性的公式。
观察142857,我们用1至6的数来乘:
1´ 142857=142857
2´ 142857=285714
3´ 142857=428571
4´ 142857=571428
5´ 142857=714285
6´ 142857=857142
所得得结果是1、4、2、8、5、7六个数字依原来的次序循环排列只是开头的数字变动而已。这种数我们叫做循环数,其实这个数142857是由1/7所形成循环小数的循环节(1/7=0.142857142857142857…)。而所有循环数也都由某质数的倒数所形成之循环小数的循环节而得来的。下一个循环数是由质数17所形成的,1/17=0.0588235294117647…,而0588235294117647即为一循环数(2 0588235294117647=1176470588235294)。
会产生如此循环数的质数依序为7、17、19、23、29、47、59、61、97(<100)。
142857还有一个很有趣的性质。当142857乘以7时其乘积为一连串的9(142857´ 7=999999),而0588235294117647乘以17也是一连串的9。还有142857分成两半相加也是一连串的9(注:142+857=999),而0588235294117647分成两半相加: 05882352+ 94117647=99999999,这真是非常奇妙的巧合。
Round and Round We Go
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11632 | Accepted: 5409 |
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:
142857 *1 = 142857
142857 *2 = 285714
142857 *3 = 428571
142857 *4 = 571428
142857 *5 = 714285
142857 *6 = 857142
Input
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.
Sample Input
142857 142856 142858 01 0588235294117647
Sample Output
142857 is cyclic 142856 is not cyclic 142858 is not cyclic 01 is not cyclic 0588235294117647 is cyclic
Source
Greater New York 2001
大意:
输入一个长度为 2 to 60 的数值,判断其是否有周期性。周期性的定义根据题目中的描述。
思路:
判断n是否为周期数的方法:len为n的长度(根据题意,001 的长度为3),若满足 n*(len+1) = 9...9 (len个9) 并且 n+1 为素数时,n为周期数。
证明:
1/7 = 0.142857 142857 .....
1/11 = 0.09090909..
1/13 = 0.076923 076923 .....
规律为,当分母为素数时,所得结果都为循环小数。
以第一个式子 1/7 = 0.142857 142857 ..... 为例,因为后面小数的循环周期为6,所以不妨把它两边同时乘以 10^6 ,所得新式子为: 10^6/7 = 142857.142857 142857 ....
又因为 1/7 = 0.142857 142857 ..... 所以在 10^6/7 = 142857.142857 142857 .... 的基础上给分子减去1,就舍去了小数点后面的数值: 10^6/7 - 1/7 = 142857
化简得:(10^6-1)/7 = 142857 于是得到: 99999/7 = 142857 所以:142857 * 7 = 99999 成立。
因此可推出判断 n 是否有周期性的公式。
观察142857,我们用1至6的数来乘:
1´ 142857=142857
2´ 142857=285714
3´ 142857=428571
4´ 142857=571428
5´ 142857=714285
6´ 142857=857142
所得得结果是1、4、2、8、5、7六个数字依原来的次序循环排列只是开头的数字变动而已。这种数我们叫做循环数,其实这个数142857是由1/7所形成循环小数的循环节(1/7=0.142857142857142857…)。而所有循环数也都由某质数的倒数所形成之循环小数的循环节而得来的。下一个循环数是由质数17所形成的,1/17=0.0588235294117647…,而0588235294117647即为一循环数(2 0588235294117647=1176470588235294)。
会产生如此循环数的质数依序为7、17、19、23、29、47、59、61、97(<100)。
142857还有一个很有趣的性质。当142857乘以7时其乘积为一连串的9(142857´ 7=999999),而0588235294117647乘以17也是一连串的9。还有142857分成两半相加也是一连串的9(注:142+857=999),而0588235294117647分成两半相加: 05882352+ 94117647=99999999,这真是非常奇妙的巧合。
#include <cstdio> #include <cmath> #include <cstring> int main() { //freopen("in.txt","r",stdin); char s[70]; while(~scanf("%s",s)){ int len=strlen(s); int num[70]; int i=0,flag=0,i_num=0,j; while(s[i]){ if(s[i]!='0') flag=1; if(flag) num[i_num++]=s[i]-'0'; i++; } for(i=0;i<(len/2);i++){//分成两半相加和为9为循环数 if(s[i]+s[i+len/2]-'0'*2!=9){ flag=0; break; } } if(!flag){ printf("%s is not cyclic\n",s); continue; } int res[70]={0},ii=0; for(i=i_num-1;i>=0;i--){ res[ii]=(res[ii]+num[i]*(len+1)); res[ii+1]+=res[ii]/10; if(res[ii]) res[ii]%=10; ii++; } while(res[ii]){ res[ii+1]+=res[ii]/10; res[ii]%=10; ii++; } if(ii==len){ flag=0; for(j=0;j<ii;j++) if(res[j]!=9){ flag=1; break; } if(flag) printf("%s is not cyclic\n",s); else printf("%s is cyclic\n",s); } else printf("%s is not cyclic\n",s); } return 0; }
相关文章推荐
- OpenJudge百炼-2952-循环数 & poj-1047-Round and Round We Go-C语言-高精度计算
- poj1047 Round and Round We Go
- POJ1047 Round and Round We Go【高精度】
- UVALive2287 POJ1047 HDU1313 ZOJ1073 Round and Round We Go【大数+数学计算+水题】
- poj1047 Round and Round We Go
- POJ1047 Round and Round We Go
- HDU 1313 Round and Round We Go (Java大数)
- poj 1047 Round And Round We Go
- ZOJ 1073 Round and Round We Go
- poj 1047 Round and Round We Go 字符串的处理
- POJ 1049 Round and Round We Go 大数模拟
- poj 1047-Round and Round We Go
- POJ 1047 Round and Round We Go (大数乘法) 水
- POJ 1047 Round and Round We Go
- poj 1047 Round and Round We Go
- hdu1313 Round and Round We Go (大数乘法)
- POJ-1047 Round and Round We Go
- ZOJ Problem Set - 1073(Round and Round We Go)
- POJ 1047 - Round and Round We Go【暴力+精度】
- Round and Round We Go POJ 1047