Sumsets

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 45   Accepted Submission(s) : 20

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

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,n又2的幂次方相加得到,问有多少中相加的方式
分析 当n为奇数的时候 就是再前一个的基础上加上1,a[n]=a[n-1]
  当n为偶数的时候:
    如果加数里含有1,则一定至少有2个1,就是对n-2后面+1+1,就是a[n-2]
    如果加数里面没有1,即对n/2的每一个加数乘以2,总类数为a[n/2]
  所以n为偶数时的总类数为a[n]=a[n-2]+a[n/2]
 #include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
int a[+];
int main()
{
int n;
a[]=,a[]=;
for(int i=;i<=;i++)
{
if(i%)
{
a[i]=a[i-];
}
else
{
a[i]=a[i-]+a[i/];
a[i]%=;
}
}
while(~scanf("%d",&n))
{
printf("%d\n",a[n]);
}
return ;
}

Sumsets 递推的更多相关文章

  1. hdu2709 Sumsets 递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2709 感觉很经典的一道递推题 自己想了有半天的时间了....比较弱.... 思路: 设f[n]表示和为 ...

  2. poj2229 Sumsets (递推)

    http://poj.org/problem?id=2229 看到题目能感觉到多半是动态规划,但是没有清晰的思路. 打表找规律: #include<cstdio> #include< ...

  3. 【POJ】2229 Sumsets(递推)

    Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 20315   Accepted: 7930 Descrip ...

  4. [USACO2005][poj2229]Sumsets(递推)

    http://poj.org/problem?id=2229 分析: 显然的递推 若n为奇数,那么肯定是在n-1的基础上前面每个数+1,即f[n]=f[n-1] 若n为偶数 当第一位数字是1的时候,等 ...

  5. POJ 2229 Sumsets(递推,找规律)

    构造,递推,因为划分是合并的逆过程,考虑怎么合并. 先把N展开成全部为N个1然后合并,因为和顺序无关,所以只和出现次数有关情况有点多并且为了避免重复,分类,C[i]表示序列中最大的数为2^i时的方案数 ...

  6. BZOJ 1677 [Usaco2005 Jan]Sumsets 求和:dp 无限背包 / 递推【2的幂次方之和】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1677 题意: 给定n(n <= 10^6),将n分解为2的幂次方之和,问你有多少种方 ...

  7. poj 2229 【完全背包dp】【递推dp】

    poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 828 ...

  8. 【BZOJ-2476】战场的数目 矩阵乘法 + 递推

    2476: 战场的数目 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 58  Solved: 38[Submit][Status][Discuss] D ...

  9. 从一道NOI练习题说递推和递归

    一.递推: 所谓递推,简单理解就是推导数列的通项公式.先举一个简单的例子(另一个NOI练习题,但不是这次要解的问题): 楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可 ...

随机推荐

  1. mybatis学习记录五——动态sql

    8       动态sql 8.1     什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 8.2     需求 用户信息综合查询列表 ...

  2. img的空白内容如何处理

    给img加一个 vertical-align: bottom;

  3. 学习WebSocket笔记

    由于HTTP协议是无状态的,服务器只会响应来自客户端的请求,但是它与客户端之间不具备持续连接. 当用户在浏览器上进行操作时,可以请求服务器上的api:但是反过来不可以:服务端发生了一件事,无法将这个事 ...

  4. Restrramework源码(包含组件)分析

    1.总体流程分析 rest_framework/view.py 请求通过url分发,触发as_view方法,该方法在ViewSetMixin类下 点进去查看as_view源码说明,可以看到它在正常情况 ...

  5. linux保留旧版本python,安装python3

    1.备份老版本 mv /usr/bin/python /usr/bin/python.bak 2. 下载python3 wget https://www.python.org/ftp/python/3 ...

  6. Linux_vsftpd服务配置

    首先安装Linux 企业版第一张光盘中的vsftpd-2.0.1-5.i386.rpm#rpm –ivh /media/cdrom/RedHat/RPMS/vsftpd-3.0.1-5.i386.rp ...

  7. 【oracle使用笔记2】使用Oracle数据库遇到的若干问题总结

    一. 关于Oracle 11g数据库在查询表中数据显示中文乱码问题 [描述]本人一开始使用的Oracle是11g版本的,用PLSQL一次查询表中的数据时出现了中文显示乱码,为此搜了许多解决办法,最终通 ...

  8. 使用poi读取excel文件 Cannot get a text value from a numeric cell

    我这样转换得到一个excel文本域的值 Cell cell = row.getCell(c); cell.setCellType(Cell.CELL_TYPE_STRING); String park ...

  9. BZOJ2659: [Beijing wc2012]算不出的算式(数学)

    Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1575  Solved: 939[Submit][Status][Discuss] Descriptio ...

  10. VS中R转义字符处理

    std::string s1 = R"(Name="Hello World ... ")"; std::string s2 = R"-(Name=&q ...