【NOIP1997】【Luogu2626】斐波那契数列(枚举,质因数分解)

problem

  • 输入n,求 f ( n ) % 2 31 ,并将其分解质因数
  • n<=48

solution

看了数学题不敢写,然而普及组的题。。。照例是枚举的,没啥优化。n=50。。。
1、递推每次mod不用开longlong。、
2、直接枚举1-f[n]分解质因数不会超时
3、开关特判第一次不要乘号。

codes

#include<iostream>
#include<cmath>
using namespace std;
const int mod = 1<<31;
int f[50];
int main(){
    int n;  cin>>n;
    if(n==1 || n==2){cout<<"1\n";return 0;}
    f[1] = f[2] = 1;
    for(int i = 3; i <= n; i++)
        f[i] = (f[i-1]+f[i-2])%mod;
    cout<<f[n]<<'='; int ok = 0;
    for(int i = 2; i <= f[n]; i++){
        while(f[n]%i == 0){
            if(ok)cout<<'*';//第一次不要乘号
            cout<<i;
            f[n] /= i;
            ok = 1;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33957603/article/details/81193049
今日推荐