Whose turn is it? mod

Insert picture description here
Idea: This is a Fibonacci sequence, except that the remainder of m is required every time f[i] is looped. f[i] is the number of rabbits in the i-th month. The number of the last group of rabbits this month is (f[n]+m-1)%m.

#include <bits/stdc++.h>

using namespace std;
int f[1005];

int main()
{
    
    
    int t; cin>>t;
    while(t--){
    
    
        int n,m; cin>>n>>m;
        f[1] = 1;f[2] = 1;
        for(int i=3;i<=n;i++){
    
    
            f[i] = (f[i-1]+f[i-2])%m;
        }
        cout<<(f[n]+m-1)%m<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43811879/article/details/109629488