zoukankan      html  css  js  c++  java
  • 整数分解为2的幂 数学

    任何正整数都能分解成2的幂,给定整数N,求N的此类划分方法的数量!由于方案数量较大,输出Mod 1000000007的结果。 
    比如N = 7时,共有6种划分方法。 

    7=1+1+1+1+1+1+1 
      =1+1+1+1+1+2 
      =1+1+1+2+2 
      =1+2+2+2 
      =1+1+1+4 
      =1+2+4 
    Input输入一个数N(1 <= N <= 10^6)Output输出划分方法的数量Mod 1000000007Sample Input

    7

    Sample Output

    6

    找规律
    #include<iostream>
    #include<cstring>
    using namespace std;
    //每个点最多翻转一次
    const int MAXN = 1e6 + 3;
    #define INF 0x3f3f3f3f
    /*
    ans(k) = ans(k-1)+ ans(k-2) + ans(k-4)
    */
    int a[MAXN];
    int main()
    {
        int i = 0;
        a[1] = 1;
        for(int i= 2; i < MAXN ;i++)
        {
            if(i&1)
                a[i] = a[i-1];
            else
                a[i] = (a[i/2] + a[i-1])%1000000007;
        }
        ios::sync_with_stdio(0);
        int n;
        while(cin>>n)
            cout<<a[n]<<endl;
    }

    #include<iostream>#include<cstring>usingnamespacestd; //每个点最多翻转一次constint MAXN = 1e6 + 3; #define INF 0x3f3f3f3f/* ans(k) = ans(k-1)+ ans(k-2) + ans(k-4) */int a[MAXN]; int main() { int i = 0; a[1] = 1; for(int i= 2; i < MAXN ;i++) { if(i&1) a[i] = a[i-1]; else a[i] = (a[i/2] + a[i-1])%1000000007; } ios::sync_with_stdio(0); int n; while(cin>>n) cout<<a[n]<<endl; }

  • 相关阅读:
    Socket编程
    jdbc03 使用servlet实现
    el和jstl
    java03变量和基本数据类型
    java02
    ssh整合
    U1总结
    多线程
    spring07 JDBC
    cocos2dx中的三种基本的数据类型
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7426647.html
Copyright © 2011-2022 走看看