1035 最长的循环节
- 1 秒
- 131,072 KB
- 20 分
- 3 级题
正整数k的倒数1/k,写为10进制的小数如果为无限循环小数,则存在一个循环节,求<=n的数中,倒数循环节长度最长的那个数,假如存在多个最优的答案,输出所有答案中最大的那个数。
1/6= 0.1(6) 循环节长度为1
1/7= 0.(142857) 循环节长度为6
1/9= 0.(1) 循环节长度为1
收起
输入
输入n(10 <= n <= 1000)
输出
输出<=n的数中倒数循环节长度最长的那个数
输入样例
10
输出样例
7
网上的神仙数论解法看不懂,菜逼只会暴力模拟,
模拟除法,当余数再次出现的时候,则发现循环节
可以用数组下标记录,也可以用map记录当前余数的出现位置,数据大的时候还是map香
#include <iostream>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef __int128 bll;
const ll maxn = 1e6+100;
const ll mod = 1e9+7;
const ld pi = acos(-1.0);
const ll inf = 1e18;
const ld eps = 1e-5;
const ld e = exp(1);
ld n;
ll sm[1005];
ll ma,ans = 1;
ll getlen(ll x)
{
ll len = 0,fz = 1;
memset(sm,0,sizeof(sm));
while(fz)
{
fz *= 10;
fz %= x;
if(sm[ fz ] != 0)
{
return len;
}
sm[ fz ]++;
len++;
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
//cin.tie(0),cout.tie(0);
cin >> n;
ma = 0;
for(ld i = 2; i <= n; i++)
{
ll t = getlen(i);
if(t > ma)
{
ma = t;
ans = i;
}
}
cout << ans << endl;
return 0;
}