Poj2229--Sumsets(递推)

Sumsets

Time Limit: 2000MS   Memory Limit: 200000K
Total Submissions: 15052   Accepted: 6001

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

Source

USACO 2005 January Silver

题目很有意思, 没想到是个递推, 脑子不够用。

#include <cmath>
#include <cstdio>
const int  MOD = 1e9;
const int N = 1000001;
int num[N];
void Sieve(){
    num[1] = 1;
    num[2] = 2;
    num[3] = 2;
    for(int i = 4; i <= N; i++){
        if(i & 1)
            num[i] = num[i-1];
        else
            num[i] = (num[i-2]%MOD + num[i/2]%MOD)%MOD;
    }
}
int main(){
    Sieve();
    int n;
    while(scanf("%d", &n) != EOF)
        printf("%d\n", num[n]);
    return 0;
}
时间: 2024-08-02 22:18:02

Poj2229--Sumsets(递推)的相关文章

POJ 2229 Sumsets(递推,思考)

/* n = 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 */ /* 若i为偶数 : 若有 1 ,至少有两个 ---->f[i-2]的情况+两个1, 若没有1 , 将偶数分解/2-----> f[i/2]; 则dp[i] = dp[i / 2] + dp[i-2] 若i为奇数: 必定有 1, dp[i] = 减去1的序列,加上1 则dp[i] = dp[i – 1] */ 1 #in

POJ 2229 Sumsets (递推&amp;整数划分变形)

http://poj.org/problem?id=2229 思路:假设加数按从小到大的顺序.当n为奇数时,第一个数必须为1,此时f(n)=f(n-1):当n为偶数时,分两种情况讨论,若第一个数为1,则f(n)=f(n-1),若第一个数不为奇数,则所有数都不为奇数,提出一个公因子2出来,就是f(n/2),所以,f(n)=f(n-1)+f(n/2) 完整代码: /*63ms,4300KB*/ #include<cstdio> const int mod = 1e9; const int maxn

HDU2709 Sumsets【递推】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2709 题目大意: 将一个整数N分解成2^i相加的形式,共有多少种分法.例如:7 = 1+1+1+1+1+1+1 = 1+1+1+1+1+2 = 1+1+1+2+2 = 1+1+1+4 = 1+2+2+2 = 1+2+4,共有6种分法. 思路: 设a[n]为整数n分解成2^i相加形式的分法个数. 当n为奇数时,n-1为偶数,n = 1 + n-1,分解出一个1,再分解偶数n-1,也就是a[n-1]种

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

poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 8281 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

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

hdu 1267 递推

下沙的沙子有几粒? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4326    Accepted Submission(s): 2268 Problem Description 2005年11月份,我们学校参加了ACM/ICPC 亚洲赛区成都站的比赛,在这里,我们获得了历史性的突破,尽管只是一枚铜牌,但获奖那一刻的激动,也许将永远铭刻

hdu 2067(递推或卡特兰数【待补充】)

//解法一:递推#include<iostream> using namespace std; long long d[36][36]; int main() { for(int i=1;i<=35;i++) { d[0][i]=1; } for(int i=1;i<=35;i++) for(int j=i;j<=35;j++) { if(i==j) d[i][j]=d[i-1][j]; else d[i][j]=d[i-1][j]+d[i][j-1]; } int n; i

NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))

Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m.但是大家来的时间不同,随机次序来机房,带电脑的人直接准备比赛而没带电脑的人需要向带电脑并还没和别人公用的人求助(当然会答应).但是,如果不存在带电脑并还没和别人公用的人,那他就要等了,等是很让人头疼的,这就不和谐了,当然假如没有这样的情况发生比赛是很和谐的. Input 输入多组数据,每组数据只有一行m(

矩阵经典题目七:Warcraft III 守望者的烦恼(矩阵加速递推)

https://www.vijos.org/p/1067 很容易推出递推式f[n] = f[n-1]+f[n-2]+......+f[n-k]. 构造矩阵的方法:构造一个k*k的矩阵,其中右上角的(k-1)*(k-1)的矩阵是单位矩阵,第k行的每个数分别对应f[n-1],f[n-2],,f[n-k]的系数.然后构造一个k*1的矩阵,它的第i行代表f[i],是经过直接递推得到的.设ans[][]是第一个矩阵的n-k次幂乘上第二个矩阵,f[n]就是ans[k][1]. 注意:用__int64 #in