1035 最长的循环节

数论

1035 最长的循环节

  1. 1 秒
  2.  
  3. 131,072 KB
  4.  
  5. 20 分
  6.  
  7. 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
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=1e6+5;
int res[M],n;

void fun(){
    memset(res,0,sizeof(res));
    for(int k=1;k<=1000;k++){
        int i=k;
        while(i%2==0)
            i/=2;
        while(i%5==0)
            i/=5;
        int ans=1;
        for(int j=1;j<=i;j++){
            ans*=10;
            ans%=i;
            if(ans==1){
                res[k]=j;
                break;
            }
        }
    }
}

int main(){
    fun();
    scanf("%d",&n);
    int ans=1;
    for(int i=1;i<=n;i++){
        if(res[i]>res[ans])
            ans=i;
    }
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/black_horse2018/article/details/83793510