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;
    }
相關文章
相關標籤/搜索