Reciprocal cycles
Problem 26
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/2 = 0.5 1/3 = 0.(3) 1/4 = 0.25 1/5 = 0.2 1/6 = 0.1(6) 1/7 = 0.(142857) 1/8 = 0.125 1/9 = 0.(1) 1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
欧拉项目,不过也可以用大数除法,感觉欧拉比较神……本来想用的是除法模拟的,后面看到学长用的欧拉,就跟着用下下……
这里还有一个重要的结论
对于一个形如 1 / x的分数
找出最小的999...99使得x能整除999....999 那么 这个999....9有多少位,
这个分数转化为小数循环部分就有多少位。
不过前提是要把2和5因子都除掉。
因为形如999...99是永远没法被5和2整除的。
那么为什么这样就对呢?
我们都知道0.(9)这个循环小数跟1是等价的
所以我们的目标就是让1 /x转化后的小数乘以x然后== 0.(9)
然后实际上就是一堆9凑成的数能被x整除就找到循环节了。
那么用这个我们又得到一种方法。 不过好像还是最坏n^ 2的
#include <iostream>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
int abc(int i)
{
int n=0,m=0;
while(i%2==0) i/=2;
while(i%5==0) i/=5;
if(i==1) return 0;
while(1)
{
m++;
n=(n*10+9)%i;
if(n==0) break;
}
return m;
}
int main()
{
int n=0,m;
for(int i=2;i<=1000;i++)
{
int l=abc(i);
if(l>n) {n=l;m=i;}
}
cout<<m<<endl;
return 0;
}