Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:

1) 1+1+1+1+1+1+1 
2) 1+1+1+1+1+2 
3) 1+1+1+2+2 
4) 1+1+1+4 
5) 1+2+2+2 
6) 1+2+4

Help FJ count all possible representations for a given integer N (1 <= N
<= 1,000,000).

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum.
Due to the potential huge size of this number, print only last 9 digits (in
base 10 representation).

Sample Input

7

Sample Output

6


题意:整数N用2^n之和的形式表示的方案数

思路:当N>1时,若N为奇数,则每个分解方案中至少含有一个1项。此时若每种分解方案中去掉一个1项,方案数不发生改变。

   即     N分解的方案数=(N-1)分解的方案数

   若该N为偶数,则分为两类情况:

   1、含有1项,同上,每个方案中去掉1项,方案数不变。

   2、不含有1项,此时每个方案中最小项应为2,若将每一项除2,方案数不变。

   即     N分解的方案数=(N-1)分解的方案数+(N/2)分解的方案数。

边界条件:当N=1时只有一种分解方案。

注意:可能溢出,需要取模


 #include<cstdio>
int s[];
int main()
{
int n; s[]=;
for( int i=; i<=; i++){
if( i%== )
s[i]=(s[i-]+s[i/])%;
else
s[i]=s[i-];
}
while(~scanf("%d",&n)){
printf("%d\n",s[n]);
} return ;
}

DP_Sumsets的更多相关文章

随机推荐

  1. 线段树详解 (原理,实现与应用)(转载自:http://blog.csdn.net/zearot/article/details/48299459)

    原文地址:http://blog.csdn.net/zearot/article/details/48299459(如有侵权,请联系博主,立即删除.) 线段树详解    By 岩之痕 目录: 一:综述 ...

  2. slax中改变终端字体

    修改~/.Xresources文件 ! English font      Xterm*faceName: DejaVu Sans Mono=True:size=16 修改颜色: ! colos XT ...

  3. 6.3 MRUnit写Mapper和Reduce的单元测试

    1.1  MRUnit写单元测试 作用:一旦MapReduce项目提交到集群之后,若是出现问题是很难定位和修改的,只能通过打印日志的方式进行筛选.又如果数据和项目较大时,修改起来则更加麻烦.所以,在将 ...

  4. spring+mybatis事务的readonly属性无效

    在Spring配置事务中设置的read-only="true"不起作用,仍可以执行写操作:但是其他的正常.查看了一下DataSourceTransactionManager这个类的 ...

  5. Quickstart: Create and publish a package using Visual Studio (.NET Framework, Windows)

    https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio-n ...

  6. Django中的 返回json对象的方式

    在返回json对象的几种方式: 1 from django.shortcuts import render, HttpResponse # Create your views here. from d ...

  7. UML期末复习题——2.6:Package Diagram

    第六题 包图 重要概念: 1.包图(package Diagram) 由若干个包以及包之间的关系组成.包是一种分组机制,其将一些相关的类集合为一个包,形成高内聚,低耦合的类集合,可以说,一个包相当于一 ...

  8. C之堆栈

    栈* 自动申请,自动释放* 大小固定,内存空间连续* 从栈上分配的内存叫静态内存 堆* 程序员自己申请* new/malloc* 大小取决于虚拟内存的大小,内存空间不连续* java中自动回收,C中需 ...

  9. Objective-C中的一些方法命名“潜规则”

    在基于Apple Xcode的Objective-C中,有一些方法命名潜规则,比如就property而言,假定你定义了如下property: @interface MyObject @property ...

  10. PHP + Smarty + MySQL

    Help me please! How to transfer data from table to smarty? Function: public function getBanLog() { g ...