POJ 2229 Sumsets (DP or math)

Source: Internet
Author: User

Description

Farmer John commanded his cows to search forDifferent sets of numbers that sum to a given number. The cows use is numbers that is an integer power of2. Here is the possible sets of numbers that sum to7: 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+4Help FJ count all possible representations forA given integer N (1<= N <=1, the, the).

Input

A single line with a single integer, N.

Output

 as  This 9 digits (inbase representation).

Sample Input

7

Sample Output

6

Source

Usaco 2005 January Silver

If I is an odd number, there must be a 1, the f[i-1] in each case plus a 1 to get fi, so f[i]=f[i-1]

If I is an even number, if there is 1, at least two, then f[i-2] each case plus two 1, get I, if not 1, the decomposition of each item in addition to 2, then get F[I/2]

So F[I]=F[I-2]+F[I/2]

Since just output the last 9 digits, don't forget modulo 1000000000

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6#include <cmath>7 using namespacestd;8 #defineN 10100069 #defineMOD 1000000000Ten intN; One intDp[n]; A voidinit () { -      -dp[1]=1; thedp[2]=2; -      for(intI=3; i<n;i++){ -         if(i&1){ -dp[i]=dp[i-1]; +         } -         Else{ +dp[i]=dp[i-1]+dp[i/2]; Adp[i]%=MOD; at         } -     } - } - intMain () - { - init (); in      while(SCANF ("%d", &n) = =1){ -          toprintf"%d\n", Dp[n]); +      -     } the     return 0; *}
View Code

POJ 2229 Sumsets (DP or math)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.