1. 程式人生 > >1035 最長的迴圈節

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;
}