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. 前端逼死强迫症系列之css

    一.编写css样式 1.ID选择器 由于ID唯一,所以也是写多遍. <head> <style> #i1{ background-color: #2459a2; height: ...

  2. mysql建表问题

    PUBLIC Stack Overflow Tags Users Jobs TeamsQ&A for workLearn More   MySQL error: The maximum col ...

  3. c++ 排序,<< 运算符重载

    #include <iostream> #include <functional> #include <list> using namespace std; ost ...

  4. 3.5寸1.44M软盘结构

    结构: 划分: 簇:磁盘驱动器在向磁盘读取和写入数据时,要以扇区为单位.在磁盘上,DOS操作系统是以“簇”为单位为文件分配磁盘空间的.硬盘的簇通常为多个扇区,与磁盘的种类.DOS 版本及硬盘分区的大小 ...

  5. Ubuntu 14.04 下安装redis后运行redis-cli 报出redis Connection refused错误【已解决】

    在运行redis-cli运行后爆出错误,看了网上的都没有用例如:改ip,注释bind 127.0.0.1,或者是先运行./redis-server redis.conf,都没有用 只需要: 找到red ...

  6. PHP 之文件上传类封装

    一.前端代码 <!doctype html> <html lang="en"> <head> <meta charset="UT ...

  7. 【git 命令总结】一

    git help config init commit diff rename mv rm head revert reset branch checkout branch-diff fast-for ...

  8. 使用sequelize-auto 生成mysql 表的实体时主键没有 autoIncrement: true 属性

    使用sequelize-auto 生成mysql 表时主键没有 autoIncrement: true 属性,这会导致插入数据时报错.看git上面是已经解决了的,解决方法是修改查询语句模板.我用的是0 ...

  9. 进程| 线程 | 阻塞 | 阻塞&非阻塞 和 同步&异步

    阻塞&非阻塞 阻塞IO 调用之后一定要等到系统内核完成所有的操作之后才结束,因此它的缺点:CPU等待IO,处理能力得不到充分利用. 非阻塞IO 为了解决阻塞IO带来的一些问题,内核提供了非阻塞 ...

  10. kotlin中抽象类

    抽象类和接口很类似,抽象类不能被实例化需要使用abstract 关键字声明,抽象类实现接口后,接口中没有函数体的函数可以不重写,接口中的这些方法就自动被继承到实现接口的抽象类中,称为抽象方法 pack ...