P4994 终于结束的起点

P4994 终于结束的起点

如今,给你一个模数 M,请你求出最小的 n > 0,使得 \(\mathrm{fib}(n) \bmod M = 0, \mathrm{fib}(n + 1) \bmod M = 1\)ios

Solution

\(NOIp\) 以前要搞点这种题找自信的啊
此题直接枚举便可
优化空间滚动数组便可
然而上考场咱们不能这么就完了
应该打表看看循环节与 \(M\) 的关系
在发现 \(M\) 与其循环节相差不大, 估算出复杂度再打
否则就打完暴力, 看完其余题在回来想正解数组

Code

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<climits>
#define LL long long
#define REP(i, x, y) for(int i = (x);i <= (y);i++)
using namespace std;
int RD(){
    int out = 0,flag = 1;char c = getchar();
    while(c < '0' || c >'9'){if(c == '-')flag = -1;c = getchar();}
    while(c >= '0' && c <= '9'){out = out * 10 + c - '0';c = getchar();}
    return flag * out;
    }
int M;
int n[7];
void init(){
    M = RD();
    n[0] = 0;
    n[1] = 1;
    }
void solve(){
    int now = 1;
    while(1){
        int temp = (n[0] + n[1]) % M;
        if(temp == 1 && n[1] == 0){
            printf("%d\n", now);
            return ;
            }
        n[0] = n[1];
        n[1] = temp;
        now++;
        }
    }
int main(){
    init();
    solve();
    return 0;
    }
相关文章
相关标签/搜索