牛客网———整数拆分

题目描述

一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1+1+2+2 7=1+1+1+1+1+2 7=1+1+1+1+1+1+1 总共有六种不同的拆分方式。 再比如:4可以拆分成:4 = 4,4 = 1 + 1 + 1 + 1,4 = 2 + 2,4=1+1+2。 用f(n)表示n的不同拆分的种数,例如f(7)=6. 要求编写程序,读入n(不超过1000000),输出f(n)%1000000000。
链接:https://www.nowcoder.com/questionTerminal/376537f4609a49d296901db5139639ec
来源:牛客网

#include<iostream>
#define MAXSIZE 1000001
using namespace std;
 
int main(){
    int n;
    int result[MAXSIZE];
    result[0] = result[1] = 1;
    for(int i = 2; i<MAXSIZE; ++i){
        if(i%2 == 0){
            result[i] = (result[i-1] + result[i/2])%1000000000;
        }
        else{
            result[i] = result[i-1]%1000000000;
        }
    }
    while(scanf("%d",&n) != EOF)
        cout<<result[n]<<endl;
    return 0;
}

  

posted @ 2018-12-02 22:02  JAYPARK01  阅读(226)  评论(0编辑  收藏  举报