您的位置:首页 > 其它

51Nod-1035-最长的循环节

2016-08-05 17:54 239 查看
ACM模版

描述



题解

已知整数n,求最小的k使10^k ≡1 (mod n),k<=n。

代码

#include <iostream>
#include <cstring>

using namespace std;

const int MAXN = 1005;

int res[MAXN];  //  循环节长度

int main()
{
memset(res, 0, sizeof(res));

int i, temp, j, n;

for (temp = 1; temp <= 1000; temp++)
{
i = temp;
while (i % 2 == 0)
{
i /= 2;
}
while (i % 5 == 0)
{
i /= 5;
}
n = 1;
for (j = 1; j <= i; j++)
{
n *= 10;
n %= i;
if (n == 1)
{
res[temp] = j;
break;
}
}
}

int max_re;

while (cin >> n)
{
max_re = 1;
for (i = 1; i <= n; i++)
{
if (res[i] > res[max_re])
{
max_re = i;
}
}
cout << max_re << endl;
}
return 0;
}


参考

《1/n循环节长度》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数论